Solved: BASH code problem - while loop

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

snorkytheweasel

Thread Starter
Joined
May 3, 2006
Messages
215
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: ambiguius redirect

As you can probably tell, I'm quite weak on bash programming.
 
Joined
Apr 23, 2003
Messages
489
Try this....
============================================

#!/bin/bash

src='/home/sites/$sitenum/web'
sitenum=1
limit=8
while [ $sitenum < $limit ];
do
echo 'The location is $src'
let $sitenum = $sitenum + 1
done
 
Joined
Sep 12, 2003
Messages
20,583
Try this instead:

#!/bin/bash
sitenum=1
limit=8
while [ $sitenum -le $limit ]
do
src="/home/sites/$sitenum/web"
echo "The location is $src"
sitenum=`expr $sitenum + 1`
done
exit
 

snorkytheweasel

Thread Starter
Joined
May 3, 2006
Messages
215
I took into account all of the suggested changes.... before writing new code based on a different approach.

The new code worked right out of the box.

I couldn't have done it without you.

Thanks
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Members online

Top