문제

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

도움이 되었습니까?

해결책

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top