【发布时间】:2012-11-21 10:07:40
【问题描述】:
谁能告诉我这个脚本会做什么?第三行的-z是什么?
Filename=File.txt
X=`ls /home/$Filename`
if [ -z "$X" ]; then
exit
fi
【问题讨论】:
-
你应该付出更多的研究努力
谁能告诉我这个脚本会做什么?第三行的-z是什么?
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
-e 如果路径存在则返回 true。您还可以检查文件 (-f)、目录 (-d)、符号链接 (-L) 等。查看man [ 以获取更多可与[ 一起使用的选项。您还可以在那里找到-z:
-z string如果string的长度为零,则为真。
【讨论】:
脚本的作用是
创建一个名为 Filename 的变量 Filename 的值为 File.txt 执行命令 ls File.txt 将结果分配给变量 X 然后检查变量 X 中值的长度是否为零 如果为零,则脚本将退出 所以基本上你的程序所做的是,它检查文件是否存在
你可以使用所有这些行来代替
if [ -a "$Filename"]
此链接会对您有所帮助
http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html
【讨论】:
它测试/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.
【讨论】: