检查提供的命令的返回码的功能

时间:2019-04-30 18:58:25

标签: bash

这是天真地尝试编写命令返回码检查器,因为长脚本我必须多次检查$?。因此,我编写了一个函数run_check_proceed(),该函数使用以下语法运行:

run_check_proceed [0|1] <command> #1st arg: to print output or not, 2nd arg: actual command.

run_check_proceed()
{
display_command=0;

#this flag will decide to print the commands output on stdout or not. this is set by first argument to the function.
provided_display_flag=${1};
#check if the flag is either 0 or 1 and store the rest of the arguments into command variable, else exit.
if echo ${provided_display_flag} |grep -qP '^[01]$' ;then
   if [ ${provided_display_flag} -eq 1 ];then
       display_command=1;
   fi
   shift;
   command=$@;
else
    echo "Error: First argument must be either 0/1. 0 : to do silent run, 1: to print the command outputs."
    exit 1;
fi

#run the command
return_text=$($command 2>&1 )

if [ $? -ne 0 ];then
    echo "[$(date)]:[Error(${BASH_LINENO[0]})]: $command failed $return_text"
    if [ $display_command -eq 1 ];then
        echo "$return_text"
    fi
else
    echo "[$(date)]:[Info(${BASH_LINENO[0]})]:) $command Sucessful"
    if [ $display_command -eq 1 ];then
        echo "$return_text"
    fi
    return 0

fi

}

#sample runs
run_check_proceed 1  cd /home/${USER}
run_check_proceed 1  pwd
run_check_proceed 1  cd /var/log          #this should cd to /var/log
run_check_proceed 1  pwd

在上面的执行中,我将cd复制到我的主目录,然后发出pwd,它显示正确的目录,然后我将cd复制到/ var / log,然后执行pwd,它仍然显示旧目录。我感觉这是因为我正在函数内部执行cd,并且不适用于父shell。因此,我99%确信这种检查返回码的方法将行不通。但是对于剩下的1%,我需要其他人的看法,如果有一些调整可以帮助我避免编写数百个if command; then ... ;fi块。

bash   ./run_check.sh
[Tue Apr 30 13:52:35 CDT 2019]:[Info(41)]:) cd /home/monk Sucessful

[Tue Apr 30 13:52:35 CDT 2019]:[Info(42)]:) pwd Sucessful
/home/monk/temp
[Tue Apr 30 13:52:35 CDT 2019]:[Info(43)]:) cd /var/log Sucessful

[Tue Apr 30 13:52:35 CDT 2019]:[Info(44)]:) pwd Sucessful
/home/monk/temp

1 个答案:

答案 0 :(得分:2)

请尝试以下操作:将输出重定向到文件中。

run_check_proceed() {
    local OPTARG OPTIND opt
    local display=false
    while getopts :d opt; do
        case $opt in 
            d) display=true ;;
        esac
    done
    shift $((OPTIND - 1))
    local file=$(mktemp)

    #run the command
    "$@" >"$file" 2>&1
    local exit_status=$?

    local status=Info result=Successful
    ((exit_status != 0)) && { status=Error; result=Failed; }

    printf '[%s]:[%s(%d)]: "%s" %s\n' "$(date)" "$status" "${BASH_LINENO[0]}" "$*" "$result"
    $display && cat "$file"

    rm "$file"
    return $exit_status
}

#sample runs
run_check_proceed     date                                  # verify no output
run_check_proceed -d  sh -c 'echo oops >&2; exit 42'        # verify Error output
run_check_proceed     cd
run_check_proceed -d  pwd
run_check_proceed     cd /var/log          #this should cd to /var/log
run_check_proceed -d  pwd

输出

[Tue Apr 30 16:54:41 EDT 2019]:[Info(27)]: "date" Successful
[Tue Apr 30 16:54:41 EDT 2019]:[Error(28)]: "sh -c echo oops >&2; exit 42" Failed
oops
[Tue Apr 30 16:54:41 EDT 2019]:[Info(29)]: "cd" Successful
[Tue Apr 30 16:54:41 EDT 2019]:[Info(30)]: "pwd" Successful
/home/jackman
[Tue Apr 30 16:54:41 EDT 2019]:[Info(31)]: "cd /var/log" Successful
[Tue Apr 30 16:54:41 EDT 2019]:[Info(32)]: "pwd" Successful
/var/log