【发布时间】: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
-
我仅使用
cd和ls作为示例
标签: bash