Sign in to follow this  
shanenin

Loop Script

Recommended Posts

I think i have copied this script exactly as printed in my book, but it keeps failing

[email protected] shane $ cat scripts/loop
#!/bin/bash
x=0
while [ $x -lt 10 ]
do
  echo $x
  x=`echo "$X + 1" | bc`
done
[email protected] shane $ ./scripts/loop
0
(standard_in) 1: parse error
./scripts/loop: line 3: [: -lt: unary operator expected

Share this post


Link to post
Share on other sites

Typo. 'x' is capitalized on the sixth line.

Edited by jcl

Share this post


Link to post
Share on other sites

thanks. I can see debugging programs being a huge undertaking. I have trouble finding them with just three lines of code ;)

Share this post


Link to post
Share on other sites
thanks. I can see debugging programs being a huge undertaking. I have trouble finding them with just three lines of code  ;)

:)

By the way, if your book doesn't mention it, that could be rewritten to use shell arithmetic expansion:

#!/bin/bash
x=0
while [ $x -lt 10 ]
do
   echo $x
   x=$(($x + 1))
done

or even:

#!/bin/bash
x=0
while [ $x -lt 10 ]
do
   echo $((x++))
done

Not that I recommend deviating from the book.

Share this post


Link to post
Share on other sites

what is -lt (is that like NE or !=)

while [ $x -lt 10 ]

Not that I recommend deviating from the book.

an why should you suggest a better way.. :D

thanks

Share this post


Link to post
Share on other sites

yep..:)

sorry with this font I thought it was (one)t and I could not figure out what that stood for..

bash is confusing to me that way.. im used to < or <= .. this lt or ne and so on alwyas cates me off guard.

thanks..

Edited by iccaros

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this