Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 03-26-2008, 05:49 PM   #1
LQ Newbie
Registered: Oct 2007
Posts: 5

Rep: Reputation: 0
BASH code problem - a simple while loop

Why doesn't this work? It's just a simple while loop


sitenum = 1
limit = 8
while [ $sitenum < $limit ];
echo 'The location is $src'
let $sitenum = $sitenum + 1

What I expect to see is

The location is /home/sites/1/web
The location is /home/sites/2/web
The location is /home/sites/3/web
The location is /home/sites/4/web
The location is /home/sites/5/web
The location is /home/sites/6/web
The location is /home/sites/7/web

The errors are
[sitenum: command not found
[sitenum: command not found
[sitenum: command not found
[sitenum: command not found
[sitenum: command not found
[sitenum: command not found
sitenum: command not found (Note: that line is different from the ones above it)
limit: command not found
$limit: ambiguous redirect
Old 03-26-2008, 05:57 PM   #2
Senior Member
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,125

Rep: Reputation: 165Reputation: 165
I haven't tested it but the single quotes on the src='/home/sites/$sitenum/web' line will stop $sitenum from being interpolated. I'd use double quotes instead.
Old 03-26-2008, 06:00 PM   #3
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Please use [code] tags when posting code - it makes it a lot more readable.

Some problems with your code:
  • < is not the less-than operator in shell scripts. Try -lt.
  • You have inappropriate whitespace in the let line.
  • The $sitenum on the left hand side of the = in the let line should not have the '$' prefix.
  • Try double quotes in your echo statement. Single quotes do not allow expansion of variables, so you will see $src as a literal string in the output.
  • You don't need a ; at the end of the while line unless you want to put the do on the same line.
  • These are very elementary errors. You should go through a shell scripting tutorial - especially important is to understand quoting rules and where whitespace is needed / not needed.

Last edited by matthewg42; 03-26-2008 at 06:04 PM.
Old 03-26-2008, 06:15 PM   #4
Senior Member
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 59
Try this...

while [[ $n < $limit ]]; do
   echo "The location is $src"
   let n=$n+1
or this...
for (( n = 1; n < 8; n++ )); do
   echo "The location is $src"
Old 03-27-2008, 12:25 PM   #5
Registered User
Registered: Mar 2008
Posts: 104

Rep: Reputation: 15
maybe switch to php

In the original source code $sitename is being used BEFORE it has been declared and given a value. Other errors have been mentioned already.

If you're new to shell scripting, you should test after each new added line to see if it does what you want.

Based on the code it seems you are trying to do certain things like C-style programming. If that is really the case and you have PHP installed on your system, you should try PHP for commandline scripting - it doesn't need a webserver to work.


Fedora User

Last edited by simplicissimus; 04-02-2008 at 05:03 AM.
Old 03-27-2008, 04:49 PM   #6
LQ Newbie
Registered: Mar 2008
Location: Backwoods New York
Distribution: Mandriva One (for now)
Posts: 6

Rep: Reputation: 0
VAR1=/this/is/where/mystuff/is NO Quotes of anykind.

N=1 not N = 1. Shells don't like spaces with integers.

You don't need a ';' in bash/ksh/sh unless you are on the same line
Old 03-27-2008, 07:25 PM   #7
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,406

Rep: Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396
homey's first example is using a string operator to do a numeric comparison: see table B2 here:
Old 03-27-2008, 09:56 PM   #8
Senior Member
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
as what homey has shown, you must not declare variables with spaces:
sitenum = 1
limit = 8
should be
and about
while [ $sitenum < $limit ];
that could be one of
while [ $sitenum -lt $limit ];
while [[ $sitenum < $limit ]];
while [[ sitenum < limit ]];
let $sitenum = $sitenum + 1
could be one of
let sitenum=$sitenum+1
let "sitenum = $sitenum + 1"
let "sitenum = sitenum + 1"
Old 03-28-2008, 05:09 PM   #9
Registered: Mar 2008
Posts: 101

Rep: Reputation: 15
you have use 'eval' to assign variables like this.

eval 'echo The location is $src'

anyway , I am not sure of the quotes . you can play with it though...


Last edited by prad77; 04-17-2008 at 03:30 AM.


bash, loop, while

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
Trouble With FOR Loop In Simple Bash Script sudleyplace Linux - Newbie 7 03-12-2008 04:55 AM
bash while loop problem MadMusician_uk Programming 3 04-20-2006 11:52 AM
Problem with simple shell script for loop abefroman Programming 2 10-25-2005 08:26 PM
Simple while loop problem (newbie question) Seventh Programming 3 09-07-2004 12:00 PM
bash for loop problem deadlock Programming 5 09-04-2003 04:32 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:40 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration