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

From: SK (soundars_at_ecs.csus.edu)
Date: 07/27/03


Date: 26 Jul 2003 17:36:13 -0700

Hi,

Thanks a ton for your response... I am trying to exit out of the
script from inside the do while and when I do an exit it exits only
the do while and not the shell itself. I rewote the code like this

(
while read server
do
     set ${server}
     SERVER=$1
     export SERVER
         
     SERVER_NAME=$2
     export SERVER_NAME
          
     TIMEOUT=$3
     export TIMEOUT

## I am checking the processes for a server from the $(ENV)

SERVER_STATE=`ps -elf|grep
${ENV)|grep"Server.Name=${SERVER_NAME}"|grep -v grep`

## If I dont do the ps in a separate variable it gives me an error in
the test statement saying "test arguement expected".
 
    if test -n "$SERVER_STATE"
     then
         ERROR_FLAG=1
         EXIT_STATUS=1
         echo "Server already present..."
         echo "Exiting ..."

## the exit below is only exiting out of the while do but not the
whole shell script. Since it didnt do it, I am setting the EXIT_FLAG
to 1 and checking it after the loop.

         exit
     else
         EXIT_STATUS=2
     fi
# this is just to test my exit_status

      echo " here i am: $EXIT_STATUS "
done
)< ${SERVER_INPUT_LISTING}

echo " here i am: $EXIT_STATUS"

# now I check the status of $EXIT_STATUS from the loop above to find
out whether I need to continue or exit out of the script... but the
values for the $EXIT_STATUS seems to be lost outside the loop... It
shows that it does not have any value... and if I declare it globally
to be 0, it shows 0.

if [$EXIT_STATUS -eq 1]
then
    exit
fi

I think you are right about my shell...
> 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.

Is there anyway to get around this problem of redirected stdin ?. I am
using a Tru64 machine with OSF1 version 5.1 sp3(patch kit 3). Is it a
problem with the shell itself?

Thanks,
- SK

"Chris F.A. Johnson" <c.f.a.johnson@rogers.com> wrote in message news:<bfsbdq$ij2io$2@ID-136730.news.uni-berlin.de>...
> 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.
> >
> > >
> > 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}



Relevant Pages

  • PATCH [1/3]: Replace kernel/timeconst.pl with kernel/timeconst.sh (v2)
    ... The new shell script ... It requires a shell which can do 64 bit math, such as bash, busybox ash, ... makefile so script is responsible for creating output file, ... +trap "rm $FILENAME" EXIT ...
    (Linux-Kernel)
  • Re: trying to understand fork and wait
    ... old habits based on learning to script in REXX on the ... > the child reads it. ... situation for me (drop through to bottom/go back to top of loop). ... just to keep a hold of the exit code. ...
    (comp.lang.perl.misc)
  • Re: Changing directories, script, mc
    ... shell, it doesn't need to be executable. ... $chap1 konsole exits immediately ... appeared in the script, that would be what was doing it - you wouldn't ... would exit. ...
    (comp.os.linux)
  • Re: problem with exit while using nohup
    ... as it is evident, the exit should cause the script to terminate,hence ... outside the loop but does not exit the program. ... My guess is that you're using different shells in the two cases. ...
    (comp.unix.shell)
  • Steve-parkers Bourne sh programming tutorial
    ... At http://steve-parker.org/sh/sh.shtml there is a bourne shell ... programming tutorial by the name of, ... While Loops and a rather small scripty showing the use of a while loop, ... same code into a script called while.sh. ...
    (comp.unix.shell)