我正在尝试使用由不同字符串组成的变量|
作为case
语句测试。例如:
string="\"foo\"|\"bar\""
read choice
case $choice in
$string)
echo "You chose $choice";;
*)
echo "Bad choice!";;
esac
Run Code Online (Sandbox Code Playgroud)
我希望能够输入foo
或bar
执行case
语句的第一部分。然而,无论是foo
和bar
带我去第二:
$ foo.sh
foo
Bad choice!
$ foo.sh
bar
Bad choice!
Run Code Online (Sandbox Code Playgroud)
使用"$string"
而不是$string
没有区别。使用string="foo|bar"
.
我知道我可以这样做:
case $choice in
"foo"|"bar")
echo "You chose $choice";;
*)
echo "Bad choice!";;
esac
Run Code Online (Sandbox Code Playgroud)
我可以想到各种解决方法,但我想知道是否可以case
在 bash 中使用变量作为条件。是否可能,如果可能,如何?