Add 'bash/booleans.bash'
This commit is contained in:
23
bash/booleans.bash
Normal file
23
bash/booleans.bash
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# BASH doesn't actually understand booleans.
|
||||||
|
# What you can do however is to trick it by using the fact that the true and false commands return 0 and 1,
|
||||||
|
# so it can stand in for boolean algebra if used correctly.
|
||||||
|
|
||||||
|
BOOLEAN1=false
|
||||||
|
|
||||||
|
if $BOOLEAN1; then
|
||||||
|
echo "We won't get here because BOOLEAN1 is false."
|
||||||
|
fi
|
||||||
|
if ! $BOOLEAN1; then
|
||||||
|
echo "But we DO get here."
|
||||||
|
fi
|
||||||
|
|
||||||
|
BOOLEAN1=true
|
||||||
|
|
||||||
|
if $BOOLEAN1; then
|
||||||
|
echo "Now we do get in this as it's been set to true."
|
||||||
|
fi
|
||||||
|
if ! $BOOLEAN1; then
|
||||||
|
echo "And now we DON'T get here."
|
||||||
|
fi
|
||||||
Reference in New Issue
Block a user