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
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
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...
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
Re: How to call each argument in a while loop
is it possible to accomplish this using a while loop?
Re: How to call each argument in a while loop
Quote:
Originally Posted by
sisco311
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++;
Re: How to call each argument in a while loop
Quote:
Originally Posted by
ofnuts
nice_bash_tricks_I_know++;
It's standard. Here are the POSIX specs: http://pubs.opengroup.org/onlinepubs.../contents.html
Re: How to call each argument in a while loop
Quote:
Originally Posted by
toad3000
is it possible to accomplish this using a while loop?
Yes. But why?
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.
Re: How to call each argument in a while loop
Quote:
Originally Posted by
sisco311
But I assume so of course... a "trick" is standard. A "hack" is not.