Simple while-loop problem

Maybe because its Friday, but I can't get a simple while loop to work!

#!bin/bash
 
i=0
while [ $i < 20  ]
do
 
echo "Hello"
 
((i++))
 
done

Hi.

Arithmetic operations are done with -lt, -eq, -gt, etc.

So...

while [ $i -lt 20  ]

still not working, says i++ not found :frowning:

I never use this notation, although it should work:

((i++))

Try:

i=$((i + 1))

Still no, says i=$ unexpected

#!/bin/bash
1 Like

oh i see, but still no,says i not found now

Combining the various suggestions above:

#!/bin/bash
# Output the word "Hello" exactly 20 times
i=0
while [ $i -ne 20  ]
do
       echo "Hello"
       i=$((i + 1))
done

Nope, still not working, says syntax error at line 8: 'i=$' unexpected....if it helps to know im on Solaris..

If you have bash, it should work!

If this works, I suspect you're using sh:

i=`expr $i + 1`

Is

#!/bin/bash

the first line of your script?

what does

ls -l `which bash`

show?

I suppose you're invoking the script in a way that the shebang is ignored and the script is executed by the old Bourne shell on Solaris.

You will need something like this in order to make it work with the old Bourne shell:

i=0
while [ $i -ne 20  ]
do
       echo "Hello"
       i=`expr $i + 1`
done

If we haven't got bash, try ksh?

#!/bin/ksh
# Output the word "Hello" exactly 20 times
i=0
while [ $i -ne 20  ]
do
       echo "Hello"
       i=$((i + 1))
done

Or as suggested earlier do it in a way which works with Bourne Shell, ksh, bash and Posix shells.

# Output the word "Hello" exactly 20 times
i=0
while [ $i -ne 20  ]
do
       echo "Hello"
       i=`expr $i + 1`
done

1) No
2)Yes
3) no such file or directory :s

---------- Post updated at 11:36 AM ---------- Previous update was at 11:35 AM ----------

says unknown test operator $i

---------- Post updated at 11:37 AM ---------- Previous update was at 11:36 AM ----------

:frowning: need i say more :frowning:

Yes,
you need to post the exact content of the script (including the modified versions you're testing), the exact command you're executing and all the output/error messages that you're getting (copy/paste, please, don't try to re-write).

Enabling xtrace/verbose output would be beneficial also, hint: add this line to your script, after the first line:

set -xv

i would LOVE to copy and paste everything but the set-up of computers means that only this one has the internet, whilst the one with unix doesn't!!! But I assure you I check everything 3 times now after missing teh !#/bin/bash thing! and its all there! and the error messages are pretty much as I type, nothing more, nothing less..

---------- Post updated at 12:06 PM ---------- Previous update was at 11:59 AM ----------

On a slightly different note, how would I get the unix OS on my laptop at home without having to install it? like with the virtual box thing? Do you guys reccomend anything?

ok so when i do the ps command, i get:

  PID TTY         TIME CMD
 14876 pts/63       0:03 tcsh
 14864 pts/63       0:03 tcsh

Yep it would really help to know your environment and the vintage of your Solaris:

What is the output from the following enquiry commands.

echo $SHELL

which sh
which ksh
which bash

uname -a

---------- Post updated at 12:34 ---------- Previous update was at 12:30 ----------

Just seen your last post.

Very surprised to see "tcsh". That is just about the least useable Shell there is.
Any way you can get your Systems Administrator to make a Posix shell available to your account?

---------- Post updated at 12:37 ---------- Previous update was at 12:34 ----------

On the other subject, please start a new thread making it absolutely clear what make model and specification of laptop you have, any existing Operating Systems and how much free disc space you have.

---------- Post updated at 12:37 ---------- Previous update was at 12:34 ----------

On the other subject, please start a new thread making it absolutely clear what make model and specification of laptop you have, any existing Operating Systems and how much free disc space you have.
[/quote]

Sure, i'll do it when i get home!