Re: problem with exit while using nohup



Sumir <sumirmehta@xxxxxxxxx> writes:

I am kinda confused with this, am not sure what is happening

i have a script say test.sh
----------
cat myfile | while read line
do
exit 2
done

echo "out of loop"
-----------

as it is evident, the exit should cause the script to terminate ,hence
producing no output for the script.

now when i run the script as "./test.sh", the behaviour is as
expected.
but when i run it with nohup as "nohup ./test.sh &", the "out of loop"
statement appears in the output, indicating that the exit just gets it
outside the loop but does not exit the program.

How odd. None of the shells I've tried have exited the shell from
inside the loop.

What's happening is that the `while' loop is being run in a subprocess;
so `exit' is exiting that subshell. You can verify this by printing
`$?' in the final `echo'. The POSIX spec allows this behaviour (IEEE
1003.1 Shell and Utilities 2.12):

: Additionally, each command of a multi-command pipeline is in a
: subshell environment; as an extension, however, any or all commands in
: a pipeline may be executed in the current environment.

any ideas on this ?

My guess is that you're using different shells in the two cases.

-- [mdw]
.



Relevant Pages

  • 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: Call for input on comp.unix.shell FAQ
    ... > Note that it's GNU specific, and with some GNU date versions, ... > Or have the script produce the environment changes on standard ... I would be a little worried about scripts from other shells updateing ... or use a for loop. ...
    (comp.unix.shell)
  • 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: Why this script is not working?
    ... other shells don't have that kind ... because ksh93 exit status can be greater than 255 and not ... pdksh, MirBSD ksh, and zsh's ksh don't have the problem. ... the invoking script with the exit status specified ...
    (comp.unix.shell)
  • Re: How do I properly use global variables?
    ... I'd like my program below to change $status to zero to exit the loop. ... When script is running,how can you re-run it with another argument to ... When you run it,you can send SIGINT or SIGTERM to let it exit gracefully. ...
    (perl.beginners)