shell 脚本如何判断文件是否存在


//[ 与 ] 的前后必须有空格符
if [ -f /path/file.ext ]
then
echo "The file exist"
else
echo "The file doesn't exist"
fi

//判断某链接是否存在
if [ -L /path/link ]
then
echo "The link exist"
else
echo "The link doesn't exist"
fi

 

 

表达式 测试含义
-a filepath file exists. all files type
-b filepath file exists and is a block special file.
-c filepath file exists and is a character special file.
-d filepath file exists and is a directory.
-e filepath file exists (等同于 -a).
-f filepath file exists and is a regular file.
-g filepath file exists and has its setgid(2) bit set.
-G filepath file exists and has the same group ID as this process.
-k filepath file exists and has its sticky bit set.
-L filepath file exists and is a symbolic link.
-n filepath string length is not zero.
-o filepath Named option is set on.
-O filepath file exists and is owned by the user ID of this process.
-p filepath file exists and is a first in, first out (FIFO) special file or
named pipe.
-r filepath file exists and is readable by the current process.
-s filepath file exists and has a size greater than zero.
-S filepath file exists and is a socket.
-t filepath file descriptor number fildes is open and associated with a
terminal device.
-u filepath file exists and has its setuid(2) bit set.
-w filepath file exists and is writable by the current process.
-x filepath file exists and is executable by the current process

posted @ 2022-05-05 19:41  胜天、半子  阅读(1696)  评论(0编辑  收藏  举报