[SOLVED] Getting a silly error in bash script, but couldn't get rid of this
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Getting a silly error in bash script, but couldn't get rid of this
Hello,
If I run a simple loop, like:
Code:
#!/bin/bash
max=10
for (( num=01; num<=$max; ++num ))
do
echo "$num"
done
Or,
Code:
#!/bin/bash
echo "Enter number: "; read num
max=10
while [ $num -le $max ]
do
echo "$num"
num=$((num+1))
done
But in both conditions, it gives me error (on lines mentioned in red color):
Code:
... syntax error at line 36: `num=$' unexpected
I am quite sure that it's because of bash version. I tried both codes on both RHEL and Solaris. It's working ok on RHEL but not on solaris.
Solaris: GNU bash, version 3.00.16(1)-release (sparc-sun-solaris2.10)
Linux: GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)
So, is there's any alternative way of incrementing variable (i.e. $num) in /bin/bash, so error can be avoided?
I have tried it with bash 2.04 and 3.2.51 and worked (on sparc solaris)
I think there is another error in your script, before this part. You can have a missing pair of " or { or ....
Try to run your script with bash -xv <script>
Or you can show us the whole script.
I have tried it with bash 2.04 and 3.2.51 and worked (on sparc solaris)
I am very sure that my pb is because of older version of bash. I don't know how it worked on your bash ver. 2.04. But I have bash 3.00.16 on Solaris and 3.2.25 in REHL.
The code I shared above is script itself. To avoid any syntax errors, I copied/pasted the same code in both Solaris & Linux. In linux distro it worked fine, but in Solaris it didn't work.
In for loop I get this error on line:
Code:
for (( num=01; num<=$max; ++num ))
In while loop, on line:
Code:
num=$((num+1))
So is there any alternative method to incement the variable, so it can work in older versions of bash as well?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.