Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.