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 |
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:
|
Try this...
Code:
#!/bin/bash Code:
for (( n = 1; n < 8; n++ )); do |
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 |
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 |
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
|
as what homey has shown, you must not declare variables with spaces:
Quote:
Code:
sitenum=1 Quote:
Code:
while [ $sitenum -lt $limit ]; Quote:
Code:
let sitenum=$sitenum+1 |
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 |
All times are GMT -5. The time now is 09:07 AM. |