PDA

View Full Version : help regarding for loop syntax in ubuntu 7.10



phanther
February 21st, 2008, 05:53 PM
hi,
i am a novice regarding linux.actually i am working on shell scripting in ubuntu 7.1.i am facing a lot of trouble with the "for loop" statement.
In which ever the format i am giving i am getting the same error as "bad for loop variable".

please help me with the syntax of for loop, if possible please provide me with a valid example which is working in the 7.1 version of ubuntu so that i can try it out.

thanx in advance:)

LaRoza
February 21st, 2008, 05:55 PM
hi,
i am a novice regarding linux.actually i am working on shell scripting in ubuntu 7.1.i am facing a lot of trouble with the "for loop" statement.
In which ever the format i am giving i am getting the same error as "bad for loop variable".

please help me with the syntax of for loop, if possible please provide me with a valid example which is working in the 7.1 version of ubuntu so that i can try it out.

thanx in advance:)

http://www.faqs.org/docs/abs/HTML/loops.html

phanther
February 21st, 2008, 06:29 PM
hey thanx for the reply, i found the syntax i was looking for in the html link u have provided, but i have already used that syntax and i am still facing the same problem.let me clearly describe what is it that i am trying to do.the syntax goes like this:

#!/bin/bash

k=10

for ((i=1; i <= k; i++))
do
echo -n "$i "
done

the error that i am getting is "Bad for loop variable".

thnx again and i'll be looking forward for a reply

darsu
February 21st, 2008, 06:42 PM
Odd. I tried that; it works with bash but not with sh.


See here: http://ubuntuforums.org/archive/index.php/t-442794.html

phanther
February 21st, 2008, 07:28 PM
@darsu

can u tell me how did u run it, n did u give the same amt of spaces as i hav in the above thread.

if so can u pls tell me wat could be the problem. i've checked the shell i'm workin in, its bash n not sh.so i still cant figure out the mistake.infact i am unable to run any script related to "for" loop

darsu
February 21st, 2008, 11:58 PM
I cut and pasted your code into something.sh and executed it with "bash ./something.sh" and "sh ./something.sh".

LaRoza
February 21st, 2008, 11:59 PM
Note that the default shell in Ubuntu is dash, not bash.

http://en.wikipedia.org/wiki/Debian_Almquist_shell

phanther
February 22nd, 2008, 05:19 AM
@darsu & LaRoza

thank you very much, i got the answer from u both.

i made the right choice of asking u darsu about the execution thing.i've been executing it using the second way i.e using "sh".i had a wrong notion while executing it.i got the answers n its clear to me now.

Thanks LaRoza for providing the information regarding the shells and loops.

Now i have another doubt regarding the the above program, it is:

what will this piece of code do: "#!/bin/bash"

ghostdog74
February 22nd, 2008, 05:23 AM
please read here (http://tldp.org/LDP/abs/html/sha-bang.html), and also the whole document.

Zwack
February 22nd, 2008, 06:56 AM
Now i have another doubt regarding the the above program, it is:

what will this piece of code do: "#!/bin/bash"

That depends on where it is in the file...

If the file is executable and that is the very first line (i.e. the first two characters are #!) then it will cause the script when run to use /bin/bash as the interpreter.

Otherwise it will be an interesting comment...

Z.

stormzen
February 29th, 2008, 01:12 AM
Please be aware that "#!/bin/bash" resulted in dash being executed. I guess the meaning of it is the "default shell" is that dash will be run when "#!/bin/bash" is specified. On the other hand, "# !/bin/bash" makes the script execute as expected. ( Note the space between # and ! )

That said, there is a claim that dash is more streamlined (strict POSIX requirement) than bash, so if a script doesn't work with dash, perhaps it makes a lot of sense to fix it? If one were to go that route, is there a good link for syntax?

Thanks!

--J.