if
Perform conditional processing in shell scripts. See also:
test,[. More information: https://www.gnu.org/software/bash/manual/bash.html#Conditional-Constructs.
- Execute the specified commands if the condition command's exit status is zero:
if ; then ; fi
- Execute the specified commands if the condition command's exit status is not zero:
if ! ; then ; fi
- Execute the first specified commands if the condition command's exit status is zero otherwise execute the second specified commands:
if ; then ; else ; fi
- Check whether a [f]ile exists:
if [[ -f ]]; then ; fi
- Check whether a [d]irectory exists:
if [[ -d ]]; then ; fi
- Check whether a file or directory [e]xists:
if [[ -e ]]; then ; fi
- Check whether a variable is defined:
if [[ -n "$" ]]; then ; fi
- List all possible conditions (
testis an alias to[; both are commonly used withif):
man test
来源:tldr-pages contributors · 作者与贡献记录 · CC BY 4.0。离线整理了版式、链接和媒体显示。
