LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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

Reply
 
Search this Thread
Old 12-04-2012, 08:43 AM   #1
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 285Reputation: 285Reputation: 285
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?

Last edited by shivaa; 12-04-2012 at 01:08 PM.
 
Old 12-04-2012, 10:55 AM   #2
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,510

Rep: Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221
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.
 
Old 12-04-2012, 01:06 PM   #3
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Original Poster
Rep: Reputation: 285Reputation: 285Reputation: 285
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?

Last edited by shivaa; 12-04-2012 at 01:08 PM.
 
Old 12-04-2012, 01:30 PM   #4
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Original Poster
Rep: Reputation: 285Reputation: 285Reputation: 285
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?

Last edited by shivaa; 12-04-2012 at 09:53 PM.
 
Old 12-04-2012, 05:44 PM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,242

Rep: Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024
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 ...
 
Old 12-04-2012, 10:10 PM   #6
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Original Poster
Rep: Reputation: 285Reputation: 285Reputation: 285
:) Solved

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


Reply

Tags
bash, error, script


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
BASH: More on the "silly" script idea. SilversleevesX Linux - Newbie 1 03-21-2011 02:01 AM
[SOLVED] BASH: Error on "silly" script idea. SilversleevesX Linux - Newbie 2 03-20-2011 11:44 PM
Silly bash script programming.... ArthurHuang Programming 12 02-14-2008 07:56 AM
Bash scripting silly question itz2000 Programming 5 05-09-2007 07:57 PM
silly bash question bennethos Programming 1 03-25-2006 09:46 AM


All times are GMT -5. The time now is 03:56 AM.

Main Menu
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