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: to print output or not, 2nd arg: actual command.


#this flag will decide to print the commands output on stdout or not. this is set by first argument to the function.
#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
   [email protected];
    echo "Error: First argument must be either 0/1. 0 : to do silent run, 1: to print the command outputs."
    exit 1;

#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"
    echo "[$(date)]:[Info(${BASH_LINENO[0]})]:) $command Sucessful"
    if [ $display_command -eq 1 ];then
        echo "$return_text"
    return 0



#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

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   ./
[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
[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

1 Answers

glenn jackman On Best Solutions

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 ;;
    shift $((OPTIND - 1))
    local file=$(mktemp)

    #run the command
    "[email protected]" >"$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

which outputs

[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
[Tue Apr 30 16:54:41 EDT 2019]:[Info(29)]: "cd" Successful
[Tue Apr 30 16:54:41 EDT 2019]:[Info(30)]: "pwd" Successful
[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