Raspberry Pi_Eng_10.7.4 “exit” Command – Terminate Process


Published Book on Amazon


All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 1
All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 2


출판된 한글판 도서


최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (상)
최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (하)


Original Book Contents


10.7.4  "exit" Command Terminate Process

 

The "exit" command is used to terminate process while a process is in progress.

 

[Command Format]

exit  [exit-status]

 

[Command Overview]

   This terminates the running process and set the exit status to the specified value.

   User privilege          -- Normal user.

 

[Detail Description]

When you run this command, the current process immediately stops processing. If there is a parent process that calls the child process, it continues processing on the next line to the statement that calls the child process in the parent process.

The "exit" command can return the processing status by specifying the status-number. If status-number is not specified, the most recent Shell command or the status-number specified in the script is used.

In Linux, when the execution of a command or script is completed, various exit-status numbers are returned to indicate whether the processing was successful depending on the processing status. The reason why the exit-status is returned is to judge in the next step work whether the previous step work has been completed normally, and to adjust the work appropriately according to the judgment result. 

 

The basic principle for exit-status is as follows:

    If exit-status is 0, it means that it was processed normally.

    If exit-status is non-zero, it means that it has not been processed normally, and the specific value depends on the script or command.

 

The basic Shell exit code is:

    0 : Successful completion of the command

    1 : General unknown error

    2 : Misuse of Shell command

    126 : The command can't execute

    127 : Command not found

    128 : Invalid exit argument

    128+x : Fatal error with Linux signal x

    130 : Command terminated with Ctrl-C

    255 : Exit status out of range

 

You can see the contents of the exit-status returned from the previous run by using "$?" variable. The contents can be displayed on the screen by command "Echo $?". If you use this value in a script, you can make various adjustments depending on the state.

 

[Used Example]

In the following, we will look at how exit-status is displayed when executing various Shell commands. The following example shows a case where the command is processed normally. You can see that exit-status is set to "0".

 

pi@raspberrypi ~/Script $ ls -l

total 28

-rwxr-xr-x 1 pi pi 261 Apr 30 15:48 test_case.sh

-rwxr-xr-x 1 pi pi 133 Apr 30 17:29 test_for.sh

-rwxr-xr-x 1 pi pi  94 Apr 30 17:21 test_if.sh

-rwxr-xr-x 1 pi pi 158 Apr 30 16:55 test_until.sh

-rwxr-xr-x 1 pi pi 158 Apr 30 16:47 test_while.sh

pi@raspberrypi ~/Script $ echo $?

0

 

The following command has an invalid directory. You can see that exit-status is set to "2".

 

pi@raspberrypi ~/Script $ ls /dddd

ls: cannot access /dddd: No such file or directory

pi@raspberrypi ~/Script $ echo $?

2

 

The following command has an error caused by a command that does not exist. The exit-status is set to "127".

 

pi@raspberrypi ~/Script $ sadf

-bash: sadf: command not found

pi@raspberrypi ~/Script $ echo $?

127

 

In the following example, we will show the example that we check the results of the previous processing in the script and adjust the processing accordingly. Create the following script and save it in "test_while.sh" file to prepare it for execution. This script intentionally assumes that you are returning exit-status "3" on the "exit" statement.

 

#! /bin/bash

number =0

while [ number -le 4 ] ;

   do

           echo "number" $number

           number=$(( $number + 1 ))

   done

echo "All is completed"

exit 3

 

Here is a script that runs the above script inside the script, checks the exit-status, and optionally adjusts the processing accordingly.

 

#! /bin/bash

 

~/Scripts/test_while.sh

 

return_code=$?

 

if  ( $ return_code = 0 ); then

          echo "while statement is OK" $return_code

else

           echo "while statement is ERROR" $return_code

fi

 

Processing this command produces the following results. As we expected in advance, exit-status "3" was set and the message "ERROR" was displayed.

 

pi@raspberrypi ~/Script $ ./test_exit.sh

number 0

number 1

number 2

number 3

number 4

All is completed

While statement is ERROR 3