Re: Errors when reading a file in Bourne shell -- HELP

From: Chris F.A. Johnson (c.f.a.johnson_at_rogers.com)
Date: 07/26/03


Date: 25 Jul 2003 22:36:43 GMT

On Fri, 25 Jul 2003 at 21:40 GMT, SK wrote:
> Hi
>
> I am trying to write a script to check the status of servers in the
> network and i wanted to enter the names into a file for future use. So
> I use the echo command as given below to redirect the values.
>
> echo SERVER SERVER_NAME TIMEOUT_VALUE > $ ${SERVER_INPUT_LISTING}
>
> In the script given below, I try to read back from the file and set
> the status flags according to the values read from the file and
> processing it. So when I used while-do for reading the file, the flags
> that i set inside the loop are not visible outside the loop.
>
> In the script below, the value of $EXIT_STATUS doesnt get displayed
> outside the loop. After I run the script, the first "Here I am" prints
> 1 or 2 but the second "here I am doesnt print anything.
>
> while read server
> do
> set ${server}
> SERVER=$1
> export SERVER
>
> SERVER_NAME=$2
> export SERVER_NAME
>
> TIMEOUT=$3
> export TIMEOUT

while read SERVER SERVER_NAME TIMEOUT
do

> SERVER_STATE="some value"
> if test -n "$SERVER_STATE"

    What are you testing for? You know that $SERVER_STATE contains
    characters; you just put them there.

> then
> ERROR_FLAG=1
> EXIT_STATUS=1
> echo "Exiting ..."
> break
> else
> EXIT_STATUS=2
> fi
>
> echo " here i am: $EXIT_STATUS "
>
> done < ${SERVER_INPUT_LISTING}
> echo " here i am: $EXIT_STATUS"
> exit
>
> Someone please help me clarify this prblem... your help will be very
> much appreciated...

   You are probably using a Bourne shell which runs a loop with
   redirected stdin in a subshell; therefore changes within the loop
   cannot affect the script outside the loop.

   You can get around this by enclosing the whole thing in braces and
   doing the redirection outside them:

{
 while read SERVER SERVER_NAME TIMEOUT
 do
   .......
 done
 echo " here i am: $EXIT_STATUS"
} < ${SERVER_INPUT_LISTING}

-- 
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2003, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License


Relevant Pages

  • Re: Same Internal Server Error from last two days
    ... I am trying to run a Hello World Perl Script in Apache 2.2. ... But its constantly giving me Internal Server Error.The script ... # have to place corresponding `LoadModule' lines at this location so the ...
    (perl.beginners)
  • Re: Same Internal Server Error from last two days
    ... I am trying to run a Hello World Perl Script in Apache 2.2. ... But its constantly giving me Internal Server Error.The script Runs perfectly fine from the command prompt. ... # This is the main Apache HTTP server configuration file. ... LoadModule actions_module modules/mod_actions.so ...
    (perl.beginners)
  • Same Internal Server Error from last two days
    ... I am trying to run a Hello World Perl Script in Apache 2.2. ... But its constantly giving me Internal Server Error.The script ... # have to place corresponding `LoadModule' lines at this location so the ...
    (perl.beginners)
  • Re: Regarding a selection for mobile code/scripting language
    ... Client Side scripting, so the server can send script commands to the client. ... I decided they should be scripted and mobile code. ...
    (Vuln-Dev)
  • Re: Script Time Out Issue
    ... The issue concerns a vbscript running on Windows Server 2k3 SP2. ... The purpose of the script is to read from a SQL Server DB on the same server, process the data and and write it back to other tables in the DB. ... I supect there is a timeout I am missing - I didn't find out about the wscript.timeout until long into the trouble shooting and fully expected that might have been the culprit - but it isn't. ...
    (microsoft.public.windows.server.scripting)