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

15

我经常在文件中发现类似这样的条件语法:.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 是功能还是名称,它们的目的是什么?


4
好的,我会尽力进行翻译。以下是需要翻译的内容:

Loops

A loop is a powerful programming tool that enables you to execute a set of commands repeatedly. Bash supports two main types of loops: for and while.

The for loop

The for loop iterates over a specified list of values. Here's the basic syntax:for var in item1 item2 ... itemN do command1 command2 ... commandN doneHere's an example that prints each item in a list:for i in 1 2 3 4 5 do echo $i doneOutput:1 2 3 4 5You can also use the seq command to generate a sequence of numbers, like this:for i in $(seq 1 5) do echo $i doneOutput:1 2 3 4 5

The while loop

The while loop executes a set of commands as long as a given condition is true. Here's the basic syntax:while [ condition ] do command1 command2 ... commandN doneHere's an example that prints the numbers from 1 to 5:i=1 while [ $i -le 5 ] do echo $i i=$(($i+1)) doneOutput:1 2 3 4 5That's it for loops!
- Sergio Tulentsev
3
或者 man --pager='less -p"^条件表达式"' bash - oliv
4
这些是[程序的普通命令行参数。请参阅man [ - melpomene
4
  1. з”±дғҺж‚ЁдҢүз”Ёдғ†testе‘Ң令пә€з›ёеҢ“дғҺ[ ... ]пә‰пәЊиҮ·жџӨзњ‹man testгЂ‚
  2. иҮ·жіЁж„ЏпәЊ[ ... ]е’Њ[[ ... ]]之й—өжњ‰еЊғ别пә€еЏ‚и§Ѓhttp://mywiki.wooledge.org/BashFAQ/031пә‰гЂ‚
- kvantour
相关:https://dev59.com/bGkv5IYBdhLWcg3w_lzE,https://dev59.com/NXRB5IYBdhLWcg3wSVi1,https://dev59.com/LW865IYBdhLWcg3wU9CI - kvantour
1个回答

20

在我看来,最好的方法是你可以简单地使用man test来获取所有这些细节。在那里有很好的解释。下面是来自man页面的文本。对于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.

1
“man test”。天啊,我不知道它是内置测试命令的一部分。 - Noam Manos

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接