检查Bash中的字符串包含斜杠还是反斜杠? [英] Check if string containts slash or backslash in Bash?

查看:69
本文介绍了检查Bash中的字符串包含斜杠还是反斜杠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在尝试获取bash脚本,以检查字符串是否包含"/" "\"

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.

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

Here is what I got so far:

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

非常感谢您的帮助!谢谢

Help is much appreciated! Thanks

推荐答案

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

This checks if either \ or / are in the variable $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

这篇关于检查Bash中的字符串包含斜杠还是反斜杠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆