bash Case 语句评估为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2283640/
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
Case statements evaluate to strings
提问by mbac32768
I've caught the functional programming bug, so naturally nothing is good enough for me anymore. ;)
我已经发现了函数式编程的错误,所以自然没有什么对我来说足够好了。;)
So, in bash one could write:
因此,在 bash 中可以这样写:
case $status in
"foo") status="bar" ;;
"baz") status="buh" ;;
*) status=$status ;;
esac
but I'm afraid of typos, so I'd preferto write:
但我怕打错字,所以我更愿意写:
status=case $status in
"foo") "bar" ;;
"baz") "buh" ;;
*) $status ;;
esac
The second form is invalid since the case evaluates to the exit code of the last executed command, which is not at all what I'm looking for.
第二种形式无效,因为 case 评估为最后执行的命令的退出代码,这根本不是我要找的。
Are there easy hacks to achieving what I amlooking for?
是否有简单的技巧来实现我正在寻找的东西?
回答by Laurence Gonsalves
If you're sure status will only be one line, you could do something like this with sed:
如果您确定 status 只有一行,则可以使用 sed 执行以下操作:
status=$(echo "$status" | sed -e 's:^foo$:bar:' -e 's:^baz$:buh:')
You may also be able to get something to work with bash's built-in substitution. This almostworks (I don't know of any way to get exact matching only):
您也可以使用 bash 的内置替换来获得一些东西。这几乎有效(我不知道有什么方法可以只获得完全匹配):
status=${status/foo/bar}
status=${status/baz/buh}
If your goal is just to be more "functional" (and not to make your code more typo-proof), you could do this:
如果您的目标只是更“实用”(而不是让您的代码更能防止打字错误),您可以这样做:
status=$(
case "$status" in
("foo") echo "bar" ;;
("baz") echo "buh" ;;
(*) echo "$status" ;;
esac)
Though honestly, bash is probably one of the worst languages to try and be functional in. It was really designed with a more imperative mindset, as illustrated by the fact that you can't easily compose expressions. See in the second code snippet how I had to break it into two separate statements? If bash were designed to be functional you'd be able to write something like this instead:
虽然老实说,bash 可能是最糟糕的语言之一,但它可能是最糟糕的语言之一。它的设计确实具有更加命令式的思维方式,正如您无法轻松组合表达式这一事实所说明的那样。在第二个代码片段中看到我如何将它分成两个单独的语句?如果 bash 被设计为功能性的,你就可以写这样的东西:
status=${{status/baz/buh}/foo/bar}
But that does notwork.
但是,这并不能正常工作。
I'd suggest only using bash for simpler scripts, and for more complicated stuff use something like Python or Ruby. They'll let you write more functional code without having to constantly wrestle with the language.
我建议只将 bash 用于更简单的脚本,而对于更复杂的东西,使用 Python 或 Ruby 之类的东西。它们会让您编写更多功能性代码,而不必经常与语言搏斗。
回答by ghostdog74
status="baz"
status=$(case $status in
"foo") echo "bar" ;;
"baz") echo "buh" ;;
*) echo $status ;;
esac)
echo "status: $status"
output
输出
$ ./shell.sh
status: buh
回答by Paused until further notice.
Bash 4 has associative arrays:
Bash 4 有关联数组:
# setup
unset vals i
indices=(foo baz)
val=(bar buh)
declare -A vals # associative
for index in ${indices[@]}
do
vals[$index]=${val[i++]}
done
$ # demos
$ status="foo"
$ status=${vals:-$status}
$ echo $status
bar
$ status="not found"
$ status=${vals:-$status}
$ echo $status
not found