Exiting a script

So far, we have seen how a script terminated with the exit status of the last command issued, and how a $? allows us to read the exit value. This is possible because every command returns an exit code, whether issued on the command line or from inside a script, and even functions that we can think of as a compound of commands return a value. Now we are going to see how a script can return an exit code on its own, despite of the result of the last command issued:

#!/bin/bash    
counter=10
while [ $counter -gt 0 ];
do
echo "Loop number: $((counter--))"
done
exit 20

We took one of our previous scripts and added this:

exit 20  

As you can see, we used the command exit followed by a positive number to give an exit code. Remember that you can use any code between:

0-255  

With the exclusion of the reserved values, we saw in the previous chapter. Now, let's run the script:

zarrelli:~$ ./loop-exit.sh ; echo $?
Loop number: 10
Loop number: 9
Loop number: 8
Loop number: 7
Loop number: 6
Loop number: 5
Loop number: 4
Loop number: 3
Loop number: 2
Loop number: 1
20

Here we are. Instead of having a 0 as the script exit value, since the last instruction was successful, we have a 20, provided by the exit command.

Let's modify the script a bit so that the exit will be just above our echo command:

#!/bin/bash    
counter=10
exit_at=5
while (( counter > 0 ));
do
echo "Loop number: $((counter--))"
if (( $counter <exit_at )); then
exit 18
fi
done

Note a couple of things:

  • We are using both $(()) and (()). The first is an arithmetic expansion and gives us a number, the second is a command that gives us an exit status so we can read if it is true (0) that the value of counter is less than value of exit_at.
  • We used a condition to break out of this infinite loop. Once the value of counter is less than the value of exit_at, we exit the whole script with a code of 18, regardless of the fact that the last command, the evaluation of the if condition got a value of 1, so was a failure.

And now, execute the following script:

zarrelli:~$ /loop-premature-exit.sh ; echo $?
Loop number: 10
Loop number: 9
Loop number: 8
Loop number: 7
Loop number: 6
Loop number: 5
18

Here we are. The script exited once it passed the boundary of 5, so the remaining five echo commands were not executed at all and we got 18 as the exit value. So, now you have a handy loop that you can use to iterate over items and stop when a condition is reached.

We said that the exit command prevents the further execution of a script and the previous example gave us a glimpse of it, but let's modify our previous loop script moving the exit 20 command to some lines earlier:

#!/bin/bash    
counter=10
while [ $counter -gt 0 ];
do
exit 20
echo"Loop number: $((counter--))"
done

Now, let's execute it:

zarrelli:$ ./loop-upper-exit.sh ; echo $?
20

Well, no output and the exit value is 20. The script had no time to reach the echo line, it was forced to exit well before. Now, let's see how our exit command masquerades the exit code from a command not found:

#!/bin/bash    
counter=10
while [ $counter -gt 0 ];
do
echo "Loop number: $((counter--))"
fsaapoiwe
done
exit 20

Have a loop at the line under echo, that is, a bunch of characters without any sense, so it will throw an error for sure:

zarrelli:~$ ./loop-error-exit.sh ; echo $?
Loop number: 10
./loop-error-exit.sh: line 8: fsaapoiwe: command not found
Loop number: 9
./loop-error-exit.sh: line 8: fsaapoiwe: command not found
Loop number: 8
./loop-error-exit.sh: line 8: fsaapoiwe: command not found
Loop number: 7
./loop-error-exit.sh: line 8: fsaapoiwe: command not found
Loop number: 6
./loop-error-exit.sh: line 8: fsaapoiwe: command not found
Loop number: 5
./loop-error-exit.sh: line 8: fsaapoiwe: command not found
Loop number: 4
./loop-error-exit.sh: line 8: fsaapoiwe: command not found
Loop number: 3
./loop-error-exit.sh: line 8: fsaapoiwe: command not found
Loop number: 2
./loop-error-exit.sh: line 8: fsaapoiwe: command not found
Loop number: 1
./loop-error-exit.sh: line 8: fsaapoiwe: command not found
20

It did, we had errors at each cycle, but the overall exit code is still 20, since we forced this using the exit command.

What are the benefits we can get from the usage of the exit command? Well, we just saw a nice and easy counter that can be useful to iterate over numbers, items, arrays, lists, but in a broader way, we can use the exit codes to check the result of a function we created, of a command we invoked and based on the value we get, react accordingly. To do this, though, we need to a way to verify and react, to check whether a condition is met or not, and based on that, to do something or something else, we need to have a closer look at the if...else statement and at the tests operators.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.28.197