简体   繁体   English

如何查找倒数第二个命令的退出状态?

[英]how to find exit status of second last command?

I am trying to make a script where I strictly want to control the standard output. 我试图在一个脚本中严格控制标准输出。 so I am redirecting everything to /dev/null and printing my own messages. 所以我将所有内容重定向到/ dev / null并打印我自己的消息。

Example: 例:

 user@root> tar zxvf samplefile.tar;> /dev/null 2>&1 ;echo $?  

 gzip: stdin: not in gzip format  
 tar: Child returned status 1  
 tar: Error exit delayed from previous errors  
 0  

tar command failed but $? tar命令失败,但是$? is storing exit status of redirection. 正在存储重定向的退出状态。 I want exit status of tar command. 我想要tar命令的退出状态。

Query: 查询:

How can I get the status of tar command also redirect all std output to /dev/null? 如何获取tar命令的状态并将所有标准输出重定向到/ dev / null?

As mentioned by Subbeh and Ploutox, I should not have used ";" 如Subbeh和Ploutox所述,我不应该使用“;”。 in between the two commands.after removing ";" 在两个命令之间。删除“;”之后 ,redirection became part of the orignal command and gave the correct return code. 重定向已成为原始命令的一部分,并提供了正确的返回码。

However I am still not sure what could be used if we need to get exit status of ($?-1). 但是,我仍然不确定如果需要获取退出状态($?-1)可以使用什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM