我经常.sh
在条件 sintaxis 中的文件Shell Script 中找到这样的:
if [ -n "condition" ]; then ...
if [ -z "condition "]; then ...
if [ -x "condition" ]; then ...
if [ -L "condition" ]; then ...
if [ -d "condition" ]; then ...
Run Code Online (Sandbox Code Playgroud)
这些 -n, -z, -x, -L, -d
是函数或者它是如何命名的,它的目的是什么?
Rav*_*h13 11
恕我直言,最好的方法是您可以简单地man test
处理所有这些细节。那里有很好的解释。以下是手册页的文本。对于 BASH 条件表达式,也请查找链接https://www.gnu.org/software/bash/manual/html_node/Bash-Conditional-Expressions.html一次。
Run Code Online (Sandbox Code Playgroud)-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
给出:
Run Code Online (Sandbox Code Playgroud)( 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
它也提供如下信息。
条件表达式 [[ 复合命令和 test 和 [ 内置命令使用条件表达式来测试文件属性并执行字符串和算术比较。表达式由以下一元或二元原色组成。如果任何一个主文件参数的格式为 /dev/fd/n,则检查文件描述符 n。如果主要参数之一的文件参数是 /dev/stdin、/dev/stdout 或 /dev/stderr 之一,则分别检查文件描述符 0、1 或 2。
Run Code Online (Sandbox Code Playgroud)Unless otherwise specified, primaries that operate on files follow symbolic links and operate on the target of the link, rather
比链接本身。
Run Code Online (Sandbox Code Playgroud)When used with [[, The < and > operators sort lexicographically using the current locale.
归档时间: |
|
查看次数: |
1831 次 |
最近记录: |