linux退出bash_Linux Bash退出和退出代码_cunjiu9486的博客-程序员秘密

技术标签: python  java  shell  ubuntu  linux  

linux退出bash

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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/cunjiu9486/article/details/109076493

智能推荐

LeetCode-638. 大礼包_边界流浪者的博客-程序员秘密

在 LeetCode 商店中, 有 n 件在售的物品。每件物品都有对应的价格。然而,也有一些大礼包,每个大礼包以优惠的价格捆绑销售一组物品。给你一个整数数组 price 表示物品价格,其中 price[i] 是第 i 件物品的价格。另有一个整数数组 needs 表示购物清单,其中 needs[i] 是需要购买第 i 件物品的数量。还有一个数组 special 表示大礼包,special[i] 的长度为 n + 1 ,其中 special[i][j] 表示第 i 个大礼包中内含第 j 件物品的数量,且

matlab 加入背景,matlab美化图片之添加背景颜色_冉桥兵的博客-程序员秘密

background.pngThe image is generated by myself in matlab.只是在产生的图案中给想要标注的地方添加背景颜色,同时要求背景颜色在线图的下面,代码如下:figureplot(YData)grid onxlabel('pixel position')ylabel('grayscale value')hold onhg1 = line([434,434...

vector,list,deque,set,map of STL_xdx2ct1314的博客-程序员秘密

List封装了链表,Vector封装了数组, list和vector得最主要的区别在于vector使用连续内存存储的,他支持[]运算符,而list是以链表形式实现的,不支持[]。Vector对于随机访问的速度很快,但是对于插入尤其是在头部插入元素速度很慢,在尾部插入速度很快。List对于随机访问速度慢得多,因为可能 要遍历整个链表才能做到,但是对于插入就快的多了,不需要拷贝和移动数据,只需要改

wordpress 最新版本 5.5.3 下载_YIDAY的博客-程序员秘密

本博客正式迁移到独立站点 http://blog.scjia.cc/欢迎前往关注程序员技术日志

将Python脚本打包成可执行文件_python 打jar_DemonHunter211的博客-程序员秘密

Python是一个脚本语言,被解释器解释执行。它的发布方式:.py文件:对于开源项目或者源码没那么重要的,直接提供源码,需要使用者自行安装Python并且安装依赖的各种库。(Python官方的各种安装包就是这样做的).pyc文件:有些公司或个人因为机密或者各种原因,不愿意源码被运行者看到,可以使用pyc文件发布,pyc文件是Python解释器可以识别的二进制码,故发布后也是跨平

随便推点

上海python机构培训_毛毛648python教学的博客-程序员秘密

错误总是在所难免。编写可预见和处理错误的脚本可让您节省大量时间,同时避免很多令人头疼的问题。当工具返回错误消息时,ArcPy 会生成系统错误或异常。在 Python 中,您能够提供多种可用于处理异常的结构和方法。当然,也会由于其他与地理处理工具不相关的原因导致脚本失败。同时需要以适当的方式发现和处理这些问题。以下部分会提供一些方法,这些方法介绍了 Python 异常处理的基础知识。当工具发出错误消息时,ArcPy 会生成arcpy.ExecuteError异常。Python 允许您编写可在生成...

java电商项目源码_电子商务系统+java+web+完整项目+包含源码和数据库Java实用源码(示例代码)..._柳东原的博客-程序员秘密

鸿鹄云商大型企业分布式互联网电子商务平台,推出PC+微信+APP+云服务的云商平台系统,其中包括B2B、B2C、C2C、O2O、新零售、直播电商等子平台。分布式、微服务、云架构电子商务平台 java b2b2c o2o技术解决方案开发语言:java、j2ee数据库:mysqlJDK支持版本:JDK1.6、JDK1.7、JDK1.8版本通用框架:maven+springmvc+mybatis+s...

使用asp动态生成页面,并将页面保存为word_asp保存为word_emeer的博客-程序员秘密

      在做一个论文管理系统的时候碰到要动态生成word表格的问题,多次试用均告失败,几乎放弃,后来一个国外的网站上看到一个相当简单的方法,于是试了下!竟然可以!好东西大家分享!呵呵现将代码公布如下,以备需要者不时之需!     Response.ContentType = "application/msword"    Response.AddHeader "Content-Disp

LIVE555 利用FIFO实现直播_tea1896的博客-程序员秘密

1.LIVE555 直播直播方案采取的是  直播流  -> FIFO -> 输出  的技术路线。2.搭建:a. 在LIVE555 编译之后,在BIN文件下,有很多可执行程序生成,这些程序有些是LIVE555服务器(live555MediaServer),有些是客户端(testRTSPClient),还有大量的流推送服务(testMPEG2TransportStreamer)b.

Qt5--串口应用--多线程读写串口(2)_qt串口线程中读写数据_liefyuan的博客-程序员秘密

 自己写的,淌过很多坑。mainwindow.cpp#include "mainwindow.h"#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this...

分布式任务分发框架Gearman教程和PHP实现实例_cclehui的博客-程序员秘密

1、Gearman介绍和使用场景Gearman是一个分发任务的程序框架,可以用在各种场合,与Hadoop相 比,Gearman更偏向于任务分发功能。它的任务分布非常简单,简单得可以只需要用脚本即可完成。Gearman最初用于LiveJournal的图片 resize功能,由于图片resize需要消耗大量计算资源,因此需要调度到后端多台服务器执行,完成任务之后返回前端再呈现到界面。

推荐文章

热门文章

相关标签