gpt4 book ai didi

linux - Shell 脚本中 -n、-z、-x、-L、-d 等的含义是什么?

转载 作者:太空宇宙 更新时间:2023-11-04 04:39:43 24 4
gpt4 key购买 nike

我经常在文件 .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是函数,或者它的名称如何以及它的用途是什么?

最佳答案

恕我直言,最好的方法是您可以简单地对所有这些细节进行人工测试。那里解释得很好。以下是手册页的文本。对于 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 它也提供如下信息。

CONDITIONAL EXPRESSIONS Conditional expressions are used by the [[ compound command and the test and [ builtin commands to test file attributes and perform string and arithmetic comparisons. Expressions are formed from the following unary or binary primaries. If any file argument to one of the primaries is of the form /dev/fd/n, then file descriptor n is checked. If the file argument to one of the primaries is one of /dev/stdin, /dev/stdout, or /dev/stderr, file descriptor 0, 1, or 2, respectively, is checked.

   Unless otherwise specified, primaries that operate on files follow symbolic links and operate on the target of the link, rather 

than the link itself.

   When used with [[, The < and > operators sort lexicographically using the current locale.

关于linux - Shell 脚本中 -n、-z、-x、-L、-d 等的含义是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53319817/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com