set -e 在 bash 脚本中是什么意思? [英] What does set -e mean in a bash script?

查看:34
本文介绍了set -e 在 bash 脚本中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究这个 preinst 文件的内容,脚本在从其 Debian 存档 (.deb) 文件中解压该软件包之前执行该文件.

I'm studying the content of this preinst file that the script executes before that package is unpacked from its Debian archive (.deb) file.

脚本代码如下:

#!/bin/bash
set -e
# Automatically added by dh_installinit
if [ "$1" = install ]; then
   if [ -d /usr/share/MyApplicationName ]; then
     echo "MyApplicationName is just installed"
     return 1
   fi
   rm -Rf $HOME/.config/nautilus-actions/nautilus-actions.conf
   rm -Rf $HOME/.local/share/file-manager/actions/*
fi
# End automatically added section

我的第一个查询是关于线路的:

My first query is about the line:

set -e

我认为脚本的其余部分非常简单:它检查 Debian/Ubuntu 包管理器是否正在执行安装操作.如果是,它会检查我的应用程序是否刚刚安装在系统上.如果有,脚本会打印消息 "MyApplicationName is just installed" 并结束(return 1 意味着以错误"结束,不是吗?).

I think that the rest of the script is pretty simple: It checks whether the Debian/Ubuntu package manager is executing an install operation. If it is, it checks whether my application has just been installed on the system. If it has, the script prints the message "MyApplicationName is just installed" and ends (return 1 mean that ends with an "error", doesn’t it?).

如果用户要求 Debian/Ubuntu 软件包系统安装我的软件包,脚本也会删除两个目录.

If the user is asking the Debian/Ubuntu package system to install my package, the script also deletes two directories.

这是正确的还是我遗漏了什么?

Is this right or am I missing something?

推荐答案

来自 help set :

  -e  Exit immediately if a command exits with a non-zero status.

但有些人认为这是不好的做法(bash FAQ 和 irc freenode #bash FAQ 作者).推荐使用:

But it's considered bad practice by some (bash FAQ and irc freenode #bash FAQ authors). It's recommended to use:

trap 'do_something' ERR

在发生错误时运行do_something函数.

to run do_something function when errors occur.

参见 http://mywiki.wooledge.org/BashFAQ/105

这篇关于set -e 在 bash 脚本中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆