检查提供的命令的返回码的功能 [英] Function to check the return code of supplied command

查看:52
本文介绍了检查提供的命令的返回码的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

this is a naive attempt to write a command return code checker, as for a long script I have to check $? multiple times. So I wrote a function run_check_proceed() which run with following syntax:

run_check_proceed [0 | 1]< command>#1st arg:是否打印输出,2nd arg:实际命令.

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命令;然后...; fi 块.

In the above execution, I am doing cd to my home dir, then issuing pwd, which is showing correct dir, then I am doing cd to /var/log and then doing pwd which is still showing old directory. I sense that this is because I am doing cd from inside the function and its not applicable on parent shell. So , I am 99% sure that this approach of checking return code is not going to work. But for rest 1% I need others view , if there is some tweak which can help me to avoid writing hundreds of if command; then ... ;fi blocks.

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

推荐答案

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

Try this instead: redirect output into a file.

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

这篇关于检查提供的命令的返回码的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆