fish shell 可以在 if 语句中使用通配符吗?
Can fish shell do wildcard globbing in an if statement?
我无法理解是否可以在 fish 的 if 语句中使用通配符。 switch/case 按预期工作:
# 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
语句中的变量进行通配符通配?我做错了吗?我无法在 fish 文档中找到告诉我这两种方式的示例。
注意:我不是在问检查 $OSTYPE
鱼。 I know there are better ways to do that。我的问题严格限于是否可以在 fish 的 if
语句中进行通配符匹配。
没有
像你说的那样使用switch
,或者像
那样使用string
内置函数
if string match -q 'darwin*' -- "$OSTYPE"
if
并不重要 - 在您的示例中您是 运行 的命令是 [
,它是 test
的替代名称,它是内置文档位于 http://fishshell.com/docs/current/commands.html#test(或 man test
或 help test
)。
我无法理解是否可以在 fish 的 if 语句中使用通配符。 switch/case 按预期工作:
# 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
语句中的变量进行通配符通配?我做错了吗?我无法在 fish 文档中找到告诉我这两种方式的示例。
注意:我不是在问检查 $OSTYPE
鱼。 I know there are better ways to do that。我的问题严格限于是否可以在 fish 的 if
语句中进行通配符匹配。
没有
像你说的那样使用switch
,或者像
string
内置函数
if string match -q 'darwin*' -- "$OSTYPE"
if
并不重要 - 在您的示例中您是 运行 的命令是 [
,它是 test
的替代名称,它是内置文档位于 http://fishshell.com/docs/current/commands.html#test(或 man test
或 help test
)。