1
0
Fork 0
mirror of https://github.com/tldr-pages/tldr.git synced 2025-06-05 12:45:59 +02:00

if: modernize (#5351)

This commit is contained in:
bl-ue 2021-03-08 15:06:24 -05:00 committed by GitHub
parent 4061936c0c
commit 672a67a2db
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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 [`