diff --git a/README.md b/README.md index 3b091ff..20487ee 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/00_about_conditionals.sh b/src/00_about_conditionals.sh index 37ab76b..0462316 100644 --- a/src/00_about_conditionals.sh +++ b/src/00_about_conditionals.sh @@ -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