参考:

http://bbs.chinaunix.net/thread-1633281-1-1.html


需求:

判断变量cache_dir中是够包括"/data/cache"字符串


法1:

if [[ "${cache_dir}" =~ "/data/cache" ]]; then
   echo "true"
fi

法2:

if [[ ${cache_dir} = */data/cache* ]]; then
   echo "true"
fi

法3:

if echo ${cache_dir} |grep -q "/data/cache"; then
   echo "true"
fi

法4:

echo ${cache_dir} |grep -q "/data/cache" && echo "true" || echo "false"


特别说明,以上方法适用于所有遵从POSIX的shell,如ksh。