Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,18 @@ Just clone this repo and run:

License is MIT. See LICENSE for details.

### Shreyas About Conditional Tips

While working on the `00_about_conditionals.sh` koan, I learned that Bash conditionals require careful attention to syntax and quoting.
Here are a few lessons I took away from the process:

- Always include spaces around `[` and `]`.
Example: `[ $a = $b ]` is correct, while `[$a=$b]` will fail.
- Quote variable references to prevent unexpected behavior when variables are empty.
Example: `[ "$var" = "text" ]`
- Use either `==` or `=` for string comparisons. Both work in test brackets, though `==` can improve readability.
- Use `-d` to check for directories and `-f` to check for files.
These flags are useful when verifying file paths.
- The order of conditions in `if`, `elif`, and `else` statements matters. Bash evaluates from top to bottom and runs the first matching block.

Overall, these exercises helped reinforce the importance of precision in Bash syntax and how subtle details like spacing or quoting can affect program behavior.
50 changes: 33 additions & 17 deletions src/00_about_conditionals.sh
Original file line number Diff line number Diff line change
@@ -1,78 +1,94 @@
lesson_title "Conditions"

# Basic 'if' condition: executes when the test is true
test_if_condition() {

local test='ok'

# The condition checks if variable 'test' equals the string 'ok'
if [ $test = 'ok' ]; then
local assert='YES'
fi

assertEqual $assert __
# The condition was true, so 'assert' should be 'YES'
assertEqual $assert "YES"
}


# 'if...else' condition: handles both true and false cases
test_if_condition_with_else() {

local test='nope'

# Since 'test' is 'nope', this condition fails
if [ $test == 'ok' ]; then
local assert='YES'
else
# Because the test failed, this branch executes
local assert='NO'
fi

assertEqual $assert __
# The result should be 'NO' since the condition was false
assertEqual $assert "NO"
}

# Using variables in conditional checks
test_if_condition_with_variables() {
local variable="OMG"
local condition='OMG' #__
local condition='OMG' # Both variables hold the same string

if [ "$variable" = "$condition" ]; then
# Compare variable and condition values
if [ "$variable" = "$condition" ]; then
# Since they match, this block runs
local assert='ok'
fi

assertEqual $assert __

# The variable matched the condition, so expect 'ok'
assertEqual $assert "ok"
}

# Multiple conditions using 'elif'
test_multiple_if_conditions() {

local test='zomg' # __
local test='zomg' # The value that will trigger the 'elif' condition

# The first condition fails ('zomg' != 'ok')
if [ $test = 'ok' ]; then
local assert='no'
# The second condition matches, so this branch executes
elif [ $test = 'zomg' ]; then
local assert='YES'
fi

assertEqual $assert __

# The expected result is 'YES'
assertEqual $assert "YES"
}

# Checking if a directory exists
test_directory_if_conditions() {
# The 'src' directory exists in the koans project
if [ -d src ]; then
local assert='yes'
fi

assertEqual $assert __
# Expect 'yes' because the 'src' directory exists
assertEqual $assert "yes"

# Check for a directory that doesn’t exist
if [ ! -d NOT_EXISTENT_DIR ]; then
local assert='no'
fi

assertEqual $assert __

# Expect 'no' because NOT_EXISTENT_DIR does not exist
assertEqual $assert "no"
}

# Checking if a file exists
test_file_if_conditions() {
# The 'README.md' file exists in the repository root
if [ -f README.md ]; then
local assert='yes'
fi

assertEqual $assert __

# Expect 'yes' because README.md exists
assertEqual $assert "yes"
}

# TODO add koans for 'man test' entries