LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices



Reply
 
Search this Thread
Old 04-23-2011, 08:39 PM   #1
replica9000
Member
 
Registered: Jul 2006
Location: Quahog, Rhode Island
Distribution: Debian 'Sid', FreeBSD, Android
Posts: 698
Blog Entries: 2

Rep: Reputation: 125Reputation: 125
bash loop not working as expected...


Code:
#!/bin/sh

BASH=$(bash --version | grep -m1 version)

echo "$BASH"
for H in {0..85}
do
echo "Counting Line $H"
done
exit 0
GNU bash, version 4.1.5(1)-release (x86_64-pc-linux-gnu)
Counting Line {0..85}

Isn't this supposed to show a result like this:
Counting Line 1
Counting Line 2
Counting Line 3
Counting Line 4
....
Counting Line 85
 
Old 04-23-2011, 08:44 PM   #2
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Hanover, Germany
Distribution: Main: Gentoo Others: What fits the task
Posts: 15,651
Blog Entries: 2

Rep: Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095
I tested that little script for you, and are getting exactly the output you expected.
 
Old 04-23-2011, 08:47 PM   #3
replica9000
Member
 
Registered: Jul 2006
Location: Quahog, Rhode Island
Distribution: Debian 'Sid', FreeBSD, Android
Posts: 698
Blog Entries: 2

Original Poster
Rep: Reputation: 125Reputation: 125
All I get is
Code:
GNU bash, version 4.1.5(1)-release (x86_64-pc-linux-gnu)
Counting Line {0..85}
 
Old 04-23-2011, 08:52 PM   #4
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Hanover, Germany
Distribution: Main: Gentoo Others: What fits the task
Posts: 15,651
Blog Entries: 2

Rep: Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095
I copy and pasted your script into the file test.sh, made it executable and then started it. This is the output:
Code:
tobi@dragon ~ :) % ./test.sh 
GNU bash, version 4.1.10(2)-release (i486-slackware-linux-gnu)
Counting Line 0
Counting Line 1
Counting Line 2
Counting Line 3
Counting Line 4
Counting Line 5
Counting Line 6
Counting Line 7
Counting Line 8
Counting Line 9
Counting Line 10
Counting Line 11
Counting Line 12
Counting Line 13
Counting Line 14
Counting Line 15
Counting Line 16
Counting Line 17
Counting Line 18
Counting Line 19
Counting Line 20
Counting Line 21
Counting Line 22
Counting Line 23
Counting Line 24
Counting Line 25
Counting Line 26
Counting Line 27
Counting Line 28
Counting Line 29
Counting Line 30
Counting Line 31
Counting Line 32
Counting Line 33
Counting Line 34
Counting Line 35
Counting Line 36
Counting Line 37
Counting Line 38
Counting Line 39
Counting Line 40
Counting Line 41
Counting Line 42
Counting Line 43
Counting Line 44
Counting Line 45
Counting Line 46
Counting Line 47
Counting Line 48
Counting Line 49
Counting Line 50
Counting Line 51
Counting Line 52
Counting Line 53
Counting Line 54
Counting Line 55
Counting Line 56
Counting Line 57
Counting Line 58
Counting Line 59
Counting Line 60
Counting Line 61
Counting Line 62
Counting Line 63
Counting Line 64
Counting Line 65
Counting Line 66
Counting Line 67
Counting Line 68
Counting Line 69
Counting Line 70
Counting Line 71
Counting Line 72
Counting Line 73
Counting Line 74
Counting Line 75
Counting Line 76
Counting Line 77
Counting Line 78
Counting Line 79
Counting Line 80
Counting Line 81
Counting Line 82
Counting Line 83
Counting Line 84
Counting Line 85
tobi@dragon ~ :) %
To make sure that this is not a shell related problem (I am using zsh instead of bash) I also started it with
Code:
bash test.sh
This gave the same output.
 
Old 04-23-2011, 08:56 PM   #5
j1alu
Member
 
Registered: Apr 2009
Distribution: debian gnu/linux
Posts: 798

Rep: Reputation: Disabled
but it does not work with:
#!/bin/sh
when not called with "bash name_of.sh".
replace it with:
#!/usr/bin/env bash
and it should work.

I figured it out by testing, not by knowing. So it might well be wrong.
 
Old 04-23-2011, 09:00 PM   #6
replica9000
Member
 
Registered: Jul 2006
Location: Quahog, Rhode Island
Distribution: Debian 'Sid', FreeBSD, Android
Posts: 698
Blog Entries: 2

Original Poster
Rep: Reputation: 125Reputation: 125
Ok it works if I do
bash ./myscript

doesn't work if I just type
./myscript

Is there something wrong here??
 
Old 04-23-2011, 09:10 PM   #7
replica9000
Member
 
Registered: Jul 2006
Location: Quahog, Rhode Island
Distribution: Debian 'Sid', FreeBSD, Android
Posts: 698
Blog Entries: 2

Original Poster
Rep: Reputation: 125Reputation: 125
Quote:
Originally Posted by j1alu View Post
but it does not work with:
#!/bin/sh
when not called with "bash name_of.sh".
replace it with:
#!/usr/bin/env bash
and it should work.

I figured it out by testing, not by knowing. So it might well be wrong.
This works too. I've only ever started scripts with #!/bin/sh and it's never failed me until now.
 
Old 04-24-2011, 02:09 AM   #8
maine_guy
LQ Newbie
 
Registered: Nov 2010
Posts: 6

Rep: Reputation: 0
You probably are invoking dash instead of bash. Check this with
ls -l `which sh`

If sh is a symbolic link to dash, change it to bash:
cd /bin
sudo rm sh
sudo ln -s bash sh

then try your script again.
 
Old 04-24-2011, 03:03 AM   #9
dudeman41465
Member
 
Registered: Jun 2005
Location: Kentucky
Distribution: Ubuntu
Posts: 794

Rep: Reputation: 56
Change the first line of the script to:
Code:
#!/bin/bash
When I just tried doing ./test.sh I got the same weird output, however when I changed the first line to specify bash, then ran the script, I got the expected output of:
Code:
Counting Line 0
Counting Line 1
Counting Line 2
Counting Line 3
Counting Line 4
........
 
Old 04-24-2011, 03:24 AM   #10
replica9000
Member
 
Registered: Jul 2006
Location: Quahog, Rhode Island
Distribution: Debian 'Sid', FreeBSD, Android
Posts: 698
Blog Entries: 2

Original Poster
Rep: Reputation: 125Reputation: 125
Quote:
Originally Posted by maine_guy View Post
You probably are invoking dash instead of bash. Check this with
ls -l `which sh`

If sh is a symbolic link to dash, change it to bash:
cd /bin
sudo rm sh
sudo ln -s bash sh

then try your script again.
Exactly what was happening, made the switch...

Thanks
 
Old 04-24-2011, 10:08 AM   #11
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714
If this is solved, mark the thread as solved.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 8 04-05-2011 10:18 AM
[SOLVED] [BASH] non-empty variable before loop end, is empty after exiting loop aitor Programming 2 08-26-2010 10:57 AM
Bash for loop not working cryptinitedemon Linux - General 5 04-24-2010 11:44 AM
bash loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 04:50 AM
Bash loop using output of grep not working as needed Jim Pye Programming 7 01-16-2008 11:27 PM


All times are GMT -5. The time now is 07:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration