我正在研究这个preinst文件的内容,该脚本在包从Debian归档文件(.deb)文件解压缩之前执行该文件。
脚本代码如下:
#!/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
我的第一个问题是关于这一行的:
set -e
我认为脚本的其余部分非常简单:它检查Debian/Ubuntu包管理器是否正在执行安装操作。如果是,它将检查我的应用程序是否刚刚安装到系统上。如果有,脚本打印消息“MyApplicationName刚刚安装”并结束(返回1意味着以“错误”结束,不是吗?)
如果用户要求Debian/Ubuntu包系统安装我的包,脚本还会删除两个目录。
是这样吗,还是我漏掉了什么?
set -e该选项指示bash在任何命令[1]的退出状态为非零时立即退出。您可能不想为命令行shell设置这个,但在脚本中它非常有用。在所有广泛使用的通用编程语言中,一个未处理的运行时错误——无论是Java中的抛出异常,还是C中的分段错误,或者Python中的语法错误——都会立即停止程序的执行;后续行不执行。
By default, bash does not do this. This default behavior is exactly what you want if you are using bash on the command line
you don't want a typo to log you out! But in a script, you really want the opposite.
If one line in a script fails, but the last line succeeds, the whole script has a successful exit code. That makes it very easy to miss the error.
Again, what you want when using bash as your command-line shell and using it in scripts are at odds here. Being intolerant of errors is a lot better in scripts, and that's what set -e gives you.
复制自:https://gist.github.com/mohanpedala/1e2ff5661761d3abd0385e8223e16425
这可能对你有帮助。