In Bash 0 is true and any other number is false, like in C.
To test if a variable is true I'm currently doing:
is_on=0
if [ $is_on -eq 0 ]; then
echo "It's on!"
else
echo "It's off!"
fi
I want somethings more simple and readable, so I tried:
This doesn't because [ always returns 0:
is_on=0
if [ $is_on ]; then
echo "It's on!"
else
echo "It's off!"
fi
This also doesn't because [[ always returns 0:
is_on=0
if [[ $is_on ]]; then
echo "It's on!"
else
echo "It's off!"
fi
This also doesn't work:
is_on=0
if [ $is_on -eq true ]; then
echo "It's on!"
else
echo "It's off!"
fi
This inverses the logic:
is_on=0
if (( $is_on )); then
echo "It's on!"
else
echo "It's off!" # Prints this!
fi
This works, but it's a string comparison:
is_on=true
if [ $is_on = true ]; then
echo "It's on!"
else
echo "It's off!"
fi
Is there a simpler and more legible way to check if a variable is true or false?
Aucun commentaire:
Enregistrer un commentaire