Check if string containts slash or backslash in Bash?

只愿长相守 提交于 2020-01-13 12:12:28

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/18148883/check-if-string-containts-slash-or-backslash-in-bash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!