123

I'm doing some practice exercises.

Write a script that will be given a month number as the argument and will translate this number into a month name. The result will be printed to stdout.

I made a solution:

# Test for number of argument

if [ "$#" -eq 0 ] ; then echo -e "No argument." echo -e "Write a number between 1 and 12." exit 1 elif [ "$#" -gt 1 ] ; then echo -e "More than 1 argument." echo -e "Write a number between 1 and 12." exit 1 else numb=$1 case "$numb" in 1) echo "Month: January";; 2) echo "Month: February";; 3) echo "Month: March";; 4) echo "Month: April";; 5) echo "Month: May";; 6) echo "Month: June";; 7) echo "Month: July";; 8) echo "Month: August";; 9) echo "Month: September";; 10) echo "Month: October";; 11) echo "Month: November";; 12) echo "Month: December";; *) echo -e "You wrote a wrong number. Try again with writing number between 1 and 12.";; esac fi exit 2 exit 0

What do exit 1, exit 0 and exit 2 mean, and why do we use them?

Pablo Bianchi
  • 15,657
  • 5
    A successfully executed code should exit with code 0. Other values indicate an error. See for instance Exit Codes With Special Meanings – V. Olsen Mar 13 '17 at 15:21
  • 2
    A bash script is like a movie theater, there are different exists. The exit statements mark the location of the exits for the bash interpreter. When the script is fired, the interpreter is required to run until the nearest exit. – DepressedDaniel Mar 13 '17 at 23:51
  • 5
    If this is the code you have written, how come you have used statements you don't even understand? – Dmitry Grigoryev Mar 14 '17 at 11:09
  • 6
  • You accepted an answer right after posting the question. Give it 24 hours to wait for even better answers. 2) The accepted answer is wrong and misleading. The correct answer is "there is no default meaning for the exit codes (beyond 0 = success); you as script developer define the semantics". 3) If you made that script, why did you add the exit commands (all of which are logically superfluous or could collapse to "exit 1" since you use if-else anyway).
  • – AnoE Mar 14 '17 at 13:30