LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Getting a silly error in bash script, but couldn't get rid of this (http://www.linuxquestions.org/questions/linux-newbie-8/getting-a-silly-error-in-bash-script-but-couldn%27t-get-rid-of-this-4175439998/)

shivaa 12-04-2012 08:43 AM

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?

pan64 12-04-2012 10:55 AM

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.

shivaa 12-04-2012 01:06 PM

Quote:

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?

shivaa 12-04-2012 01:30 PM

;) OK, I solved it for while loop. It was a problem with defining variable in old version of bash. So I tried:
Code:

#!/bin/bash
echo "Enter number: "; read num
max=10
while [ $num -le $max ]
do
echo "$num"
num=`expr $num + 1`
done

But it couldn't be solved for for loop, even after making following changes:
Code:

#!/bin/bash
max=10
num=1
for [ $num -le $max ]
do
echo "$num"
num=`expr $num + 1`
done

But getting:
Code:

... Syntax error near line 4: `$num' unexpected
So how to rectify the for loop error?

chrism01 12-04-2012 05:44 PM

See section 11-12 here http://www.tldp.org/LDP/abs/html/loops1.html
'for' doesn't take square brackets.

incidentally, in the above, the expr lines are different ...

shivaa 12-04-2012 10:10 PM

:) Solved
 
Thanks Chris. Clearly options are limited with an older version of bash.


All times are GMT -5. The time now is 03:19 PM.