检查字符串在Bash中是否包含斜杠或反斜杠



我目前正在尝试让我的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

最新更新