Another BASH Scripting question

Tim Writer tim-s/rLXaiAEBtBDgjK7y7TUQ at public.gmane.org
Mon Feb 9 19:26:39 UTC 2004


Madison Kelly <linux-5ZoueyuiTZhBDgjK7y7TUQ at public.gmane.org> writes:

> Thank you very much for replying but I can't seem to implement properly your
> advice... With all three methods the output is the same; the variable name
> instead of the variable data...
> 
> 
> Here is the modified script and the output:

[snip]

> echo -n "Checking to see if we have servers: "
> if [ "${SRV_NAME}" != "" ] ; then
>      echo "we do!"
>      echo "Setting up rules for each public server: "
>      for server in ${SRV_NAME} ; do
>          echo "Processing: "${server}
>          eval var=${server}_SNAT_IP
>              echo $var | {

The last two lines should read:

           var=${server}_SNAT_IP
           eval echo \$$var | {

Roughly speaking, when evaluating a command, the shell performs the
following steps:

    1.  Read in the command and break it into words, yielding these three
        strings (for the first command in the pipeline)

            eval
            echo
            \$$var

    2.  Perform substitutions on the words of the command.  The first two
        words have no substitutions, the third has two, namely "\$" and
        "$var" which yield "$" and "SRV1_SNAT_IP" respectively.  After this
        step we have:

            eval
            echo
            $SRV1_SNAT_IP

    3.  Execute the command, in this case "eval" which is a shell builtin.
        From the manpage:

             The args are read and concatenated together into a
             single command.  This command is then read and
             executed by the shell.

        In other words, go to step 1 with the command "echo $SRV1_SNAT_IP"

To see this for yourself, try the following test:

    % cat > /tmp/test.sh
    SRV1_SNAT_IP=1.2.3.4
    server=SRV1
    var=${server}_SNAT_IP
    eval echo \$$var
    ^D
    % bash -vx /tmp/test.sh

-- 
tim writer <tim-s/rLXaiAEBtBDgjK7y7TUQ at public.gmane.org>                                  starnix inc.
905.771.0017 ext. 225                           thornhill, ontario, canada
http://www.starnix.com              professional linux services & products
--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list