Re: How to exit a shell script



On Mar 30, 5:46 pm, Keith Thompson <ks...@xxxxxxx> wrote:
mathieu <mathieu.malate...@xxxxxxxxx> writes:
  Could someone let me know why calling exit 1 from a shell scrip does
not exit the script ? I have something like

  cat input.txt | while read x; do set -- $x
    if [ "$#" != "2" ]
    then
      echo "Multiple parents: $x";
      exit 1
    fi;
  done
  exit 0

  However the message "Multiple parents" is properly printed but the
script continue to exit without error (0 returned).

I was suspicious of your statement that you have "something like"
what you posted.  It can be difficult or impossible to track down a
problem if all we have is "something like" the script that actually
exhibits the problem.

So here's a script that clearly exhibits the problem, with some extra
trace statements added:

#!/bin/sh

( echo 1 2 ; echo 3 4 5 ) | while read x; do set -- $x
  echo "\$x = $x, \$# = $#"
  if [ "$#" != "2" ]
  then
    echo "Multiple parents: $x";
    echo $$ Before exit 1
    exit 1
    echo $$ After exit 1
  fi;
done
echo $$ Before exit 0
exit 0
echo $$ After exit 0

The output I get is:

$x = 1 2, $# = 2
$x = 3 4 5, $# = 3
Multiple parents: 3 4 5
15380 Before exit 1
15380 Before exit 0

So the "exit 1" does do exit *something* (the statement following it is
not executed), but it doesn't terminate the script, whereas the "exit 0"
does terminate the script.  And both exit statement occur in the same
process, as shown by printing the value of $$.

I get the same output (with different values for $$, of course) sh,
bash, ash, dash, and ksh (on Ubuntu 10.10).  In zsh, the "exit 1" does
terminate the script.

I don't have an answer (and it's interesting that zsh behaves
differently), but maybe this extra information will help someone
else figure it out.

I had to change it to:


cat input.txt \
| while read x; do
set -- $x
if [ "$#" != "2" ]
then
echo "Multiple parents: $x";
exit 1
fi;
done
[ $? -ne 0 ] && exit 1

I never realized that exit was simply exiting a while loop and not
the whole script.

Thanks anyway
.



Relevant Pages

  • Re: how to run a command-line program from Perl/Tk (ExecuteCommand?)
    ... I _am_ detaching the thread, so I cannot join it explicitly, if I am ... Another thing to watch for, is using 'exit' in a thread, will exit all ... threads and terminate the application. ... if ($Script) { ...
    (comp.lang.perl.tk)
  • Re: Bad habits of GOTO-using programmers
    ... The fact that it contains only the word EXIT confirms its function. ...     perform section-a ... The results of the execution of overlapping PERFORM statements are ...     The exit to main is destroyed by the second perform of a-x. ...
    (comp.lang.cobol)
  • Re: Bad habits of GOTO-using programmers
    ... The fact that it contains only the word EXIT confirms its function. ...     perform section-a ... The results of the execution of overlapping PERFORM statements are ...     The exit to main is destroyed by the second perform of a-x. ...
    (comp.lang.cobol)
  • Control a process interactively (pexpect) and save data
    ... The main goal is to record all sequence of commands and responses in order to automatically generate pexpect script. ... The optional input argument should be a string to be sent to the child process, or None, if no data should be sent to the child." ... So it "waits for process to terminate" and I'm unable to interact... ... If I enter 'exit' sh exits, ...
    (comp.lang.python)
  • Re: get or post
    ... the customer user and not the internal or owner user. ...   # THIS IS id INSTEAD OF lease_id B/C ONE DAY WE MIGHT HAVE CUSTOMER ...     exit; ... $query = "SELECT m.lease_num ...
    (comp.lang.php)