Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop through positional variables 2

Status
Not open for further replies.

ravioli

Programmer
Mar 30, 2004
39
US
Hi All,

I have a brain teaser. I am attempting to loop through a number of positional parameters but keep getting caught in an infinite loop.

The input is : $ script_name (p_par_1) ...(p_par_n)

The script I have:

#!/usr/bin/ksh
x=1
while [ -n $`$x` ] ; do #do while x is not null
echo $`$x` #print the value of $1, $2, etc.
x=x+1 # increment x to evaluate next
done #script should end when pos_vars run out or [-n $n]

Problem is that it treats $`$x` as a literal and never evaluates it as $1, $2 , etc as I want it to. I think I am close and that it is something with the quotes or something easy.

Ravioli
 
In your shell man page take a look at the eval builtin function.

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
You can also use a for loop :
[tt]
for p_par
do
echo $ppar
done
[/tt]

Jean Pierre.
 
PHV,

Can one use "eval" within a [test condition] ?

Bradley
 
Jean Pierre,

Does a for loop quit when it runs our of data?
In this case p_par . I will have to look into this.

Bradley

 
So here is what I did:

for p_par in $*; do
echo $p_par
done

It worked great. This was just my test script for a more complicated process where I had to take action on each positional variable. It worked just as easily. Thank you, PHV and Jean Pierre.

Bradley


 
[tt]for p_par[/tt]

is the same thing than

[tt]for p_par in "$@"[/tt]

Jean Pierre.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top