for now I'm looking at install_freeswitch.sh but the concept in the same for the other scripts.
The script basically executes a series of commands, but if one of them fails, the script goes ahead 'blindly'. A workaround for this issue can be define a function called die that looks at the exit code, see the following POC:
#!/bin/bash
die() {
if [ "${?}" != "0" ]
then
test -n "${1}" && echo -ne "\n\n"${1}"\n\n"
exit 1
fi
}
echo "ciao" || die "failed to echo ciao"
run_non_existing_command || die "failed to run the run_non_existing_command"
So, while the code is simply, in short, the || die ensures that the exit code was 0, if not, it hangs immediately. If the die has an argument, then the message is printed, so you know at which command there was the failure.
Hello Dave,
for now I'm looking at
install_freeswitch.sh
but the concept in the same for the other scripts.The script basically executes a series of commands, but if one of them fails, the script goes ahead 'blindly'. A workaround for this issue can be define a function called
die
that looks at the exit code, see the following POC:So, while the code is simply, in short, the
|| die
ensures that the exit code was 0, if not, it hangs immediately. If thedie
has an argument, then the message is printed, so you know at which command there was the failure.What means in the practice?
For example,
install_freeswitch.sh
al line 31 (https://github.com/jambonz/jambonz-infrastructure/blob/main/packer/jambonz-mini/scripts/install_freeswitch.sh#L31), tries tocd
into/usr/local/src
but in the practice the wasn't anmkdir
fo that directory, so it will become:Another example:
will become:
So, if when you are cloning freeswitch.git, you can't reach internet or github, you catch the issue.
The idea comes from here: https://devmanual.gentoo.org/ebuild-writing/error-handling/index.html