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

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


Date: 27 Jul 2003 01:24:26 GMT

On Sun, 27 Jul 2003 at 00:36 GMT, SK wrote:
> 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

    Why are you doing it this way? Why not:

while read SERVER SERVER_NAME TIMEOUT
do

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

    What's $(ENV)? Do you mean ${ENV}? Where is it defined?

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

   You don't need the third grep:

SERVER_STATE=`ps -elf | grep ${ENV} | grep"[S]erver.Name=${SERVER_NAME}"

   Do you need the first one? What does $ENV contain?

> ## 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.

    That's because you are using parentheses, which create a subshell,
    instead of braces (see the example from my previous post).
 
> 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"

    To preserve the value of $EXIT_STATUS, this needs to be inside the
    parantheses or braces (see the example from my previous post).

> # 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]

    This will give you an error; it should be:

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 ?.

    Yes. Read what I wrote:

>> 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}

> I am using a Tru64 machine with OSF1 version 5.1 sp3(patch kit 3).
> Is it a problem with the shell itself?

    Yes. It's the common behaviour. An exception is ksh.

-- 
    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

  • 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: Errors when reading a file in Bourne shell -- HELP
    ... I am trying to exit out of the ... the do while and not the shell itself. ... whole shell script. ... to 1 and checking it after the loop. ...
    (comp.unix.questions)
  • Re: Session problem
    ... it was a leftover from various test i've made before ... Also i forgot to mention that on a server with php5 it was working ... I've put exit after the header. ... otherwhise the execution of the script is not stopped. ...
    (comp.lang.php)
  • Re: Problem signing on to AIM with Net::AOLIM
    ... What do you mean 'post real code'? ... Of course it's not needed but an exit statement at the of a Perl script does no harm and it makes clear to the reader the point at which the main part of the code ends. ... I am able to telnet to the AOL login server port and it responds so it's not a firewall problem. ...
    (comp.lang.perl.misc)
  • Re: exit menu
    ... will exit all the all way or log completely out from the system? ... You can make the menu script into a shell function and call the function from ... Unix Guy Consulting, LLC ...
    (comp.unix.shell)