Last active
October 28, 2024 01:02
-
-
Save StrangeRanger/81de73f08234380053cd130ebd1da832 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
#!/usr/bin/env bash | |
# | |
# shellcheck disable=SC2016 | |
# | |
# The point of this script is to show the difference between the different kinds of if | |
# and test statements, and how they behave differently when dealing with different kinds | |
# variables. | |
# | |
######################################################################################## | |
####[ Setup ]########################################################################### | |
a=true | |
b=false | |
c="cat" | |
#d="does_not_exist" <- DO NOT UNCOMMENT THIS LINE | |
echo " | |
---------------------------------- | |
| Value of variables: | | |
| a=true | | |
| b=false | | |
| c='cat' | | |
| # d is a non-existent variable | | |
---------------------------------- | |
" | |
####[ Testing 'If Statements' ]######################################################### | |
### | |
### [ Section 1: If Statements ] | |
### | |
# True if ==> $a is true | |
# Produce ==> true | |
printf 'if "$a"' | |
if "$a"; then echo ' => true' | |
else echo ' => false' | |
fi | |
# True if ==> $b is true | |
# Produce ==> false | |
printf 'if "$b"' | |
if "$b"; then echo ' => true' | |
else echo ' => false' | |
fi | |
# True if ==> DOES NOT WORK... | |
# Produce ==> DOES NOT WORK ... | |
#printf 'if "$c"' | |
#if "$c"; then echo ' => true' | |
#else echo ' => false' | |
#fi | |
# True if ==> COMMAND NOT FOUND | |
# Produce ==> COMMAND NOT FOUND | |
#printf 'if "$d"' | |
#if "$d"; then echo ' => true' | |
#else echo ' => false' | |
#fi | |
### | |
### [ Section 2: If Statements With Brackets ] | |
### | |
# True if ==> $a exists | |
# Produce ==> true | |
printf 'if [[ $a ]]' | |
if [[ $a ]]; then echo ' => true' | |
else echo ' => false' | |
fi | |
# True if ==> $b exists | |
# Produce ==> true | |
printf 'if [[ $b ]]' | |
if [[ $b ]]; then echo ' => true' | |
else echo ' => false' | |
fi | |
# True if ==> $c exists | |
# Produce ==> true | |
printf 'if [[ $c ]]' | |
if [[ $c ]]; then echo ' => true' | |
else echo ' => false' | |
fi | |
# True if ==> $d exists | |
# Produce ==> false | |
printf 'if [[ $d ]]' | |
if [[ $d ]]; then echo ' => true' | |
else echo ' => false' | |
fi | |
### | |
### [ Section 3: If Statements With Brackets and Equals ] | |
### | |
# True if ==> $a is true | |
# Produce ==> true | |
printf 'if [[ $a = true ]]' | |
if [[ $a = true ]]; then echo ' => true' | |
else echo ' => false' | |
fi | |
# True if ==> $b is true | |
# Produce ==> false | |
printf 'if [[ $b = true ]]' | |
if [[ $b = true ]]; then echo ' => true' | |
else echo ' => false' | |
fi | |
# True if ==> $c is true | |
# Produce ==> false | |
printf 'if [[ $c = true ]]' | |
if [[ $c = true ]]; then echo ' => true' | |
else echo ' => false' | |
fi | |
# True if ==> $d is true | |
# Produce ==> false | |
printf 'if [[ $d = true ]]' | |
if [[ $d = true ]]; then echo ' => true' | |
else echo ' => false' | |
fi | |
### | |
### [ Section 4: && Statements ] | |
### | |
# True if ==> $a is true | |
# Produce ==> true | |
printf '"$a" &&' | |
"$a" && echo ' => true' || echo ' => false' | |
# True if ==> $b is true | |
# Produce ==> false | |
printf '"$b" &&' | |
"$b" && echo ' => true' || echo ' => false' | |
# True if ==> DOES NOT WORK... | |
# Produce ==> DOES NOT WORK... | |
#printf '"$c" &&' | |
#"$c" && echo ' => true' || echo ' => false' | |
# True if ==> COMMAND NOT FOUND | |
# Produce ==> COMMAND NOT FOUND | |
#printf '"$d" &&' | |
#"$d" && echo ' => true' || echo ' => false' | |
### | |
### [ Section 5: && Statements With Brackets ] | |
### | |
# True if ==> $a exists | |
# Produce ==> true | |
printf '[[ $a ]] &&' | |
[[ $a ]] && echo ' => true' || echo ' => false' | |
# True if ==> $b exists | |
# Produce ==> true | |
printf '[[ $b ]] &&' | |
[[ $b ]] && echo ' => true' || echo ' => false' | |
# True if ==> $c exists | |
# Produce ==> true | |
printf '[[ $c ]] &&' | |
[[ $c ]] && echo ' => true' || echo ' => false' | |
# True if ==> $c exists | |
# Produce ==> false | |
printf '[[ $d ]] &&' | |
[[ $d ]] && echo ' => true' || echo ' => false' | |
### | |
### [ Section 6: && Statements With Brackets and Equals ] | |
### | |
# True if ==> $a is true | |
# Produce ==> true | |
printf '[[ $a = true ]] &&' | |
[[ $a = true ]] && echo ' => true' || echo ' => false' | |
# True if ==> $b is true | |
# Produce ==> false | |
printf '[[ $b = true ]] &&' | |
[[ $b = true ]] && echo ' => true' || echo ' => false' | |
# True if ==> $c is true | |
# Produce ==> false | |
printf '[[ $c = true ]] &&' | |
[[ $c = true ]] && echo ' => true' || echo ' => false' | |
# True if ==> $d is true | |
# Produce ==> false | |
printf '[[ $d = true ]] &&' | |
[[ $d = true ]] && echo ' => true' || echo ' => false' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment