【发布时间】:2013-08-11 12:21:51
【问题描述】:
我目前正在尝试让我的 bash 脚本检查字符串是否包含 "/" 或 "\",但不知何故我无法让它工作。
这是我目前得到的:
if [[ "$1" == *\/* ]]; then
...
elif if [[ "$1" == *\\* ]]; then
...
fi
非常感谢您的帮助!谢谢
【问题讨论】:
标签: regex string bash backslash
我目前正在尝试让我的 bash 脚本检查字符串是否包含 "/" 或 "\",但不知何故我无法让它工作。
这是我目前得到的:
if [[ "$1" == *\/* ]]; then
...
elif if [[ "$1" == *\\* ]]; then
...
fi
非常感谢您的帮助!谢谢
【问题讨论】:
标签: regex string bash backslash
这会检查\ 或/ 是否在变量$string 中。
if [[ "$string" == *\/* ]] || [[ "$string" == *\\* ]]
then
echo "yes"
fi
$ string="hello"
$ if [[ "$string" == *\/* ]] || [[ "$string" == *\\* ]]; then echo "yes"; fi
$
$ string="hel\lo"
$ if [[ "$string" == *\/* ]] || [[ "$string" == *\\* ]]; then echo "yes"; fi
yes
$ string="hel//lo"
$ if [[ "$string" == *\/* ]] || [[ "$string" == *\\* ]]; then echo "yes"; fi
yes
【讨论】:
=~而不是==,则不需要周围的*s。
[[ "$string" == *\/* || "$string" == *\\* ]]