0

basically I have written a shell script for a homework assignment that works fine however I am having issues with exiting. Essentially the script reads numbers from the user until it reads a negative number and then does some output. I have the script set to exit and output an error code when it receives anything but a number and that's where the issue is.

The code is as follows:

if test $number -eq $number >dev/null 2>&1
then
   "do stuff"
else
    echo "There was an error"
    exit

The problem is that we have to turn in our programs as text files using script and whenever I try to script my program and test the error cases it exits out of script as well. Is there a better way to do this? The script is being run with the following command in the terminal

script "insert name of program here"

Thanks

tacoofdoomk
  • 53
  • 1
  • 7
  • How do you run your script? – that other guy Nov 30 '15 at 22:47
  • Possible duplicate of http://stackoverflow.com/questions/6112540/return-an-exit-code-without-closing-shell – Jerry Jeremiah Nov 30 '15 at 22:50
  • @JerryJeremiah, given as the OP doesn't seem to know whether they're invoking or sourcing at all, I'm not sure this can be a duplicate until they're informed enough to pick one. :) – Charles Duffy Nov 30 '15 at 22:51
  • Aside: See http://shellcheck.net/ to get the easy-to-catch bugs detected automatically before you ask questions here. – Charles Duffy Nov 30 '15 at 22:52
  • Also, your `script "insert name of program here"` isn't very helpful. Is `script` in this case the external `script` command? Is it the outer test script, where `insert name of program here` is the name of the inner script under test? If so, how is the test script invoking the program under test? – Charles Duffy Nov 30 '15 at 22:57
  • BTW, note that `exit` -- given no arguments -- exits with the status of the most recent command run. Since your `exit` is immediately after an `echo`, that means your script will exit with the same status that the `echo` had, which will almost always be success; probably not what you want for an error condition. – Charles Duffy Nov 30 '15 at 22:58
  • BTW, you realize that without a `fi`, your `if` statement's syntax is invalid? – Charles Duffy Nov 30 '15 at 23:03
  • Sorry about the vague edit, the script was indeed the external script command. The fi was just forgotten but is there in the real code, also thanks for the advice on the exit statement, I changed it to exit with a more a different value – tacoofdoomk Nov 30 '15 at 23:38

1 Answers1

1

If the program you're testing is invoked as a subprocess, then any exit command will only exit the command itself. The fact that you're seeing contrary behavior means you must be invoking it differently.


When invoking your script from the parent testing program, use:

# this runs "yourscript" as its own, external process.
./yourscript

...to invoke it as a subprocess, not

# this is POSIX-compliant syntax to run the commands in "yourscript" in the current shell.
. yourscript

...or...

# this is bash-extended syntax to run the commands in "yourscript" in the current shell.
source yourscript

...as either of the latter will run all the commands -- including exit -- inside your current shell, modifying its state or, in the case of exit, exec or similar, telling it to cease execution.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441