LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 03-26-2008, 05:49 PM   #1
snorkytheweasel
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

#!/bin/sh

src='/home/sites/$sitenum/web'
sitenum = 1
limit = 8
while [ $sitenum < $limit ];
do
echo 'The location is $src'
let $sitenum = $sitenum + 1
done

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
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
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
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Try this...
Code:
#!/bin/bash

n=1
limit=8
while [[ $n < $limit ]]; do
   src="/home/sites/$n/web"
   echo "The location is $src"
   let n=$n+1
done
or this...
Code:
for (( n = 1; n < 8; n++ )); do
   src="/home/sites/$n/web"
   echo "The location is $src"
done
 
Old 03-27-2008, 12:25 PM   #5
simplicissimus
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.

Regards,
SIMP

Fedora User

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

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

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

Third:
You don't need a ';' in bash/ksh/sh unless you are on the same line
 
Old 03-27-2008, 07:25 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
homey's first example is using a string operator to do a numeric comparison: see table B2 here: http://www.tldp.org/LDP/abs/html/refcards.html#AEN20735
 
Old 03-27-2008, 09:56 PM   #8
konsolebox
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:
Quote:
sitenum = 1
limit = 8
should be
Code:
sitenum=1
limit=8
and about
Quote:
while [ $sitenum < $limit ];
that could be one of
Code:
while [ $sitenum -lt $limit ];
while [[ $sitenum < $limit ]];
while [[ sitenum < limit ]];
and
Quote:
let $sitenum = $sitenum + 1
could be one of
Code:
let sitenum=$sitenum+1
let "sitenum = $sitenum + 1"
let "sitenum = sitenum + 1"
 
Old 03-28-2008, 05:09 PM   #9
prad77
Member
 
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...

Gentoo

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


Reply

Tags
bash, loop, while



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

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

All times are GMT -5. The time now is 09:29 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
Open Source Consulting | Domain Registration