LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 05-02-2010, 06:34 AM   #1
lipun4u
Member
 
Registered: Sep 2008
Location: Mumbai, india
Distribution: ubuntu and hp-unix
Posts: 118

Rep: Reputation: 15
for loop error


Why the following code shows error ??


Code:
#!/bin/bash

factorial=1
for (( number = 1; number <= $1; number++ ))
do
        factorial=$[ $number * $factorial ]
done

echo "The factorial of $1 : $factorial"
The error
Quote:
./test1.sh: line 4: ((: number <= : syntax error: operand expected (error token is "<= ")
The factorial of : 1
 
Old 05-02-2010, 06:40 AM   #2
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 249Reputation: 249Reputation: 249
Where is $1 coming from ?

You can't say something is less than or equal to something else, unless the something else (an operand) exists.

That's apart from the bad assignment in the loop condition.
number=1

Last edited by smoker; 05-02-2010 at 06:44 AM.
 
0 members found this post helpful.
Old 05-02-2010, 07:16 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
FYI $[ <arithmetic expression> ] does work but is no longer documented in the man page; the current method is $(( <arithmetic expression> )).
 
1 members found this post helpful.
Old 05-02-2010, 03:32 PM   #4
lipun4u
Member
 
Registered: Sep 2008
Location: Mumbai, india
Distribution: ubuntu and hp-unix
Posts: 118

Original Poster
Rep: Reputation: 15
In my book, they say it as advanced for loop. Would please give a better link on this ?
 
Old 05-02-2010, 04:09 PM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by lipun4u View Post
In my book, they say it as advanced for loop. Would please give a better link on this ?
for (( )) is the advanced for loop. $(( <arithmetic expresssion> )) is arithmetic expansion (the value of the arithmetic expansion is substituted) and (( <arithmetic expresssion> )) is a conditional construct equivalent to let <arithmetic expresssion>.
 
Old 05-02-2010, 04:59 PM   #6
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 249Reputation: 249Reputation: 249
The code as you quoted it works IF you supply a number to it.
I repeat - where are you getting $1 from ?

You have to supply that number or there is nothing to compare in the for loop condition.

try running
Code:
test1.sh 15
 
  


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
for loop error.... lipun4u Linux - Newbie 4 02-16-2010 06:03 AM
Error about loop device... Kindly help.. ashishshukla Linux - Server 1 05-30-2009 06:16 PM
segmentation error while using mpz_powm in a loop shrims4u Linux - Software 0 05-07-2009 12:23 PM
error: ‘for’ loop initial declaration nasim751 Programming 3 06-07-2008 11:31 AM
Nested-double loop error Harry Seldon Programming 3 05-06-2006 06:15 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:10 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration