【问题标题】:Function to check the return code of supplied command检查提供的命令的返回码的功能
【发布时间】:2019-04-30 18:58:25
【问题描述】:

这是编写命令返回码检查器的幼稚尝试,至于长脚本,我必须多次检查$?。所以我写了一个函数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,它显示正确的目录,然后我对 /var/log 执行 cd,然后执行仍然显示旧目录的 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

【问题讨论】:

  • 你正在一个子shell中运行$command;没有办法以这种方式更改 current shell 的工作目录。
  • ^ 此外,这通常是不好的做法,因为它可能会让人感到困惑。只需将完整目录用于您拥有的任何命令,例如 ls -lah /var/log 而不是 cd /var/log; ls -lah -- PLUS 使用(大多数时候)已经存在的预设置变量几乎总是更好gtg 给你 -- $PWD
  • 如果您只是想在第一个返回非零退出的命令上失败,您可能需要在脚本顶部“设置 -e”davidpashley.com/articles/writing-robust-shell-scripts
  • 我仅使用cdls 作为示例

标签: bash


【解决方案1】:

试试这个:将输出重定向到文件中。

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

【讨论】:

  • 感谢@glenn 的精彩回答。这可以节省很多代码行来检查返回码。
  • 请注意,变量赋值 let x=10 有效,而 x=10 无效。
  • 另一个注释run_check_proceed -d my_dir=${HOME} 不起作用,有什么建议吗?
  • 要让任意 shell simple commands 工作,您需要使用 eval,但随后引用变得更加困难。试试eval "$(printf "%q " "$@")" >"$file" 2>&1
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多