|Help for Alpine - Filters - Exit Status|
|You are in Home > Filters - Exit Status|
In case you need a little reminder on how to use the exit status of programs and scripts this section will remind you how to do so.
In the tcsh shell the way you check the exit status of a program is by executing the program first and then executing the command echo $?. For example take a look at the following examples:
> ls addressbook collections config filters maildrop other scores build commands drivers index.html misc roles > echo $? 0You see that the ls command exit status is 0.
> ls aaaa ls: aaaa: No such file or directory > echo $? 1In this case the exit status was 1.
As you can see from these examples, the exit status depends on what happened during the execution. This is the way that the exit status is always used.
You can use it in a script to determine if the completion of another command was successful or not. For example, the patch program has 3 different exit status: 0 if it applied the patch without problem, 1 and 2 if there was some problem, where 2 is a serious problem (see the manual). This could be used in a script as follows:
#!/bin/sh PATCH="$1" patch -f -p 1 < $PATCH #apply the patch if [ "$?" = "2" ]; then echo "Serious trouble to apply the patch $PATCH" echo "Check the output above" else echo "No serious problems applying the patch" fiIn this script two custom messages are printed depending on how the execution of the patch program ended. If there was a serious problem then a message indicating the name of the patch that failed is printed, otherwise a message indicating that nothing serious happened is printed.
If you want to communicate to another program your exit status, use the code that you want to exit as an argument of the exit command. For example we can rewrite the above script so that it exits with status -1 if there was a serious error and 0 otherwise as follows:
#!/bin/sh PATCH="$1" patch -f -p 1 < $PATCH #apply the patch if [ "$?" = "2" ]; then echo "Serious trouble to apply the patch $PATCH" echo "Check the output above" exit -1 else echo "No serious problems applying the patch" exit 0 fi
Observe that in this way you have complete control over the exit status of a program. In other words, if a program exits with an exit status that you do not want the next program to see, you simply wrap it in a script, like above and make that script exit with the status that you like.
|You are in Home > Filters > Exit Status|