1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: BASH code problem - while loop

Discussion in 'Linux and Unix' started by snorkytheweasel, Mar 26, 2008.

Thread Status:
Not open for further replies.
Advertisement
  1. snorkytheweasel

    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.
     
  2. bkdc

    bkdc

    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
     
  3. lotuseclat79

    lotuseclat79

    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
     
  4. snorkytheweasel

    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
     
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 733,556 other people just like you!

Loading...
Similar Threads - Solved BASH code
  1. binny18
    Replies:
    10
    Views:
    420
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/697396

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice