简体   繁体   English

鱼壳可以在if语句中进行通配符修饰吗?

[英]Can fish shell do wildcard globbing in an if statement?

I'm having trouble understanding whether I can use wildcard globbing in an if statement in fish. 我很难理解我是否可以在fish的if语句中使用通配符。 This switch/case works as expected: 此开关/外壳按预期工作:

# correctly echos macOS on macOS
switch "$OSTYPE"
    case 'darwin*'
        echo 'macOS'
    case '*'
        echo 'not macOS'
end

However, I cannot get an if statement version of the same thing to work. 但是,我无法获得相同内容的if语句版本。

# doesn't work - prints 'not macOS' on macOS
if [ "$OSTYPE" = 'darwin*' ]
    echo 'macOS'
else
    echo 'not macOS'
end

In zsh/bash you can do something like this: 在zsh / bash中,您可以执行以下操作:

[[ $OSTYPE == darwin* ]] && echo 'macOS' || echo 'not macOS'

Or, more verbosely, 或者,更详细地说,

if [[ $OSTYPE == darwin* ]]
  then echo 'macOS'
  else echo 'not macOS'
fi

My question is, does fish support wildcard globbing against a variable in if statements? 我的问题是,fish是否支持在if语句中对变量使用通配符? Am I doing this wrong? 我做错了吗? I cannot find an example in the fish docs that tells me either way. 我在fish文档中找不到一个可以告诉我任何一种方法的示例。

NOTE : I'm not asking about checking $OSTYPE in fish. 注意 :我不是在询问在鱼中检查$OSTYPE问题。 I know there are better ways to do that . 我知道有更好的方法可以做到这一点 My question is limited strictly to whether it is possible to do wildcard globbing in an if statement in fish. 我的问题严格限于在if语句中是否有可能在通配符中进行通配符修饰。

No. 没有。

Use switch like you said, or the string builtin like 使用您所说的switch或内置的string

if string match -q 'darwin*' -- "$OSTYPE"

The if isn't important - the command you are running in your example is [ , which is an alternative name for test , which is a builtin with documentation at http://fishshell.com/docs/current/commands.html#test (or man test or help test ). if并不重要-在示例中运行的命令是[ ,这是test的替代名称,它是内置的文档,位于http://fishshell.com/docs/current/commands.html#test (或man testhelp test )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM