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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18148883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:11:48  来源:igfitidea点击:

Check if string containts slash or backslash in Bash?

regexstringbashbackslash

提问by user1685565

I'm currently trying to get my bash script checking if a string containts a "/"or a "\"but somehow I can't get it working.

我目前正在尝试让我的 bash 脚本检查字符串是否包含 a"/"或 a"\"但不知何故我无法让它工作。

Here is what I got so far:

这是我到目前为止所得到的:

if [[ "" == *\/* ]]; then
   ...
elif if [[ "" == *\* ]]; then
   ...
fi

Help is much appreciated! Thanks

非常感谢帮助!谢谢

回答by fedorqui 'SO stop harming'

This checks if either \or /are in the variable $string.

这将检查\/是否在变量中$string

if [[ "$string" == *\/* ]] || [[ "$string" == *\* ]]
then
  echo "yes"
fi

Test:

测试:

$ 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