您的位置:首页 > 运维架构 > Shell

shell中if 变量里包含字符串的判断

2016-08-03 03:17 274 查看
参考:
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。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: