我正在研究脚本在从Debian存档(.deb)文件解压缩该包之前执行的这个preinst文件的内容.
该脚本具有以下代码:
#!/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
Run Code Online (Sandbox Code Playgroud)
我的第一个问题是关于这一行:
set -e
Run Code Online (Sandbox Code Playgroud)
我认为脚本的其余部分非常简单:它检查Debian/Ubuntu包管理器是否正在执行安装操作.如果是,它会检查我的应用程序是否刚刚安装在系统上.如果有,脚本将打印消息"MyApplicationName刚刚安装"并结束(return 1意味着以"错误"结束,不是吗?).
如果用户要求Debian/Ubuntu软件包系统安装我的软件包,该脚本还会删除两个目录.
这是对的还是我错过了什么?
我有一个调用许多命令的Bash shell脚本.如果任何命令返回非零值,我希望shell脚本自动退出,返回值为1.
如果没有明确检查每个命令的结果,这可能吗?
例如
dosomething1
if [[ $? -ne 0 ]]; then
exit 1
fi
dosomething2
if [[ $? -ne 0 ]]; then
exit 1
fi
Run Code Online (Sandbox Code Playgroud) 我看到代码写在网上的某个地方,我想知道究竟什么是"$?" 做/给我们.谷歌搜索没有帮助.
这是我看到的代码:
#!/bin/sh
ping -c 2 localhost
if [ $? != 0 ] ; then
echo "Couldn't ping localhost, weird"
fi
ping -c 2 veryweirdhostname.noend
if [ $? != 0 ] ; then
echo "Surprise, Couldn't ping a very weird hostname.."
fi
echo "The pid of this process is $$"
Run Code Online (Sandbox Code Playgroud)
取自:http://efod.se/writings/linuxbook/html/shell-scripts.html
如果赋值是函数中的局部变量,如何在bash中检查命令替换的退出代码?
请参阅以下示例.第二个是我想检查退出代码的地方.
有人有一个良好的解决方案或正确的解决方案吗?
$ function testing { test="$(return 1)"; echo $?; }; testing
1
$ function testing { local test="$(return 1)"; echo $?; }; testing
0
Run Code Online (Sandbox Code Playgroud) 有一堆基于托管云的托管服务用于nodejs ,这些服务看起来相对较新,有些还处于测试阶段.
托管nodejs应用程序的另一个途径是在像Linode这样的VPS上设置堆栈.
我想知道这两种部署之间的基本区别是什么.在选择一个因素时应该考虑哪些因素?
考虑到这些服务的年轻程度,哪一个更适合生产.
要明确我不是要求选择提供商,而是要决定是在托管节点上托管特定托管还是在老式自我设置VPS上托管.
我使用Makefile(在Linux下运行GNU make)来重构Python脚本时自动完成我的工作.该脚本创建了一个输出文件,我想确保输出文件在我的重构中保持不变.
但是,我发现无法获取命令的状态代码以影响后续shell if命令.
以下规则说明了问题:
check-cond-codes:
diff report2008_4.csv report2008_4.csv-save-for-regression-testing; echo no differences: =$$!=
diff -q poalim report2008_4.csv; echo differences: =$$!=
Run Code Online (Sandbox Code Playgroud)
第一个'diff'比较两个相同的文件,第二个比较两个不同的文件.输出是:
diff report2008_4.csv report2008_4.csv-save-for-regression-testing; echo no differences: =$!=
no differences: ==
diff -q poalim report2008_4.csv; echo differences: =$!=
Files poalim and report2008_4.csv differ
differences: ==
Run Code Online (Sandbox Code Playgroud)
显然'$$!' 是捕获'diff'状态代码的错误变量.即使在Makefile开头使用SHELL:=/bin/bash也无法解决问题.
返回我需要的值的变量(如果它存在的话)将在真实规则中的'if'命令中使用.
创建一个小的ad-hoc shell脚本代替在Makefile中内联编写所有命令的替代方案是不可取的,但我会将它作为最后的手段使用它.
有关:
在我的shell脚本中,我运行此命令:
python script.py
Run Code Online (Sandbox Code Playgroud)
我想知道,作为一个两部分问题:
我如何编程我的python脚本将状态传递回运行它的shell脚本,具体取决于python脚本中发生的情况.例如,如果python脚本出现问题,则退出时将代码1发送回shell.
如何让我的shell脚本读取python的退出代码并退出以获取错误?例如,除了0之外的任何状态代码然后退出.
我有以下插件来运行.sh脚本:
<plugin>
<artifactId>exec-maven-plugin</artifactId>
<groupId>org.codehaus.mojo</groupId>
<executions>
<execution>
<id>deploy-bundles</id>
<phase>install</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>${basedir}/deploy.sh</executable>
<successCodes>
<successCode>0</successCode> <-- Not working
</successCodes>
</configuration>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
它将一些文件夹和文件复制到某些位置。有用。但是,为了以防万一,我希望有一个失败错误机制。set -e我的.sh脚本中已经有了命令,但我也想要一个 Maven 解决方案。我听说有一个名为的标签successCodes,我尝试合并它。但到目前为止没有运气。有人能指出正确的做法吗?
编辑:我的.sh脚本如下所示:
cp ../template/config.properties $component/conf
cp ../../runtime/group1/group1.mw/conf/log4j.xml $component/conf
# if the component is planning, create an additional folder called plans
if [[ $component == *".planning"* ]]
then
mkdir -p $component/plans
# copy all the plans here
cp ../../mission.planning/plans/* $component/plans
fi
Run Code Online (Sandbox Code Playgroud)
如果这些文件夹/文件不存在,预计会失败。因此,作为测试,我手动更改了上面的路径并期望它失败。它确实使执行过程失败并告诉我错误(因为我set …
我有一个简单的ruby脚本,它使用abort函数退出non-zero exit code
#!/usr/bin/env ruby
puts "I ran"
abort "Exiting"
Run Code Online (Sandbox Code Playgroud)
在bash中执行此命令时如何捕获退出代码?
我试过exit_code=./test或exit_code=ruby test无济于事.
谢谢
如果我直接使用单个命令对退出代码进行简单测试:
[user@localhost ~]$ date
Tue Sep 8 14:00:11 CEST 2020
[user@localhost ~]$ echo $?
0
[user@localhost ~]$ dat
bash: dat: command not found...
[user@localhost ~]$ echo $?
127
Run Code Online (Sandbox Code Playgroud)
但是,如果我想将它们放入 if 语句中,我发现对于退出代码 0,输出正常,但是,对于“命令未找到”代码(应该是 127),它返回“1”。
[user@localhost ~]$ date
Tue Sep 8 14:02:18 CEST 2020
[user@localhost ~]$ if [ $? -eq 0 ]; then echo "Success: $?"; else echo "Failure: $?" >&2; fi
Success: 0
[user@localhost ~]$ dat
bash: dat: command not found...
[user@localhost ~]$ if [ $? -eq 0 ]; then echo …Run Code Online (Sandbox Code Playgroud) Lets say a program that outputs a zero in case of success, or 1 in case of failure, like this:
main () {
if (task_success())
return 0;
else
return 1;
}
Run Code Online (Sandbox Code Playgroud)
Similar with Python, if you execute exit(0) or exit(1) to indicate the result of running a script. How do you know what the program outputs when you run it in shell. I tried this:
./myprog 2> out
Run Code Online (Sandbox Code Playgroud)
but I do not get the result in the file.