Weird difference in script execution


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Weird difference in script execution
# 1  
Old 09-18-2014
Weird difference in script execution

Hi,

So I have a very simple script which loops over 5 times and prints the iterator value.

Code:
#!/bin/sh
START=1
END=5
for i in $(eval echo "{$START..$END}")
do
        echo "$i"
done

If I save this script in a .sh file and run it in the terminal, the output I get is

Code:
{1..5}

But if I paste the same code in a terminal, I get the following

Code:
1
2
3
4
5

The second one is the expected output, but why is the first case not working as I expect?

I'm totally puzzled. Under cygwin the same code executes okay, but not in a unix shell..Smilie

Thanks.
# 2  
Old 09-18-2014
Make sure you run it with bash or any recent shell instead of sh (in your shebang)...
This User Gave Thanks to RudiC For This Post:
# 3  
Old 09-18-2014
Quote:
Originally Posted by jamie_123
Under cygwin the same code executes okay
Do you mean it works in the terminal as well as in script under cygwin? If yes, then there's a simple explanation for that: In cygwin /bin/sh is actually bash (https://cygwin.com/faq/faq.html#faq.using.shell-scripts).

Quote:
Originally Posted by jamie_123
but not in a unix shell
Well, most likely /bin/sh there is in fact the good ol' bourne shell, which does not support the {$START..$END} bash feature. If you have bash installed there, you could simply change the shebang line from #!/bin/sh to #!/bin/bash and it should work. If not, you'll have to write a more portable code, try:
Code:
#!/bin/sh

START=1
END=5
while [ $START -le $END ]
do
 echo $START
 START=`expr $START + 1`
done

This should work at least with bash, sh and ksh.
This User Gave Thanks to junior-helper For This Post:
# 4  
Old 09-18-2014
On cygwin if you want a good shell to test bourne shell script try ash or dash
This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 09-18-2014
Quote:
Originally Posted by jamie_123
Hi,
If I save this script in a .sh file and run it in the terminal, the output I get is

Code:
{1..5}

What is the exact operating system are you on when this occurs?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Traceroute script weird output

This script is giving weird output #!/bin/bash NETPATH=(`/bin/traceroute -n 4.2.2.2 | awk '{print $2}'`) for i in "${NETPATH}" do echo $i done The output: to 11.11.11.1 1.1.1.1 99.111.208.2 traceroute_test.sh traceroute_test.sh (7 Replies)
Discussion started by: thumbs
7 Replies

2. Shell Programming and Scripting

Execution difference in perl scripts for windows / AIX

Hi, I have perl script abc.pl which runs perfectly fine on windows ( execution from cmd). Now i tried to execute the same perl module on the AIX server after defining the captureoutput.pm and other relevant changes. But its behaving very weirdly as a portion of the URL which is formed by... (3 Replies)
Discussion started by: slayer0611
3 Replies

3. Shell Programming and Scripting

Weird script behaviour !

Hello, I am getting an infinite loop from a script in Linux. Here is the last version of the script in question. As you can see I tried to define everything properly: #!/bin/ksh # Script to loop over a series of dates set -ex typeset -i start_date=20090701 typeset -i... (2 Replies)
Discussion started by: stavros
2 Replies

4. Shell Programming and Scripting

script acting weird..

Hi Guys, I have this script which is being called from another script, sh +x SCRIPTNAME. Now this script is failing saying the source file is missing. But i was able to see the source file was present. It was renamed and but somehow the source file is removed. There is no remove command in the... (1 Reply)
Discussion started by: mac4rfree
1 Replies

5. Shell Programming and Scripting

weird script in crontab

Hello here's the first line's of the code that works perfect on command line but not as a crontab job ??? crontab: 15 * * * * /root/scripts/checkclamd_mem.shscript: #!/bin/bash # Checks Memory of the Clamav-daemon and it's .pid file # restarts if over the LIMIT. Starts if pid file not... (3 Replies)
Discussion started by: nls mchn
3 Replies

6. Shell Programming and Scripting

Weird sed behaviour in script

I've written a small script to replace certain words in all the the files in a directory. #!/bin/sh #Get list of files to be edited file_list=`ls -p` for i in $file_list do echo "Processing $i" alteredi=`echo "$i" | sed -e 's/\//d/'` if then if then #actual altering (2 Replies)
Discussion started by: Peetrus
2 Replies

7. UNIX for Dummies Questions & Answers

Weird Behavior of a Script

ok, there's a script i'm working on written in shell programming. #!/bin/sh this script is written to spit out the contents of certain variables inside of it so the output looks something like this: server01=89 server02=69 server03=89 server04=76 now, when i run this script from the... (4 Replies)
Discussion started by: SkySmart
4 Replies

8. Shell Programming and Scripting

Help with my weird script!

So I have this script titled "testing.sh" #!/bin/ksh #PROGRAM INITIALIZATION HomeDir=/home/sap/gl/ftp server=testftp01 userid=ftp_uatollmsgbus password="f&p53715" MSGLOG=${HomeDir}/msglog.txt FTPLogTmp=${HomeDir}/testing.tmp FTPLogFile=${HomeDir}/testing.log... (1 Reply)
Discussion started by: kdyzsa
1 Replies

9. Shell Programming and Scripting

Weird date difference problem

I am trying to find the difference in days between 2 dates. I have to extract the 1st date from a filename, which i did using the awk command. I have to compare this date to today's date and if the difference is greater than 30 days, do something, else do something else. This is what i wrote... (22 Replies)
Discussion started by: meeraKh
22 Replies

10. UNIX for Dummies Questions & Answers

Weird script

I need a script to do the following and have no idea how to do it...can someone help? I need to start Sql*Plus, load a query, say "unmatched.sql", run the query, then load unmatched.sc and run it, then print the output file that unmatched.sc created... any help greatly appreciated. Duckman (2 Replies)
Discussion started by: Duckman
2 Replies
Login or Register to Ask a Question