- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在尝试将来自 stdout 和 stderr 的所有内容记录到一个日志文件中,同时仍然保留控制台。为此,我只是将:|& tee -a log_file.log
添加到每个命令。
但是,如果脚本期间发生任何错误,我也想运行自定义命令。为此,我在脚本的开头添加了以下内容:trap "echo Non-zero exit code detected"ERR
。
问题是通过使用管道运算符,陷阱中的回显不再执行。
脚本 1,没有管道:
$cat test.sh
#!/bin/bash
trap "echo Non-zero exit code detected!" ERR
function fail_please()
{
echo "Returning non-zero exit code!"
return 1
}
fail_please
输出 1:
$ ./test.sh
Returning non-zero exit code!
Non-zero exit code detected!
脚本 2,带管道:
$ cat test.sh
#!/bin/bash
trap "echo Non-zero exit code detected!" ERR
function fail_please()
{
echo "Returning non-zero exit code!"
return 1
}
fail_please |& tee log_file.log
输出 2:
$ ./test.sh
Returning non-zero exit code!
$ cat log_file.log
Returning non-zero exit code!
在输出 2 中,消息“检测到非零退出代码!”不见了。知道为什么吗?谢谢!
最佳答案
ERR
陷阱针对“简单命令”触发,管道不是简单命令。
它可能会触发整个管道的结果(我不确定),您可以通过设置 pipefail
来获得更接近您想要的结果。
(注意:这是人们通常不推荐使用 set -e
的原因之一,因为它有像这样令人惊讶的细节。)
pipefail
起作用的原因是,通常管道的返回状态是最后一个命令的返回,但是如果有 pipefail
,它就会成为最后一个命令的返回状态那失败了。
The return status of a pipeline is the exit status of the last command, unless the pipefail option is enabled. If pipefail is enabled, the pipeline’s return status is the value of the last (rightmost) command to exit with a non-zero status, or zero if all commands exit success- fully.
关于linux - Bash:使用管道运算符时陷阱 ERR 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27546819/
在编程方面我是个菜鸟。我正在尝试了解 Node.js 和 MongoDB。 下面是我的代码,它将搜索数据库并按州(例如加利福尼亚州、夏威夷州、佛蒙特州等)和最高温度对其进行排序。 当我运行 app.j
我可能在这里遗漏了一些非常简单的东西: package main import ( "fmt" "strconv" "reflect" ) func main() {
我正在尝试运行 npm install bitgo命令但在终端上出现以下错误, npm ERR! code EMFILE npm ERR! syscall spawn git npm ERR! pat
我问了另一个与错误有关的问题。不确定我是否应该编辑该问题。 无论如何,对于下面的代码: app.use((err, req, res, next) => { res.status(err.stat
我正在使用 Promise,并且代码如下所示: function getStuff() { return fetchStuff().then(stuff => process(stuff)
这个问题已经有答案了: Why is `throw` invalid in an ES6 arrow function? (3 个回答) 已关闭 6 年前。 以下代码可以正常工作: pool.get
当我尝试向 redis 添加模块时,出现错误:(err) ERR 不支持的 CONFIG 参数:module-add 我已经通过以下方式安装了动态 redis :- $mkdir -p ~/repos
当我重新分片redis集群时,我遇到了一个问题。 redis集群信息和问题详情如下: obasa04:/usr/redis-4.0.2/src # redis-trib.rb info 10.239.
有什么理由在nodejs中写next(new Error(err))而不是next(err)吗? 非常感谢! 最佳答案 在没有任何上下文的情况下,我假设err已经是一个对象(通常在节点中),因此没有理
我正在尝试创建一个从 API 获取数据的全栈 Node 和 Vue 应用程序。我遇到了一个问题,我试图同时运行客户端和服务器,但代码遇到错误。如果我错误地构建了这个问题,请耐心等待,因为我对编码还很陌
我要更新 npm 最新版本,但显示此错误消息: G:\>npm i -g npm ERR! code ENOLOCAL npm ERR! Could not install from "" as it
为什么在执行 sudo npm install npm 时会出现这个奇怪的错误?我有时在尝试安装其他模块时也会得到它! 错误: > scrypt@6.0.3 preinstall /home/user
我使用 ctypes 围绕 libbgpdump 编写了一个 python 包装器,但是当 libbgpdump 调用“err()”时它会出现段错误。这是失败的 C 代码: if(count > MA
我想运行这个项目:https://github.com/adonis-china/adonis-adminify 当我运行npm install时,存在错误: > sqlite3@3.1.13 ins
我尝试在我的服务器上运行 npm install 并收到此错误。 > node-cron@2.0.3 postinstall /home/workspace/AgreementCancellation
我正在尝试在我的 Node 项目上创建一个错误处理程序,但我不明白一些事情。 对于抛出的错误,我可以从 2 种不同的方式中捕获: 进程.on() process.on('uncaughtExcepti
当我在我的“npm cache clean”中写入时,出现了这个错误“npm ERR!Windows_NT 6.3.9600 npm ERR!argv” C:\iaAC>npm cache clean
在我的 npm run 构建脚本执行后,我很难深入了解我收到的 npm 错误。终端报错如下: npm ERR! code ELIFECYCLE npm ERR! errno 2 npm ERR! vi
我试图在一个新的项目目录中运行 yo angular,但它在途中的某个地方给了我 ENOENT 错误。是的,我看过 this similar question ,但它的解决方案对我不起作用。 在运行
试图在 Ubuntu 18.04 x64 上运行它。它曾经在一天前正常工作,但它突然停止了。 npm i simple-youtube-api 我曾经能够安装它们但不是任何人,我得到这个: > buf
我是一名优秀的程序员,十分优秀!