if: update page (#7481)

This commit is contained in:
Emily Grace Seville
2021-12-26 14:30:57 +10:00
committed by GitHub
parent 60333f871c
commit 018d7f57fb

View File

@@ -1,28 +1,36 @@
# if # if
> Simple shell conditional. > Performs conditional processing in shell scripts.
> See also: `test`, `[`. > See also: `test`, `[`.
> More information: <https://www.tcl.tk/man/tcl8.6.11/TclCmd/if.html>. > More information: <https://www.gnu.org/software/bash/manual/bash.html#Conditional-Constructs>.
- 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`): - List all possible conditions (`test` is an alias to `[`; both are commonly used with `if`):