简体   繁体   English

Bash脚本未检测到失败的退出代码

[英]Bash Script not detecting failed exit codes

I can't get my bash script (a logging file) to detect any other exit code other than 0, so the count for failed commands isn't being incremented, but the successes is incremented regardless of whether the command failed or succeeded. 我无法获取我的bash脚本(日志文件)来检测除0以外的任何其他退出代码,因此失败命令的计数不会增加,但是成功将增加,而不管命令是失败还是成功。

Here is the code: 这是代码:

#!/bin/bash
#Script for Homework 8
#Created by Greg Kendall on 5/10/2016
file=$$.cmd
signal() {
    rm -f $file
    echo
    echo "User Aborted by Control-C"
    exit
}
trap signal 2
i=0
success=0
fail=0
commands=0
read  -p "$(pwd)$" "command"
while [ "$command" != 'exit' ]
do
    $command
    ((i++))
    echo $i: "$command" >> $file
    if [ "$?" -eq 0 ]
        then
            ((success++))
            ((commands++))
        else
            ((fail++))
            ((commands++))
    fi
    read -p "$(pwd)" "command"
done
if [ "$command" == 'exit' ]
    then
    rm -f $file
    echo commands:$commands "(successes:$success, failures:$fail)"
fi

Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks! 谢谢!

That's because echo $i: "$command" is succeeding always. 那是因为echo $i: "$command"总是成功。

The exit status $? 退出状态$? in if [ "$?" -eq 0 ] in if [ "$?" -eq 0 ] if [ "$?" -eq 0 ] is actually the exit status of echo , the command that is run immediately before the checking. if [ "$?" -eq 0 ]实际上是echo的退出状态, echo是在检查之前立即运行的命令。

So do the test immediate after the command: 因此,在命令后立即执行测试:

$command
if [ "$?" -eq 0 ]

and use echo elsewhere 并在其他地方使用echo

Or if you prefer you don't need the $? 或者,如果您愿意,则不需要$? check at all, you can run the command and check status within if alone: 检查所有,您可以运行命令并在检查状态if单纯:

if $command; then .....; else ....; fi

If you do not want to get the STDOUT and STDERR: 如果您不想获取STDOUT和STDERR:

if $command &>/dev/null; then .....; else ....; fi

** Note that, as @Charles Duffy mentioned in the comment, you should not run command(s) from variables . **请注意,正如注释中提到的@Charles Duffy一样您不应从variables运行命令

Your code is correctly counting the number of times that the echo $i: "$command" command fails. 您的代码正确计算了echo $i: "$command"命令失败的次数。 I presume that you would prefer to count the number of times that $command fails. 我假设您希望计算$command失败的次数。 In that case, replace: 在这种情况下,请替换:

$command
((i++))
echo $i: "$command" >> $file
if [ "$?" -eq 0 ]

With: 带有:

$command
code=$?
((i++))
echo $i: "$command" >> $file
if [ "$code" -eq 0 ]

Since $? 由于$? captures the exit code of the previous command, it should be placed immediately after the command whose code we want to capture. 捕获前一个命令的退出代码,应将其放置在我们要捕获其代码的命令之后。

Improvement 改善

To make sure that the value of $? 确保$?的值$? is captured before any other command is run, Charles Duffy suggests placing the assignment on the same line as the command like so: 是在运行任何其他命令之前捕获的,Charles Duffy建议将分配与该命令放在同一行,如下所示:

$command; code=$?
((i++))
echo $i: "$command" >> $file
if [ "$code" -eq 0 ]

This should make it less likely that any future changes to the code would separate the command from the capture of the value of $? 这应该使以后对代码进行的任何更改将命令与捕获$?值分开的可能性降低$? .

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

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