Created
May 13, 2009 19:20
-
-
Save skriticos/111229 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# I think I must have been really bored when I wrote this litte script. | |
# Well, it is a good reference for true/false evaluation. | |
echo | |
echo "Testing \"0\"" | |
if [ 0 ] # zero | |
then | |
echo "0 is true." | |
else # Or else ... | |
echo "0 is false." | |
fi # 0 is true. | |
echo | |
echo "Testing \"1\"" | |
if [ 1 ] # one | |
then | |
echo "1 is true." | |
else | |
echo "1 is false." | |
fi # 1 is true. | |
echo | |
echo "Testing \"-1\"" | |
if [ -1 ] # minus one | |
then | |
echo "-1 is true." | |
else | |
echo "-1 is false." | |
fi # -1 is true. | |
echo | |
echo "Testing \"NULL\"" | |
if [ ] # NULL (empty condition) | |
then | |
echo "NULL is true." | |
else | |
echo "NULL is false." | |
fi # NULL is false. | |
echo | |
echo "Testing \"xyz\"" | |
if [ xyz ] # string | |
then | |
echo "Random string is true." | |
else | |
echo "Random string is false." | |
fi # Random string is true. | |
echo | |
echo "Testing \"\$xyz\"" | |
if [ $xyz ] # Tests if $xyz is null, but... | |
# it's only an uninitialized variable. | |
then | |
echo "Uninitialized variable is true." | |
else | |
echo "Uninitialized variable is false." | |
fi # Uninitialized variable is false. | |
echo | |
echo "Testing \"-n \$xyz\"" | |
if [ -n "$xyz" ] # More pedantically correct. | |
then | |
echo "Uninitialized variable is true." | |
else | |
echo "Uninitialized variable is false." | |
fi # Uninitialized variable is false. | |
echo | |
xyz= # Initialized, but set to null value. | |
echo "Testing \"-n \$xyz\"" | |
if [ -n "$xyz" ] | |
then | |
echo "Null variable is true." | |
else | |
echo "Null variable is false." | |
fi # Null variable is false. | |
echo | |
# When is "false" true? | |
echo "Testing \"false\"" | |
if [ "false" ] # It seems that "false" is just a string. | |
then | |
echo "\"false\" is true." #+ and it tests true. | |
else | |
echo "\"false\" is false." | |
fi # "false" is true. | |
echo | |
echo "Testing \"\$false\"" # Again, uninitialized variable. | |
if [ "$false" ] | |
then | |
echo "\"\$false\" is true." | |
else | |
echo "\"\$false\" is false." | |
fi # "$false" is false. | |
# Now, we get the expected result. | |
# What would happen if we tested the uninitialized variable "$true"? | |
echo | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment