有人可以告訴我這個腳本會做什麼嗎?第三行的-z是什麼?解釋這個bash腳本
Filename=File.txt
X=`ls /home/$Filename`
if [ -z "$X" ]; then
exit
fi
有人可以告訴我這個腳本會做什麼嗎?第三行的-z是什麼?解釋這個bash腳本
Filename=File.txt
X=`ls /home/$Filename`
if [ -z "$X" ]; then
exit
fi
Filename=File.txt
的$Filename
變量現在的值爲File.txt
。
X=`ls /home/$Filename`
的$X
變量現在包含指令ls /home/File.txt
的輸出。如果文件存在,它將包含一些內容,否則它將是一個空字符串。
if [ -z "$X" ]; then
如果變量$X
的值是一個空字符串(這意味着/home/File.txt
不存在,否則$X
將包含的東西),然後:
exit
fi
這是一個相當尷尬寫作方式:
if ! [ -e "/home/File.txt" ]; then exit; fi
如果路徑存在,3210返回true。您還可以檢查文件(-f
),目錄(-d
),符號鏈接(-L
)等。請查看man [
瞭解更多可與[
一起使用的選項。您還可以找到-z
有:
-z string
真如果string
長度爲零。
Thx回答! – Steve88
什麼腳本做是
創建一個名爲文件名 變量名的值是FILE.TXT 執行命令ls FILE.TXT 結果分配給變量X 然後檢查是否長度的變量X的值爲零 如果它爲零,腳本將退出 因此,基本上你的程序所做的是檢查一個文件是否存在
而不是所有這些行,你也可以使用
if [ -a "$Filename"]
此鏈接將有助於您
http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html
因此,這是一種測試文件的存在? – Steve88
它測試,如果/home/File.txt
存在,&如果沒有,exit
。但它不是一個理想的解決方案,這是更好寫成這樣:
if ! test -e /home/File.txt; then exit; fi
$ help test
test: test [expr]
Evaluate conditional expression.
Exits with a status of 0 (true) or 1 (false) depending on
the evaluation of EXPR. Expressions may be unary or binary. Unary
expressions are often used to examine the status of a file. There
are string operators and numeric comparison operators as well.
The behavior of test depends on the number of arguments. Read the
bash manual page for the complete specification.
File operators:
-a FILE True if file exists.
-b FILE True if file is block special.
-c FILE True if file is character special.
-d FILE True if file is a directory.
-e FILE True if file exists.
-f FILE True if file exists and is a regular file.
-g FILE True if file is set-group-id.
-h FILE True if file is a symbolic link.
-L FILE True if file is a symbolic link.
-k FILE True if file has its `sticky' bit set.
-p FILE True if file is a named pipe.
-r FILE True if file is readable by you.
-s FILE True if file exists and is not empty.
-S FILE True if file is a socket.
-t FD True if FD is opened on a terminal.
-u FILE True if the file is set-user-id.
-w FILE True if the file is writable by you.
-x FILE True if the file is executable by you.
-O FILE True if the file is effectively owned by you.
-G FILE True if the file is effectively owned by your group.
-N FILE True if the file has been modified since it was last read.
FILE1 -nt FILE2 True if file1 is newer than file2 (according to
modification date).
FILE1 -ot FILE2 True if file1 is older than file2.
FILE1 -ef FILE2 True if file1 is a hard link to file2.
String operators:
-z STRING True if string is empty.
-n STRING
STRING True if string is not empty.
STRING1 = STRING2
True if the strings are equal.
STRING1 != STRING2
True if the strings are not equal.
STRING1 < STRING2
True if STRING1 sorts before STRING2 lexicographically.
STRING1 > STRING2
True if STRING1 sorts after STRING2 lexicographically.
Other operators:
-o OPTION True if the shell option OPTION is enabled.
-v VAR True if the shell variable VAR is set
! EXPR True if expr is false.
EXPR1 -a EXPR2 True if both expr1 AND expr2 are true.
EXPR1 -o EXPR2 True if either expr1 OR expr2 is true.
arg1 OP arg2 Arithmetic tests. OP is one of -eq, -ne,
-lt, -le, -gt, or -ge.
Arithmetic binary operators return true if ARG1 is equal, not-equal,
less-than, less-than-or-equal, greater-than, or greater-than-or-equal
than ARG2.
Exit Status:
Returns success if EXPR evaluates to true; fails if EXPR evaluates to
false or an invalid argument is given.
太棒了!我會!謝謝! – Steve88
你應該表現出更多的研究工作 – keyser
[谷歌是你的朋友(http://www.google.com/search?q=bash+-z+test) – koola