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:
#!/usr/bin/env bash
install_auto() {
apt-get -h > /dev/null 2>&1
if [ $? -eq 0 ] ; then
sudo apt-get install --assume-yes $@
fi
}
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 first apt
command failed and you did not go into the if
loop. To make it more robust, use this:
#!/usr/bin/env bash
install_auto() {
apt-get -h > /dev/null 2>&1
ret=$?
if [ $ret -eq 0 ] ; then
## If this is executed, the else is ignored and $? will be
## returned. Here, $?will be the exit status of this command
sudo apt-get install --assume-yes $@
else
## Else, return the exit value of the first apt-get
return $ret
fi
}
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:
function foo() {
run_a_command arg1 arg2 argN
## Save the command's exit status into a variable
return_value= $?
[the rest of the function goes here]
## return the variable
return $return_value
}
EDIT: Actually, as @gniourf_gniourf pointed out in the comments, you could greatly simplify the whole thing using &&
:
install_auto() {
apt-get -h > /dev/null 2>&1 &&
sudo apt-get install --assume-yes $@
}
The return value of this function will be one of:
- If
apt-get -h
failed, it will return its exit code
- If
apt-get -h
succeeded, it will return the exit code of sudo apt-get install
.
This would sort of do it:
#!/bin/bash
RESULT=0
while IFS= read -r -u3 -d $'\0' file; do
echo -n "building $file ..."
ant -f "$file" build &&
echo "success" ||
{ echo "failure" ; RESULT=1 ; break; }
done 3< <(find path-a path-b path-c -print0)
echo "result was $RESULT"
Note that it mixes find
with a bash
loop as showed here.
It does not use $?
but instead it directly uses the variable $RESULT
.
If everything goes fine $RESULT
is 0, else it is 1. The loop breaks as soon as an error is encountered.
It should be hopefully safe against malicious file names (because of the use of -print0
).
Best Answer
You could do:
It won't copy anything but the content of the file though (not the permissions, ownership or sparseness as some
cp
implementations do).