if: update page (#7481)

feature/windows-fix-syntax-2
Emily Grace Seville 2021-12-26 14:30:57 +10:00 committed by GitHub
parent 60333f871c
commit 018d7f57fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 12 deletions

View File

@ -1,28 +1,36 @@
# if
> Simple shell conditional.
> Performs conditional processing in shell scripts.
> 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`):