我经常在文件.sh
的Shell脚本中发现以下条件条件:
if [ -n "condition" ]; then ...
if [ -z "condition "]; then ...
if [ -x "condition" ]; then ...
if [ -L "condition" ]; then ...
if [ -d "condition" ]; then ...
这些-n, -z, -x, -L, -d
是函数,或者它的名字和目的是什么?
答案 0 :(得分:3)
恕我直言,最好的方法是您只需为所有这些详细信息进行man test
。那里很好解释。以下是手册页中的文本。对于BASH条件表达式,一次也找不到链接https://www.gnu.org/software/bash/manual/html_node/Bash-Conditional-Expressions.html。
-b FILE FILE exists and is block special -c FILE FILE exists and is character special -d FILE FILE exists and is a directory -e FILE FILE exists -f FILE FILE exists and is a regular file -g FILE FILE exists and is set-group-ID -G FILE FILE exists and is owned by the effective group ID -h FILE FILE exists and is a symbolic link (same as -L) -k FILE FILE exists and has its sticky bit set -L FILE FILE exists and is a symbolic link (same as -h) -O FILE FILE exists and is owned by the effective user ID -p FILE FILE exists and is a named pipe -r FILE FILE exists and read permission is granted -s FILE FILE exists and has a size greater than zero -S FILE FILE exists and is a socket -t FD file descriptor FD is opened on a terminal -u FILE FILE exists and its set-user-ID bit is set -w FILE FILE exists and write permission is granted -x FILE FILE exists and execute (or search) permission is granted
对于man test
中的表达式,给出:
( EXPRESSION ) EXPRESSION is true ! EXPRESSION EXPRESSION is false EXPRESSION1 -a EXPRESSION2 both EXPRESSION1 and EXPRESSION2 are true EXPRESSION1 -o EXPRESSION2 either EXPRESSION1 or EXPRESSION2 is true -n STRING the length of STRING is nonzero STRING equivalent to -n STRING -z STRING the length of STRING is zero STRING1 = STRING2 the strings are equal STRING1 != STRING2 the strings are not equal INTEGER1 -eq INTEGER2 INTEGER1 is equal to INTEGER2 INTEGER1 -ge INTEGER2 INTEGER1 is greater than or equal to INTEGER2
对于条件表达式,请输入man bash
,它也提供如下信息。
条件表达 [[复合命令和测试以及[内置命令]使用条件表达式来测试文件属性和 执行字符串和 算术比较。表达式由以下一元或二进制原语组成。如果任何文件自变量之一 主要是 格式为/ dev / fd / n,然后检查文件描述符n。如果其中一个原语的文件参数是/ dev / stdin之一, / dev / stdout,或 / dev / stderr,分别检查文件描述符0、1或2。
Unless otherwise specified, primaries that operate on files follow symbolic links and operate on the target of the link, rather
比链接 本身。
When used with [[, The < and > operators sort lexicographically using the current locale.