相关疑难解决方法(0)

set -e在bash脚本中的含义是什么?

我正在研究脚本在从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软件包系统安装我的软件包,该脚本还会删除两个目录.

这是对的还是我错过了什么?

linux bash shell sh

617
推荐指数
9
解决办法
34万
查看次数

如果任何命令返回非零值,则中止shell脚本?

我有一个调用许多命令的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)

unix linux bash shell

405
推荐指数
8
解决办法
18万
查看次数

什么是"$?" 准确地给我们一个shell脚本?

我看到代码写在网上的某个地方,我想知道究竟什么是"$?" 做/给我们.谷歌搜索没有帮助.

这是我看到的代码:

#!/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

linux bash shell sh

26
推荐指数
3
解决办法
5万
查看次数

退出bash局部变量赋值中的命令替换代码

如果赋值是函数中的局部变量,如何在bash中检查命令替换的退出代码?
请参阅以下示例.第二个是我想检查退出代码的地方.
有人有一个良好的解决方案或正确的解决方案吗?

$ function testing { test="$(return 1)"; echo $?; }; testing
1
$ function testing { local test="$(return 1)"; echo $?; }; testing
0
Run Code Online (Sandbox Code Playgroud)

bash local-variables exit-code command-substitution

26
推荐指数
1
解决办法
4506
查看次数

NodeJS托管主机与VPS

有一堆基于托管云的托管服务用于nodejs ,这些服务看起来相对较新,有些还处于测试阶段.

托管nodejs应用程序的另一个途径是在像Linode这样的VPS上设置堆栈.

我想知道这两种部署之间的基本区别是什么.在选择一个因素时应该考虑哪些因素?

考虑到这些服务的年轻程度,哪一个更适合生产.

要明确我不是要求选择提供商,而是要决定是在托管节点上托管特定托管还是在老式自我设置VPS上托管.

cloud hosting vps node.js

25
推荐指数
2
解决办法
9672
查看次数

make中的Shell状态代码

我使用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 makefile

18
推荐指数
2
解决办法
3万
查看次数

在shell脚本中运行Python脚本 - 检查状态

在我的shell脚本中,我运行此命令:

python script.py
Run Code Online (Sandbox Code Playgroud)

我想知道,作为一个两部分问题:

  1. 我如何编程我的python脚本将状态传递回运行它的shell脚本,具体取决于python脚本中发生的情况.例如,如果python脚本出现问题,则退出时将代码1发送回shell.

  2. 如何让我的shell脚本读取python的退出代码并退出以获取错误?例如,除了0之外的任何状态代码然后退出.

python bash shell scripting exit-code

8
推荐指数
1
解决办法
8837
查看次数

为什么测试"$?" 一个反模式,看一个命令是否成功?

在这里看到测试是否$?是零(成功)或其他东西(失败)是反模式,但我无法在其他任何地方找到它.

坚持维基百科反模式的定义:"反模式(或反模式)是对反复出现的问题的常见反应,这种反复出现的问题通常是无效的,并且可能会产生极大的反作用." 为什么这会是反模式?

bash shell sh

8
推荐指数
1
解决办法
476
查看次数

exec maven 插件:退出代码

我有以下插件来运行.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 …

bash maven exec-maven-plugin

4
推荐指数
1
解决办法
6662
查看次数

Linux - 捕获ruby脚本的退出代码

我有一个简单的ruby脚本,它使用abort函数退出non-zero exit code

#!/usr/bin/env ruby

puts "I ran"
abort "Exiting"
Run Code Online (Sandbox Code Playgroud)

在bash中执行此命令时如何捕获退出代码?

我试过exit_code=./testexit_code=ruby test无济于事.

谢谢

ruby linux bash

2
推荐指数
1
解决办法
1124
查看次数

在 if 语句中捕获 bash 退出代码

如果我直接使用单个命令对退出代码进行简单测试:

[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)

bash if-statement exit-code

1
推荐指数
1
解决办法
5547
查看次数

View exit code of a program (After the program exited)

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.

linux shell

0
推荐指数
1
解决办法
1998
查看次数