Computing.Net > Forums > Unix > loops in shell scripts!!!

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

loops in shell scripts!!!

Reply to Message Icon

Name: mdp02ab
Date: June 11, 2003 at 08:26:32 Pacific
OS: Unix
CPU/Ram: P4
Comment:

Hi,

I'm trying to run loops in shell scripts using the following script.

loop=1
while [ loop -lt 10 ]
do
echo 'this is my $loop text-line'
loop='expr $loop + 1'
done
----
but I'm getting error:
test: unknown parameter $loop
when I change ' by " in the above expr statement, I get the error:
test: unknown parameter 0

I need urgent help for this script. please mail your responses to: mdp02ab@sheffield.ac.uk

thanks in advance.

cheers,
amit.



Sponsored Link
Ads by Google

Response Number 1
Name: David Perry
Date: June 11, 2003 at 11:29:22 Pacific
Reply:

while [ $loop -lt 10 ] ; do
echo "this is my $loop text-line"
loop=`expr $loop + 1`
done

you need a "$" before the variable.
expressions with variables need double quotes
use the back-tick character instead of a single quote for evaluation.


0

Response Number 2
Name: WilliamRobertson
Date: June 12, 2003 at 12:04:39 Pacific
Reply:

Or in Korn shell:

#!/bin/ksh

integer i=0 limit=10

while (( i limit )) && (( i += 1 ))
do
    print "This is iteration ${i}"
done


0

Response Number 3
Name: WilliamRobertson
Date: June 12, 2003 at 12:07:22 Pacific
Reply:

As usual forgot to quote the less-than sign in posting. That should have been:

#!/bin/ksh

integer i=0 limit=10

while (( i < limit )) && (( i += 1 ))
do
print "This is iteration ${i}"
done


0

Response Number 4
Name: WilliamRobertson
Date: June 13, 2003 at 06:05:34 Pacific
Reply:

Or as I've recently discovered, the even more concise:

#!/bin/ksh

integer i=0 limit=10

while (( i++ < limit ))
do
    print "This is iteration ${i}"
done


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: loops in shell scripts!!!

Using hex-code in shell script www.computing.net/answers/unix/using-hexcode-in-shell-script/4227.html

Array size in shell script? www.computing.net/answers/unix/array-size-in-shell-script/5761.html

SQL in shell script www.computing.net/answers/unix/sql-in-shell-script/5538.html