1、字段 grep:
案例:
str1="abcdefgh"
str2="def"
result=$(echo $str1 | grep "${str2}")
if [[ "$result" != "" ]];then
echo "包含"
else
echo "不包含"
fi
2. 字符串运算符 =~:
案例:
str1="abcdefgh"
str2="def"
if [[ $str1 =~ $str2 ]];then
echo "包含"
else
echo "不包含"
fi
3、 正则表达式中的通配符 *:
案例:
str1="abcdefgh"
str2="def"
if [[ $str1 == *$str2* ]];then
echo "包含"
else
echo "不包含"
fi
参照:
https ://www.cnblogs.com/smlile-you-me/p/11425404.html
ファイルとディレクトリがLinuxシェルに存在するかどうかを判断する方法
-efilenameファイル名が存在する場合、それはtrueです。
-d filenameがディレクトリの場合、filenameはtrueです。
-f filenameが通常のファイルの場合、filenameはtrueです。
-ファイル名がシンボリックリンクの場合、Lファイル名はtrueです。
-rファイル名が読み取り可能な場合、ファイル名はtrueです。
-wファイル名が書き込み可能である場合、ファイル名はtrueです。
-xファイル名が実行可能ファイルの場合はtrue
-sファイル名が0でない場合、ファイル名はtrueです。
-hファイル名は、ファイルがソフトリンクの場合はtrueです。
#!/bin/sh
#判断文件存在,判断是否为文件夹等
testPath="/Volumes/MacBookProHD/Mr.Wen/08 shell命令"
testFile="/Volumes/MacBookProHD/Mr.Wen/08 shell命令/fileWen"
#判断文件夹是否存在 -d
if [[ ! -d "$testPath" ]]; then
echo "文件夹不存在"
else
echo "文件夹存在"
fi
#判断文件夹是否存在,并且具有可执行权限
if [[ ! -x "$testFile" ]]; then
echo "文件不存在并且没有可执行权限"
else
echo "文件存在并有可执行权限"
fi
#判断文件是否存在
if [[ ! -f "$testFile" ]]; then
echo "文件不存在"
else
echo "文件存在"
fi
参照:https://www.jb51.net/article/186273.htm