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
리눅스 쉘에 파일과 디렉토리가 존재하는지 판단하는 방법
-e filename 파일명이 존재하면 참
-d filename은 파일 이름이 디렉토리이면 참입니다.
-f filename이 일반 파일이면 true입니다.
-L filename은 filename이 심볼릭 링크 인 경우 true입니다.
-r filename을 읽을 수있는 경우 filename은 true입니다.
-w filename이 쓰기 가능한 경우 파일 이름은 true입니다.
-x filename이 실행 가능하면 true입니다.
-s filename은 파일 길이가 0이 아니면 참입니다.
-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