LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   BASH code problem - a simple while loop (https://www.linuxquestions.org/questions/linux-newbie-8/bash-code-problem-a-simple-while-loop-630866/)

snorkytheweasel 03-26-2008 05:49 PM

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

gilead 03-26-2008 05:57 PM

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.

matthewg42 03-26-2008 06:00 PM

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.

homey 03-26-2008 06:15 PM

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


simplicissimus 03-27-2008 12:25 PM

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

FreeRadical2600 03-27-2008 04:49 PM

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

chrism01 03-27-2008 07:25 PM

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

konsolebox 03-27-2008 09:56 PM

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"


prad77 03-28-2008 05:09 PM

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.