PDA

View Full Version : bash script for-loop problem



ko_ko_now
November 26th, 2009, 12:56 PM
hello ,
i'm facing a little problem here,


after running this:



#!/bin/bash
for i in {1..5}
do
echo "Welcome $i times"
done
I get this:


~$ sh name.sh
Welcome {1..5} times
instead of this:



~$ Welcome 1 times
Welcome 2 times
Welcome 3 times
Welcome 4 times
Welcome 5 times




my bash version is :


~$ /bin/bash --version
GNU bash, version 4.0.33(1)-release (i486-pc-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
and i'm on Ubuntu 9.10


can somebody help me and tell me what i'm doing wrong??

(it's not the first time that stupid problems of this kind happen to me :x)

thanks

mo.reina
November 26th, 2009, 01:06 PM
i think you're supposed to use the seq arguement


for i in $(seq 1 2 20)

ghostdog74
November 26th, 2009, 02:07 PM
it works for me


# bash --version
GNU bash, version 4.0.16(1)-release

# ./shell.sh
Welcome 1 times
Welcome 2 times
Welcome 3 times
Welcome 4 times
Welcome 5 times

Arndt
November 26th, 2009, 02:27 PM
it works for me


# bash --version
GNU bash, version 4.0.16(1)-release

# ./shell.sh
Welcome 1 times
Welcome 2 times
Welcome 3 times
Welcome 4 times
Welcome 5 times



The OP probably ran the script like this:

sh shell.sh

rather than

./shell.sh

John Bean
November 26th, 2009, 02:40 PM
The OP probably ran the script like this:

sh shell.sh

rather than

./shell.sh

No probably about it, that's exactly what he did - it's right there in his post for all to read.

Arndt
November 26th, 2009, 02:50 PM
No probably about it, that's exactly what he did - it's right there in his post for all to read.

I must have assumed the previous answerers took care of such details as reading what was actually written...

ko_ko_now
November 26th, 2009, 02:55 PM
wow!!!! it works,
thanks a lot guys!!!
I'm glad for posting here,
and that you noticed that detail ... by chance i wrote the way i run the script.
:D

(i thought it's the same the "sh command" and the "./")

John Bean
November 26th, 2009, 03:04 PM
(i thought it's the same the "sh command" and the "./")
No, you explicitly used "sh" to run the script in a new shell. Had you used "bash" instead it would have worked.

Not specifying will tell your current shell to use the one it finds at the top of the script, or the default shell (usually bash) otherwise.

John Bean
November 26th, 2009, 03:12 PM
I must have assumed the previous answerers took care of such details as reading what was actually written...
Fair comment, and quite right ;-)

Actually I wasn't intentionally being critical of your message and apologise if it seemed that way. My comment was on the whole thread and carries much the same implications as your reply to it.

Arndt
November 26th, 2009, 05:26 PM
Fair comment, and quite right ;-)

Actually I wasn't intentionally being critical of your message and apologise if it seemed that way. My comment was on the whole thread and carries much the same implications as your reply to it.

No problem, no need to apologise.