0

I wrote a simple script which will prompt pppd to work. I have read in the pppd documentation that it can return an exit code in the range 1-20. I would like my script to return the value of pppd if it is not 0. I tried this way but unfortunately it failed.

myscript.sh:

#!/bin/sh

exec pppd call ${@:-provider}
if [ $? !-eq 0 ]
then
  exit $?
fi

How could i get the exit code from pppd?

1
  • There you can't. exec isn't simply a call to a utility, exec replaces the current shell with a new one for pppd call -- it never returns. You could do if !pppd call ${@:-provider}; then exit X; fi; if you just wanted to have the current shell wait for the pppd script to finish. Commented Mar 30, 2021 at 15:41

1 Answer 1

2

Just do:

#!/bin/sh

pppd call "${@:-provider}" || exit
...

or

if pppd call "${@:-provider}"; then : ; else exit; fi

If pppd fails, the script will exit and the value it returns will be that returned by pppd. (You could explicitly write exit $?, but that is the default value returned by exit when no argument is given and is not necessary.) If pppd succeeds, the script will continue.

The problem with your script (other than the ill-advised usage of exec, which I will mostly ignore) is that calling [ $? -ne 0 ] resets $?. You could re-write your script as:

#!/bin/sh

pppd call "${@:-provider}"
save_val=$?
if ! [ $save_val -eq 0 ]
then
  exit $save_val
fi

but that seems excessively verbose.

Note that in your original script, you would get an error on the line if [ $? !-eq 0 ], since !-eq is not a valid operator. However, you never see that error because your early invocation of exec makes it so that line is never executed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.