Here's a simplified version of my script. My question is, How do I return the exit code from apt-get
in this case?
#!/bin/bash
install_auto() {
apt-get -h > /dev/null 2>&1
if [ $? -eq 0 ] ; then
return $(sudo apt-get install --assume-yes $@)
fi
return 1
}
echo "installing $@"
install_auto "$@"
echo $?
echo "finished"
exit 0
The output is:
./install_test.sh: line 5: return: Reading: numeric argument required
Update: I came up with something that works:
return $(sudo apt-get install --assume-yes "$@" >/dev/null 2>&1; echo $?)
Is that a good approach?
Best Answer
Bash's
return()
can only return numerical arguments. In any case, by default, it will return the exit status of the last command run. So, all you really need is:You don't need to explicitly set a value to be returned since by default a function will return
$?
. However, that will not work if the firstapt
command failed and you did not go into theif
loop. To make it more robust, use this:The general rule is that in order to have a function return the exit status of a particular job and not necessarily the last one it ran, you will need to save the exit status to a variable and return the variable:
EDIT: Actually, as @gniourf_gniourf pointed out in the comments, you could greatly simplify the whole thing using
&&
:The return value of this function will be one of:
apt-get -h
failed, it will return its exit codeapt-get -h
succeeded, it will return the exit code ofsudo apt-get install
.