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
- Next message: Floyd Davidson: "Re: Q: how do I open xterm, run command, and continue manual input?"
- Previous message: Floyd Davidson: "Re: Q: how do I open xterm, run command, and continue manual input?"
- In reply to: SK: "Re: Errors when reading a file in Bourne shell -- HELP"
- Next in thread: SK: "Re: Errors when reading a file in Bourne shell -- HELP"
- Reply: SK: "Re: Errors when reading a file in Bourne shell -- HELP"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Floyd Davidson: "Re: Q: how do I open xterm, run command, and continue manual input?"
- Previous message: Floyd Davidson: "Re: Q: how do I open xterm, run command, and continue manual input?"
- In reply to: SK: "Re: Errors when reading a file in Bourne shell -- HELP"
- Next in thread: SK: "Re: Errors when reading a file in Bourne shell -- HELP"
- Reply: SK: "Re: Errors when reading a file in Bourne shell -- HELP"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|