TOUVE 发表于 2018-8-19 10:36:41

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

  参考:
  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。


页: [1]
查看完整版本: shell中if 变量里包含字符串的判断