In all cases I would save the resultcode; Either from the PIPESTATUS array or from $?
The bunch of if statements could be replaced by a switch:
Code:
ask=$(zenity --entry --title="age" --width=100 --height=100 --text="write your age" --entry-text="20")
Rc=$?
case $Rc in
"5") echo "20" > age.tmp ;;
"0") echo "$ask" > age.tmp ;;
"1") echo "20" > age.tmp ;;
esac
or use if then elif then fi construction like so:
Code:
ask=$(zenity --entry --title="age" --width=100 --height=100 --text="write your age" --entry-text="20")
Rc=$?
if [[ $Rc = "5" ]]
then
echo "20" > age.tmp
elif [[ $Rc = "0" ]]
then
echo "$ask" > age.tmp
elif [[ $Rc = "1" ]]
then
echo "20" > age.tmp
fi
Both are more logical: you're testing against a fixed value towards the same variable that cannot have changed in between the comparisons; So, having tested one as true, the other cases do not have to be computed, since they cannot be true.
And always, have a care with special (volatile) variables; $? and PIPESTATUS change directly after each command.
For good measure, as I mentioned PIPESTATUS, here's an example:
Code:
somefunction | tee -a logfile.log
RETVAL=${PIPESTATUS[0]}
Pipestatus is (particulary) handy when you want to know the return value of one (or more) commands that have been executed in a pipe; It is an array, and if you want to keep all values, you have to store them all in an array:
Code:
true | false | echo done | grep nothing | true | false | true | false
RETVAL=${PIPESTATUS[*]}
echo ${RETVAL[*]}