linux退出bash_Linux Bash退出和退出代码

linux退出bash_Linux Bash退出和退出代码_第1张图片

linux退出bash

Linux bash shell provide simple but useful programming environment. We can write simple applications, scripts which will run commands, redirect input and outputs and create, stop process. After the execution of the script or function we may need to provide some information to script caller. exit keyword is used to end given script with useful information. In this tutorial we will examine different use cases with exit and send exit status to the program or script caller.

Linux bash shell提供了简单但有用的编程环境。 我们可以编写简单的应用程序,脚本来运行命令,重定向输入和输出以及创建,停止进程。 执行脚本或函数后,我们可能需要向脚本调用者提供一些信息。 exit关键字用于以有用的信息结束给定脚本。 在本教程中,我们将研究具有exit不同用例,并将退出状态发送给程序或脚本调用者。

以最后命令的状态退出 (Exit With Status Of Last Command)

We will start by providing the status of the last command as exit status. We generally run commands in scripts or applications. Every command executed in this scripts or applications will finish execution with a status. The last command exit status can be provided at the end of the script in 3 different ways.

我们将从提供最后一个命令的状态作为退出状态开始。 我们通常在脚本或应用程序中运行命令。 在此脚本或应用程序中执行的每个命令都将以状态结束执行。 可以在脚本末尾以3种不同的方式提供最后一个命令的退出状态。

  • Simply doing nothing will provide the last command exit status as the script exit status. In the following example the ls command exit status will be also the exit status of the script.

    简单地不执行任何操作将提供最后的命令退出状态作为脚本退出状态。 在以下示例中, ls命令的退出状态也将是脚本的退出状态。

#!/bin/bash 

ls /home
  • Or we can explicitly use the exit keyword in order to send the staus of the ls command like below.

    或者我们可以显式地使用exit关键字来发送ls命令的状态,如下所示。

#!/bin/bash 

ls /home 

exit
  • Or the most explicit version is using exit $? which will return the last executed command status with $? by providing exit .

    还是最明确的版本正在使用exit $? 哪个将返回带有$?的最后执行的命令状态$? 通过提供exit

#!/bin/bash 

ls /home 

exit $?

打印最后一个命令的退出状态 (Print Last Command Exit Status)

As we learned in previous part we can use $? in order to provide the last command exit status. This is actually a bash feautre which can be used in different ways apart from scripts. Here we will run mkdir command with a unsuccessful operation to create a folder in /root/test which will give Permission denied error. Then we will print the exit status of this mkdir command $?

如前所述,我们可以使用$? 为了提供最后的命令退出状态。 这实际上是一个bash feautre,除了脚本之外,还可以以其他方式使用。 在这里,我们将使用不成功的操作运行mkdir命令,以在/root/test创建一个文件夹,该文件夹会出现PermissionPermission error错误。 然后,我们将打印此mkdir命令$?的退出状态$?

$ mkdir /root/test 

$ echo $?
Print Last Command Exit Status Print Last Command Exit Status 打印最后一个命令的退出状态

As we can see this will print 1 which means there is an error. We will learn exit status codes in the following parts.

如我们所见,这将打印1 ,表示存在错误。 我们将在以下部分中学习退出状态代码。

LEARN MORE  Linux Bash Pipe Command Usage with Examples For Redirection
了解更多Linux Bash Pipe命令用法以及重定向示例

明确提供退出状态(Explicitly Provide Exit Status)

While exexuting scripts and applications we can provide the exit code explicitly. We just provide the code we want to provide to the exit keyword. Keep in mind that the exit will exit the current running script,application or process. In this example we will provide the exit status as 17 .

在执行脚本和应用程序时,我们可以显式提供退出代码。 我们只提供要提供给exit关键字的代码。 请记住, exit将退出当前正在运行的脚本,应用程序或进程。 在此示例中,我们将退出状态设置为17

#!/bin/bash 

ls /home 

exit 17

成功退出状态 (Success Exit Status)

There are different exit status codes and meanings. But there are some general rules. Success exit status code is  which will accepted by the Linux community. In the following example we will provide successful exit status from given script.

有不同的退出状态代码和含义。 但是有一些通用规则。 成功退出状态代码为这将被Linux社区接受。 在以下示例中,我们将提供给定脚本的成功退出状态。

#!/bin/bash 

ls /home 

exit 0

一般错误退出状态 (General Errors Exit Status)

As stated previously there different type of codes. We can also create our own codes and their meanings. Here is most generic and accepted exit status codes.

如前所述,存在不同类型的代码。 我们还可以创建自己的代码及其含义。 这是最通用和可接受的退出状态代码。

  •  exit code is used to state command or script execution is completed succesfully.

    退出代码用于说明命令或脚本执行成功完成。

  • 1 exit code is used to state that there are general errors like divide by zero etc.

    1退出代码用于声明存在一般错误,例如divide by zero等。

  • 2 exit code is used to state there is a misuse of the shell builtin.

    2退出代码用于说明内置shell的滥用。

We can also create our own exit status code and interpret it. For example we can use 17 as successful completion but there are some minor warnings.

我们还可以创建自己的退出状态代码并对其进行解释。 例如,我们可以使用17作为成功完成,但是有一些次要警告。

exit 17

exit 17

将退出状态设置为变量 (Set Exit Status Into A Variable)

While working with exit status we may need to set the exit status of given script, application or command into a variable. This is very simple case where we will use $? and variable assignment for this.

在处理退出状态时,我们可能需要将给定脚本,应用程序或命令的退出状态设置为变量。 这是非常简单的情况,我们将使用$? 并为此进行变量分配。

$ mkdir /root/test 

$ last_exit_status=$? 

$ echo $last_exit_status
Set Exit Status Into A Variable Set Exit Status Into A Variable 将退出状态设置为变量

As we can see the exit status is saved into variable named last_exit_status and get the exit value 1

如我们所见,退出状态保存到名为last_exit_status变量中,并获得退出值1

翻译自: https://www.poftut.com/linux-bash-exit-and-exit-codes/

linux退出bash

你可能感兴趣的:(python,linux,java,shell,ubuntu)