I have a function which returns one of three values, 0, 1 or 2
I want to check the return value of the function in an if statement.
How can I do this in idiomatic bash?
This works, but looks very wrong to me:
vercomp $prev $new
result=$?
if [[ result -eq 1 ]]; then
echo "Greater than"
elif [[ result -eq 2 ]]; then
echo "Less than"
fi
How can I consolidate this code and use the function directly in the if statement instead of using the intermediate result variable and how can I access the return value without using $? ?
Everything I have tried has failed.
For example even this fails - why?
result=vercomp $prev $new
if [[ result -eq 1 ]]; then
echo "Greater than"
elif [[ result -eq 2 ]]; then
echo "Less than"
fi
And this fails:
if [[ vercomp $prev $new -eq 1 ]]; then
echo "Greater than"
elif [[ vercomp $prev $new -eq 2 ]]; then
echo "Less than"
fi
This fails:
if [[ $(vercomp $prev $new) -eq 1 ]]; then
echo "Greater than"
elif [[ $(vercomp $prev $new) -eq 2 ]]; then
echo "Less than"
fi
What am I doing wrong? How can I improve my working code?