如何测试命令的结果是否包含鱼壳中的字符串? [英] How to test if result of a command contains a string in fish shell?

查看:62
本文介绍了如何测试命令的结果是否包含鱼壳中的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个简短的函数,以允许我从命令行打开和关闭 wemo 灯.基本上我有一个命令,如果我输入 wemo status 将返回 Switch: Lights 1 如果灯打开或 0 如果它们关闭.我想编写一个基本上可以让我切换它们的鱼函数:

I'm trying to write a brief function to allow me to toggle wemo lights on and off from the command line. Basically I have a command that if i type wemo status will return either Switch: Lights 1 if the lights are on or 0 if they are off. I'd like to write a fish function that essentially lets me toggle them:

function lights --description 'Toggle lights'
    if contains (wemo status) "Lights 1"
        wemo switch "Lights" off
    else
        wemo switch "Lights" on
    end
end

虽然这行不通.我在想父母可能会进行文本替换?任何人都知道如何测试一个字符串是否包含 Fish 中的另一个字符串?

Though this doesn't work. I'm thinking that the parens probably do a textual replacement? Anyone know how I can test if a string contains another string in Fish?

推荐答案

所以我最终解决了以下问题:

So I ended up fixing this with the following:

# Toggle lights
function lights --description "Toggle Wemo Lights"
    set -l wemo (wemo status)
    switch $wemo
        case '*1'
            wemo switch "Lights" off
        case '*0'
            wemo switch "Lights" on
    end
end

这篇关于如何测试命令的结果是否包含鱼壳中的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆