覆盖退出状态的bash脚本中命令输出的第一行

时间:2013-09-02 06:30:00

标签: bash unix

我正在尝试在另一个shell脚本的函数中执行测试bash脚本。 该函数接受一个参数,即test bash脚本(echo命令),然后检查退出状态。这里的问题是退出状态被视为测试脚本输出的第一行。

脚本:single_run.sh

    function sing_run() {
    exec_script=$1
    `$exec_script`
    return_status=$?

    if [[ `$return_status` = "0" ]]; then
    echo "SUCCESS"
    else
    echo "FAILURE"
    fi

    }

    sing_run "/bin/bash -x /home/mspprod/customers/adobe/prod/scripts/test_run.sh | tee -a /home/mspprod/customers/adobe/prod/logs/testing.log"

脚本:test_run.sh

#!/bin/bash

echo "This is a test script"

错误消息:

bash -x single_run.sh
+ sing_run '/bin/bash -x /home/mspprod/customers/adobe/prod/scripts/test_run.sh | tee 
-a /home/mspprod/customers/adobe/prod/logs/testing.log'
+ exec_script='/bin/bash -x /home/mspprod/customers/adobe/prod/scripts/test_run.sh | 
tee -a /home/mspprod/customers/adobe/prod/logs/testing.log'
++ /bin/bash -x /home/mspprod/customers/adobe/prod/scripts/test_run.sh '|' tee -a 
/home/mspprod/customers/adobe/prod/logs/testing.log
+ echo 'This is a Test Script'
+ This is a Test Script
single_run.sh: line 5: This: command not found
+ return_status='echo 127'
++ echo 127
+ [[ 127 = \0 ]]
+ echo FAILURE
FAILURE

如何获得正确的退出状态而不是This作为退出状态?

1 个答案:

答案 0 :(得分:0)

function myfunc()
{
  bash -x $1 | tee -a output.log
}

myfunc $1

if [ $?=0 ] 
  then 
    echo "Success"
else
  echo "Failure"
fi

这将执行脚本并检查返回码