我很难理解是否可以在fish中的if语句中使用通配符。此开关/情况按预期工作:
# correctly echos macOS on macOS
switch "$OSTYPE"
case 'darwin*'
echo 'macOS'
case '*'
echo 'not macOS'
end但是,我无法获得相同事物的if语句版本。
# doesn't work - prints 'not macOS' on macOS
if [ "$OSTYPE" = 'darwin*' ]
echo 'macOS'
else
echo 'not macOS'
end在zsh/bash中,您可以这样做:
[[ $OSTYPE == darwin* ]] && echo 'macOS' || echo 'not macOS'
或者,更详细地说,
if [[ $OSTYPE == darwin* ]]
then echo 'macOS'
else echo 'not macOS'
fi我的问题是,fish是否支持针对if语句中的变量进行通配符珠化?我做错了吗?我在鱼的文档中找不到任何一个例子来告诉我。
注意:__:我不是在问检查鱼的$OSTYPE。https://stackoverflow.com/questions/26258244/is-there-a-way-to-detect-os-in-fish-shell-akin-to-the-ostype-variable-in-bash。我的问题严格地限制在fish中是否可以在if语句中进行通配符珠化。
发布于 2018-11-14 17:37:41
不是的。
像你说的那样使用switch,或者使用string内置的
if string match -q 'darwin*' -- "$OSTYPE"if并不重要-在您的示例中运行的命令是[,它是test的另一个名称,它是http://fishshell.com/docs/current/commands.html#test (或man test或help test)文档的内置。
https://stackoverflow.com/questions/53305808
复制相似问题