我目前正在尝试让我的bash脚本检查字符串是否包含"/"
或""
,但不知怎么的,我无法让它工作。
到目前为止,我得到的是:
if [[ "$1" == */* ]]; then
...
elif if [[ "$1" == *\* ]]; then
...
fi
非常感谢您的帮助!感谢
这会检查或
/
是否在变量$string
中。
if [[ "$string" == */* ]] || [[ "$string" == *\* ]]
then
echo "yes"
fi
测试:
$ string="hello"
$ if [[ "$string" == */* ]] || [[ "$string" == *\* ]]; then echo "yes"; fi
$
$ string="hello"
$ if [[ "$string" == */* ]] || [[ "$string" == *\* ]]; then echo "yes"; fi
yes
$ string="hel//lo"
$ if [[ "$string" == */* ]] || [[ "$string" == *\* ]]; then echo "yes"; fi
yes
感谢@fedorqui下面的评论,这里有一个更简单的语法:
#!/bin/bash
string=$1
if [[ "$string" =~ '/' || "$string" =~ '' ]]
then
echo "yes"
fi