0

I have the following code:

debug=$?
function a {
    su - javi -c "uptime"
    return $debug
}

function b {
    su - javi -c "cat /etc/redhat-release"
    return $debug
}

function c {
    su - javi -c "cat /etc/redhat-release"
    return $debug
}

case $debug in
0)
    a
    if [ $debug == 0 ]; then
        b
        echo "se ejcuta la funcion"
    elif [ $debug == 0 ]
        c   
    elif[].... <-----this

    fi
;;

1)
    echo "se ha producido un error"
;;

esac

Is there any way to debug by removing the if ??I want them to go running a function if it ends well that jumps to the other function and if it does not end well that it leaves the escript, that with 5 functions

6
  • ìf false ; thenfollowed by # if?
    – Archemar
    Feb 6, 2019 at 19:39
  • 1
    You also don't need the su operation to run uptime or to read /etc/redhat-release. And why do you return $debug from your functions when it's a global variable, and you ignore the return value anyway? Feb 6, 2019 at 19:48
  • To do what you want, you need to reset $debug with the result of the previous command. Since you're not doing that it looks like all your tests are measuring the initial value of $debug, which incidentally, is $?... not sure what that would be in a new subshell. is it just the value of the last command run? Feb 6, 2019 at 19:48
  • 1
    @Tim yes it is. So here Ortiga could run false; ./myscript.sh to enable debug or true; ./myscript.sh to keep it off. But it would also get triggered with two commands such as ls /does/not/exist then ./myscript.sh Feb 6, 2019 at 19:49
  • 1
    Ortiga, you probably want to run this through shellcheck.net You're missing space around the [ and ] characters, for starters. And == is a string comparison. Use -eq for a numeric one. Feb 6, 2019 at 19:51

2 Answers 2

0

If the goal is simply to run each function, and bail out of there was any errors, maybe something like this will work:

function bail {
    echo "se ha producido un error ($1)"
    exit 1
}

function a {
    uptime
}

function b {
    cat /etc/redhat-release
}

function c { 
    cat /etc/redhat-release
}

for f in 'a' 'b' 'c'; do
    $f || bail "$f: $?"
done

This will run each function, and if an error is encountered, the function name and exit code are sent to the bail function, which will print a line and bail out of any additional commands.

I did remove the use of su because it was easier to test that way. If you want to automate this, I'd recommend creating a sudo profile to run those commands with elevated privileges in such a way that you only need to enter your password once, or no times at all.

0
  1. debug=$? is not evaluated at every command. So the value of debug will be the same throughout the entire script.
  2. The return code of a function is the return code of its last command. So the return statements are pointless.

In summary, something like a && b && c will run b if a succeeds, and will run c if b succeeds. The return code of this compound command is the first non-zero return code of each of the functions.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .