From 672a67a2db7be98a6d822334d73b90a13f20cf05 Mon Sep 17 00:00:00 2001 From: bl-ue <54780737+bl-ue@users.noreply.github.com> Date: Mon, 8 Mar 2021 15:06:24 -0500 Subject: [PATCH] if: modernize (#5351) --- pages/common/if.md | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/pages/common/if.md b/pages/common/if.md index 95108a4843..3154b2c233 100644 --- a/pages/common/if.md +++ b/pages/common/if.md @@ -1,27 +1,28 @@ # if > Simple shell conditional. +> See also: `test`, `[`. -- Echo a different thing depending on a command's success: +- Execute two different commands based on a condition: -`if {{command}}; then echo "success"; else echo "failure"; fi` +`if {{command}}; then {{echo "true"}}; else {{echo "false"}}; fi` -- Full if syntax: +- Check if a variable is defined: -`if {{condition}}; then echo "true"; else echo "false"; fi` +`if [[ -n "{{$VARIABLE}}" ]]; then {{echo "defined"}}; else {{echo "not defined"}}; fi` -- List available if conditions: +- Check if a file exists: -`help test` +`if [[ -f "{{path/to/file}}" ]]; then {{echo "true"}}; else {{echo "false"}}; fi` -- Test if a given variable is empty: +- Check if a directory exists: -`if [[ -z $GIT_BRANCH ]]; then echo "true"; else echo "false"; fi` +`if [[ -d "{{path/to/directory}}" ]]; then {{echo "true"}}; else {{echo "false"}}; fi` -- Test if a file exists: +- Check if a file or directory exists: -`if [[ -e {{filename}} ]]; then echo "true"; else echo "false"; fi` +`if [[ -e "{{path/to/file_or_directory}}" ]]; then {{echo "true"}}; else {{echo "false"}}; fi` -- If directory not exists: +- List all possible conditions (`test` is an alias to `[`; both are commonly used with `if`): -`if [[ ! -d {{path/to/directory}} ]]; then echo "true"; else echo "false"; fi` +`man [`