Skip to content

Instantly share code, notes, and snippets.

@StrangeRanger
Last active April 6, 2023 05:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StrangeRanger/81de73f08234380053cd130ebd1da832 to your computer and use it in GitHub Desktop.
Save StrangeRanger/81de73f08234380053cd130ebd1da832 to your computer and use it in GitHub Desktop.
#!/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 |
----------------------------------
"
#### End of [ Setup ]
########################################################################################
#### [ Testing 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
############################################
# 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
############################################
# 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
############################################
# 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'
############################################
# 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'
############################################
# 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'
#### End of [ Testing if statements ]
########################################################################################
# shellcheck shell=bash
#
# 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.
#
# The main difference between this script and the other one is that this script uses
# unit tests to test the output of the if statements, and the other script just uses
# simple bash commands to test the output of the if statements. Niether is better than
# the other. I mainly created to get practice with unit testing in bash.
#
# To execute this script, you must have shellspec installed. You can install it by
# following the instructions here: https://github.com/shellspec/shellspec
# Once you have shellspec installed, you can execute the script by running the following
# command: `shellspec --format tap if_statements_spec.sh`
#
########################################################################################
#### [ Functions ]
func_one() {
# First if statement required to prevent the program from freezing when performing
# unit test. If you just ran the if statement such that it is 'if "$c"', it'd return
# a status of 1, because we are attempting to test if the string "cat" is true or
# false, which is not possible.
if [[ $1 = "cat" ]]; then
return 2
fi
if "$1"; then
return 0
else
return 1
fi
}
func_two() {
# First if statement required to prevent the program from freezing when performing
# unit test. If you just ran the if statement such that it is 'if "$c"', it'd return
# a status of 1.
if [[ $1 = "cat" ]]; then
return 2
fi
"$1" && return 0 || return 1
}
#### End of [ Functions ]
########################################################################################
#### [ 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 |
----------------------------------
"
#### End of [ Setup ]
########################################################################################
#### [ Testing if statements ]
Describe "When the if statement is 'if"
It "\"\$a\"', the output is 'true'"
When call func_one "$a"
The status should eq 0
End
It "\"\$b\"', the output is 'false'"
When call func_one "$b"
The status should eq 1
End
It "\"\$c\"', the output is 'false'"
When call func_one "$c"
The status should eq 2
End
It "\"\$d\"', the output should include 'command not found'"
When call func_one "$d"
The status should eq 1
The stderr should include "command not found"
End
End
Describe "When the if statement is"
It "'\"\$a\" &&', the output is 'true'"
When call func_two "$a"
The status should eq 0
End
It "'\"\$b\" &&', the output is 'false'"
When call func_two "$b"
The status should eq 1
End
It "'\"\$c\" &&', the output is 'false'"
When call func_two "$c"
The status should eq 2
End
It "'\"\$d\" &&', the output should include 'command not found'"
When call func_two "$d"
The status should eq 1
The stderr should include "command not found"
End
End
#### End of [ Testing if statements ]
########################################################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment