Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查bash中的rcode和管道_Bash_Pipe - Fatal编程技术网

检查bash中的rcode和管道

检查bash中的rcode和管道,bash,pipe,Bash,Pipe,我想这样做: ./first.sh if [ $? -ne 0 ]; then exit fi # pipe output of first.sh to second.sh 因此,除非first.sh成功,否则second.sh不应该运行,但我真的不知道在同一时间将first.sh的输出传输到哪里。如果first.sh成功,为什么不将其转储到文件中,然后将其写入second.sh ./first.sh > /tmp/first.pid if [ $? -ne 0 ]; then

我想这样做:

./first.sh
if [ $? -ne 0 ]; then
exit
fi
# pipe output of first.sh to second.sh

因此,除非
first.sh
成功,否则
second.sh
不应该运行,但我真的不知道在同一时间将
first.sh
的输出传输到哪里。

如果
first.sh
成功,为什么不将其转储到文件中,然后将其写入
second.sh

./first.sh > /tmp/first.pid 
if [ $? -ne 0 ]; then
   exit
fi   
./second.sh < /tmp/first.pid
/first.sh>/tmp/first.pid
如果[$?-ne 0];然后
出口
fi
/second.sh

如果并发性是一个问题,您可能希望使用pid或类似工具来定义文件的范围。

您可以设置
bash
选项
pipefail
以导致错误,并在管道中的命令失败时报告退出:

set -e # Set exit on error
set -o pipefail # Set exit on error to work with pipes

.

磁盘空间使用、并发性问题等。虽然这样做有效,但我想这里有一个更好的做法。我并不这么认为。这很标准。您可以通过执行tmpstr=“$(./first.sh)”然后执行echo“$tmpstr”| second.sh将输出存储在变量中,但如果不小心,有时可能会导致转义问题。您几乎必须执行其中一项操作,因为使用管道的任何变体都会在使用first.sh finishesPipes之前开始将输入发送到second.sh,以减少处理时间,方法是允许process2在创建process1时对其输出进行微调。如果您必须确保在启动p2之前,所有process1都是干净的,那么您必须花费额外的时间(通过不使用管道)来写入tmp文件,然后将它们传递到下一步。或者,您已经构建了process2,以便它能够识别process1输入中的问题,并能够回滚已经进行的任何更改。这不是一个简单的答案。你确定一切都那么重要吗?也许有其他方法来测试p2的结果,然后停止?祝你好运但是管道中的所有命令都已在运行,因此second.sh将已部分运行。这对他来说可能没问题,但它的语义与他发布的不同。这真的不是一种更好的、可移植的方式。如果您还修复了
$?
的无用用法,我将+1——惯用的编写方法是
if first.sh;然后…
或者更现实地说,
first.sh>/tmp/first.pid |{rm-f/tmp/first.pid;退出$?};sh很好地使用pid-最好使用文件描述符3及以上,但这是使第一个和第二个进程同步的正确方法。