我经常在文件中发现类似这样的条件语法:.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
是功能还是名称,它们的目的是什么?
在我看来,最好的方法是你可以简单地使用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.
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
andwhile
.The
Thefor
loopfor
loop iterates over a specified list of values. Here's the basic syntax:for var in item1 item2 ... itemN do command1 command2 ... commandN done
Here's an example that prints each item in a list:for i in 1 2 3 4 5 do echo $i done
Output:1 2 3 4 5
You can also use theseq
command to generate a sequence of numbers, like this:for i in $(seq 1 5) do echo $i done
Output:1 2 3 4 5
The
Thewhile
loopwhile
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 done
Here's an example that prints the numbers from 1 to 5:i=1 while [ $i -le 5 ] do echo $i i=$(($i+1)) done
Output:1 2 3 4 5
That's it for loops! - Sergio Tulentsevman --pager='less -p"^条件表达式"' bash
- oliv[
程序的普通命令行参数。请参阅man [
。 - melpomenetest
е‘Ң令пә€з›ёеҢ“дғҺ[ ... ]
пә‰пәЊиҮ·жџӨзњ‹man test
гЂ‚[ ... ]
е’Њ[[ ... ]]
之й—өжњ‰еЊғ别пә€еЏ‚и§Ѓhttp://mywiki.wooledge.org/BashFAQ/031пә‰гЂ‚