# 注意
# [[ 后面要有个空格,否则会报错
# ]] 前面要有个空格,否则会报错
if [[ -z "${STR1}" ]]; then
echo "str1 is null"
else
echo "str1=$ISNULL"
fi
-z 参数
[ -z STRING ] 判断字符串长度是否为空
STR1=
STR2=""
STR3=''
STR4=NULL
if [[ -z "${STR1}" ]]; then
echo "str1 is null"
fi
if [[ -z "${STR2}" ]]; then
echo "str2 is null"
fi
if [[ -z "${STR3}" ]]; then
echo "str3 is null"
fi
if [[ -z "${STR4}" ]]; then
echo "str4 is null"
fi
打印结果:
-n 参数
[ -n STRING ] 判断字符串长度是否为非空字符串
STR1=
STR2=""
STR3=''
STR4=NULL
STR5=123
# -n是否是空字符串 -n判断是否是非空字符串
if [[ -n "${STR1}" ]]; then
echo "str1 is not null"
fi
# -n是否是空字符串 -n判断是否是非空字符串
if [[ -n "${STR2}" ]]; then
echo "str2 is not null"
fi
# -n是否是空字符串 -n判断是否是非空字符串
if [[ -n "${STR3}" ]]; then
echo "str3 is not null"
fi
# -n是否是空字符串 -n判断是否是非空字符串
if [[ -n "${STR4}" ]]; then
echo "str4 is not null"
fi
# -n是否是空字符串 -n判断是否是非空字符串
if [[ -n "${STR5}" ]]; then
echo "str5 is not null"
fi
打印结果:
文件相关参数
# 通过help查看
help test
-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.
-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.
-e -d 例子
BASE_PATH=/media
# 判断是否是目录
if [[ -d ${BASE_PATH}/conf/ ]]; then
echo "$BASE_PATH/conf is directory"
fi
# 判断文件或目录是否存在
if [[ -e ${BASE_PATH}/conf1/ ]]; then
echo "$BASE_PATH/conf1 is exists"
else
echo "$BASE_PATH/conf1 is not exists"
fi