How to check if the result of a command contains a string in a fish shell?

I am trying to write a concise function that allows me to turn wemo highlights on and off from the command line. Basically, I have a command that, if I type wemo status, will return either Switch: Lights 1if the light is on, or 0 if they are off. I would like to write a fish function that essentially allows me to switch them:

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

Although this does not work. I think that perhaps the paranas are doing text replacement? Does anyone know how I can check if a string contains another string in Fish?

+4
source share
2 answers

So, I decided to do this as follows:

# 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
+2
source

contains , ,

set elems foo bar baz
contains bar $elems; and echo yep

, -:

contains "e f"   (printf "%s\n" "a b c" "d e f" "g h i"); and echo y; or echo n
contains "d e f" (printf "%s\n" "a b c" "d e f" "g h i"); and echo y; or echo n
n
y

switch .

+3

Source: https://habr.com/ru/post/1524771/


All Articles