I have looked at the bash function return values and it seems I still don't understand how that works. I setup 3 function each supposedly returning a RET_VAL return value. I assume func_1 returns either a 1 or 0 based on the if statement I assume func_2 return either a 1 or a 0 based on the if statement I assume func_3 should either print a 1 or a 0 based on the return value of func_2
func_1() {
RET_VAL=0
if [ -d /tmp/dir ]; then
echo "Dir exists"
RET_VAL=0
else
echo "Dir doesn't exist"
RET_VAL=1
fi
return ${RET_VAL}
}
func_2() {
RET_VAL=0
if func_1; then
if [ -f /tmp/file_1]; then
echo "File exists"
else
echo "File doesn't exist"
RET_VAL=1
fi
else
RET_VAL=1
fi
return ${RET_VAL}
}
func_3() {
if func_2; then
echo "Dir and File do exist"
else
echo "Dir and file do not exist"
fi
}
Are my assumptions correct or is each function returning what it executed last, like the echo statement? If so, how could I assure that the functions return a 1 or a 0 value?
Cheers, Roland
RET_VALat the top offunc_2will be immediately overwritten by the ones insidefunc_1. (In this case this happens to have no net effect, in the behavior of the code, though).