After a Shell Is No Longer Needed, What Command Can Be Given to Exit the Shell?

Chapter 6. Exit and Drop dead Status

... there are Acheronian corners in the Bourne shell, and hoi polloi employment all of them.

--Chet Ramey

The exit command terminates a script, even as in a C program. It can besides return a value, which is available to the hand's parent process.

All control returns an exit status (sometimes referred to A a return status operating room exit code). A successful command returns a 0, while an empty-handed one returns a non-zero value that usually can exist interpreted as an error code. Well-behaved UNIX commands, programs, and utilities pass a 0 exit cypher upon eminent completion, though there are some exceptions.

Likewise, functions within a book and the script itself return an exit status. The last command executed in the function or script determines the exit status. Within a script, an loss nnn dominate may cost used to deliver an nnn exit status to the shell ( nnn must follow an integer in the 0 - 255 range).

Note

When a script ends with an exit that has no parameter, the exit status of the playscript is the exit status of the shoemaker's last command executed in the script (previous to the exit).

#!/bin/bang  COMMAND_1  . . .  COMMAND_LAST  # Will exit with status of most recently command.  exit

The tantamount of a empty exit is way out $? or even just omitting the expiration.

#!/bin/bash  COMMAND_1  . . .  COMMAND_LAST  # Will exit with status of sunset command.  exit $?

#!/bin/bash  COMMAND1  . . .   COMMAND_LAST  # Will exit with status of last command.

$? reads the exit status of the last bid executed. After a operate returns, $? gives the croak condition of the last command executed in the function. This is Brawl's way of giving functions a "getting even appreciate." [1]

Following the murder of a pipework, a $? gives the kick the bucket status of the last command executed.

After a script terminates, a $? from the bid-line gives the exit position of the script, that is, the last instruction executed in the script, which is, by formula, 0 on succeeder operating theater an integer in the range 1 - 255 on error.

Example 6-1. exit / exit status

#!/bin/bash  echo how-do-you-do echo $?    # Exit status 0 returned because command dead successfully.  lskdf      # Unrecognized command. echo $?    # Not-zilch exit status returned -- command failed to execute.  echo  exit 113   # Will return 113 to shell.            # To verify this, type "repeat $?" afterwards script terminates.  #  By convention, an 'decease 0' indicates success, #+ while a not-nought give-up the ghost apprais way an error Beaver State anomalous condition. #  Project the "Exit Codes With Special Meanings" appendix.

$? is especially useful for testing the answer of a program line in a script (run across Example 16-35 and Example 16-20).

Note

The !, the logical not qualifier, reverses the outcome of a trial run or command, and this affects its exit status.

Example 6-2. Negating a term exploitation !

correct    # The "dead on target" builtin. echo "exit position of \"true\" = $?"     # 0  ! true echo "exit condition of \"! true\" = $?"   # 1 # Note that the "!" needs a space between it and the command. #    !true   leads to a "command not found" error # # The '!' operator prefixing a command invokes the Bash account chemical mechanism.  confessedly !true # No error this time, only no more negation either. # Information technology just repeats the previous command (true).   # =========================================================== # # Preceding a _pipe_ with ! inverts the exit status returned. ls | bogus_command     # whop: bogus_command: dictation non found echo $?                # 127  ! ls | bogus_command   # bash: bogus_command: command not found echo $?                # 0 # Note that the ! does non change the execution of the tube. # Only the exit position changes. # =========================================================== #  # Thanks, St�phane Chazelas and Kristopher Newsome.

Caution

Fated exit status codes have reserved meanings and should not comprise user-mere in a script.

After a Shell Is No Longer Needed, What Command Can Be Given to Exit the Shell?

Source: https://tldp.org/LDP/abs/html/exit-status.html

0 Response to "After a Shell Is No Longer Needed, What Command Can Be Given to Exit the Shell?"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel