我经常.sh在条件 sintaxis 中的文件Shell Script 中找到这样的:
if [ -n "condition" ]; then ...
if [ -z "condition "]; then ...
if [ -x "condition" ]; then ...
if [ -L "condition" ]; then ...
if [ -d "condition" ]; then ...
Run Code Online (Sandbox Code Playgroud)
这些 -n, -z, -x, -L, -d是函数或者它是如何命名的,它的目的是什么?
我的环境是:
GNU coreutils 8.4 有 test 命令来检查文件 using-f选项。
man test 显示
Run Code Online (Sandbox Code Playgroud)-f FILE FILE exists and is a regular file
“常规文件”的定义对我来说是模棱两可的。
在终端上,我做了
$ touch afile
$ ln -fs afile LN-FILE
Run Code Online (Sandbox Code Playgroud)
然后,我执行了以下脚本(check_file_exist_180320_exec)
#!/usr/bin/env bash
if [ -e LN-file ]
then
echo "file exists [-e]"
fi
if [ -f LN-file ]
then
echo "file exists [-f]"
fi
Run Code Online (Sandbox Code Playgroud)
对于 CentOS 和 Ubuntu,对于符号链接文件 (LN-FILE),都显示 -e 和 -f。
但是, ls -l 返回l:symbolik link(不是-:regular file)LN-FILE …