tldr/pages.zh/common/[.md

34 lines
853 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# [
> 检查文件类型,比较数值。
> 如果条件计算结果为真返回 0如果计算结果为假返回 1。
> 更多信息:<https://www.gnu.org/software/bash/manual/bash.html#index-test>.
- 测试一个给定的变量是否等于/不等于指定的字符串:
`[ "${{变量}}" {{=|!=}} "{{字符串}}" ]`
- 测试一个给定的变量是否等于/不等于/大于/小于/大于等于/小于等于指定的数字:
`[ "${{变量}}" -{{eq|ne|gt|lt|ge|le}} {{数字}} ]`
- 测试指定的变量的值是否非空:
`[ -n "${{变量}}" ]`
- 测试指定变量的值是否为空:
`[ -z "${{变量}}" ]`
- 测试指定文件是否存在:
`[ -f {{路径/到/文件}} ]`
- 测试指定目录是否存在:
`[ -d {{路径/到/目录}} ]`
- 测试指定文件或目录是否存在:
`[ -e {{路径/到/文件或目录}} ]`