From 018d7f57fbba7470baedd4cfdd6cfb685ce3354a Mon Sep 17 00:00:00 2001 From: Emily Grace Seville Date: Sun, 26 Dec 2021 14:30:57 +1000 Subject: [PATCH] if: update page (#7481) --- pages/common/if.md | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/pages/common/if.md b/pages/common/if.md index 0f16297d9..044807370 100644 --- a/pages/common/if.md +++ b/pages/common/if.md @@ -1,28 +1,36 @@ # if -> Simple shell conditional. +> Performs conditional processing in shell scripts. > See also: `test`, `[`. -> More information: . +> More information: . -- Execute two different commands based on a condition: +- Execute the specified commands if the condition command's exit status is zero: -`if {{command}}; then {{echo "true"}}; else {{echo "false"}}; fi` +`if {{condition_command}}; then {{echo "Condition is true"}}; fi` -- Check if a variable is defined: +- Execute the specified commands if the condition command's exit status is not zero: -`if [[ -n "{{$VARIABLE}}" ]]; then {{echo "defined"}}; else {{echo "not defined"}}; fi` +`if ! {{condition_command}}; then {{echo "Condition is true"}}; fi` -- Check if a file exists: +- Execute the first specified commands if the condition command's exit status is zero otherwise execute the second specified commands: -`if [[ -f "{{path/to/file}}" ]]; then {{echo "true"}}; else {{echo "false"}}; fi` +`if {{condition_command}}; then {{echo "Condition is true"}}; else {{echo "Condition is false"}}; fi` -- Check if a directory exists: +- Check whether a [f]ile exists: -`if [[ -d "{{path/to/directory}}" ]]; then {{echo "true"}}; else {{echo "false"}}; fi` +`if [[ -f {{path/to/file}} ]]; then {{echo "Condition is true"}}; fi` -- Check if a file or directory exists: +- Check whether a [d]irectory exists: -`if [[ -e "{{path/to/file_or_directory}}" ]]; then {{echo "true"}}; else {{echo "false"}}; fi` +`if [[ -d {{path/to/directory}} ]]; then {{echo "Condition is true"}}; fi` + +- Check whether a file or directory [e]xists: + +`if [[ -e {{path/to/file_or_directory}} ]]; then {{echo "Condition is true"}}; fi` + +- Check whether a variable is defined: + +`if [[ -n "${{variable}}" ]]; then {{echo "Condition is true"}}; fi` - List all possible conditions (`test` is an alias to `[`; both are commonly used with `if`):