Recommended Posts

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

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

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.

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

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
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...