Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: How to call each argument in a while loop

  1. #1
    Join Date
    Feb 2013
    Beans
    27

    How to call each argument in a while loop

    I want to access each argument so I tried doing ${$counter2}} because that was my first intuition but I keep getting bad substitution error. Does anyone know how to do this?

    Code:
    typeset -i counter=1typeset -i counter2=1
    while [ $counter2 -le $# ] 
    do
    cat ${$counter2} | while read line 
    do
    echo "$counter. $line"
    counter=counter+1
    done
    counter2=counter2+1
    done
    Last edited by toad3000; March 10th, 2013 at 06:13 AM.

  2. #2
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,664
    Distro
    Ubuntu 13.10 Saucy Salamander

    Re: How to call each argument in a while loop

    Are you just trying to access command line arguments? Can we assume you want to write this in Bash shell script?

    test.sh
    Code:
    #!/bin/bash
    
    for arg in "$@"
    do
        echo $arg
    done
    Code:
    $ ./test.sh a b c
    a
    b
    c
    Please create new threads for new questions.
    Please wrap code in code tags using the '#' button or enter it in your post like this: [code]...[/code].

  3. #3
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    1,815
    Distro
    Kubuntu 12.10 Quantal Quetzal

    Re: How to call each argument in a while loop

    You don't really need this. If you want to iterate on your arguments, you can use:

    Code:
    for arg in "$@"
    do
        echo "Some arg: $arg"
    done
    The quotes around "$@" are important...

  4. #4
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: How to call each argument in a while loop

    If the 'in words...' part is ommited then in "$@" is assumed. So you can simply run:
    Code:
    for arg
    do
        ...
    done

  5. #5
    Join Date
    Feb 2013
    Beans
    27

    Re: How to call each argument in a while loop

    is it possible to accomplish this using a while loop?

  6. #6
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    1,815
    Distro
    Kubuntu 12.10 Quantal Quetzal

    Re: How to call each argument in a while loop

    Quote Originally Posted by sisco311 View Post
    If the 'in words...' part is ommited then in "$@" is assumed. So you can simply run:
    Code:
    for arg
    do
        ...
    done
    nice_bash_tricks_I_know++;

  7. #7
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: How to call each argument in a while loop

    Quote Originally Posted by ofnuts View Post
    nice_bash_tricks_I_know++;
    It's standard. Here are the POSIX specs: http://pubs.opengroup.org/onlinepubs.../contents.html

  8. #8
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: How to call each argument in a while loop

    Quote Originally Posted by toad3000 View Post
    is it possible to accomplish this using a while loop?
    Yes. But why?

  9. #9
    Join Date
    Feb 2013
    Beans
    27

    Re: How to call each argument in a while loop

    because I tried many ways to accomplish this using a while loop and It wasn't working. I made a counter and I tried ${$counter} but I get bad substitutional error.

  10. #10
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    1,815
    Distro
    Kubuntu 12.10 Quantal Quetzal

    Re: How to call each argument in a while loop

    Quote Originally Posted by sisco311 View Post
    It's standard. Here are the POSIX specs: http://pubs.opengroup.org/onlinepubs.../contents.html
    But I assume so of course... a "trick" is standard. A "hack" is not.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •