Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: AWK if else statment on conky

  1. #1
    Join Date
    Oct 2007
    Location
    Portugal
    Beans
    155
    Distro
    Ubuntu 20.04 Focal Fossa

    AWK if else statment on conky

    Hi there guys!

    Since ubuntu 20.04 brings us Gamemode included, why not make conky work for us?
    So, i added this to my conky:

    Code:
    ${exec gamemoded -s |awk '{print $3}' }
    Final result, it will print "active" or "inactive" on my setup. Now, i want to bring this to the next level! Instead of printing the word itself, i want it to print ON or OFF accordingly.
    I have no experience in scripting, i'm still learning, and i'm not getting the if statment to work!

    I tried this:

    Code:
    gamemoded -s |awk 'if($3 =="active")' print "On" else 'if($3 =="inactive")' print "off"
    However its not working :s. can anyone help?
    Light a candle for the sinners...set the world on fire...

  2. #2
    Join Date
    Apr 2008
    Location
    Southern California, USA
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: AWK if else statment on conky

    I tested your awk statement:
    Code:
    $ echo on;awk 'if($3 =="active")' print "On" else 'if($3 =="inactive")' print "off"
    on
    awk: cmd. line:1: if($3 =="active")
    awk: cmd. line:1: ^ syntax error
    Mine:
    Code:
    $ echo on;awk '{if($3 =="active") print "On"; else if($3 =="inactive") print "off"}'
    on
    Appears missing "{}" blocks

  3. #3
    Join Date
    Oct 2007
    Location
    Portugal
    Beans
    155
    Distro
    Ubuntu 20.04 Focal Fossa

    Re: AWK if else statment on conky

    Hi! thanks for your help!
    Indeed it works, however it disables too the other lines bellow for my conky setup. i wonder why?

    Code:
    ${color2}Game Mode:${color} ${alignr}${exec gamemoded -s |awk '{if($3 =="active") print "On"; else if($3 =="inactive") print "off"}'
    Edit: all working! thanks!!
    Last edited by UpSignal; May 19th, 2020 at 04:20 PM.
    Light a candle for the sinners...set the world on fire...

  4. #4
    Join Date
    Oct 2007
    Location
    Portugal
    Beans
    155
    Distro
    Ubuntu 20.04 Focal Fossa

    Re: AWK if else statment on conky

    Heym can i ask just one more question?
    in that line of code, how can i change the color of each print ?

    i'm trying to change the "off" color, but it doesnt work. tried the code before the print and after the print, no joy.

    Code:
    ${color2}Game Mode:${color} ${alignr}${exec gamemoded -s |awk '{if($3 =="active") print "On"; else if($3 =="inactive") print ${color5} "off"}'
    Last edited by UpSignal; May 19th, 2020 at 06:09 PM.
    Light a candle for the sinners...set the world on fire...

  5. #5
    Join Date
    Jan 2006
    Location
    Sunny Southend-on-Sea
    Beans
    8,414
    Distro
    Kubuntu 20.04 Focal Fossa

    Re: AWK if else statment on conky

    Quote Originally Posted by UpSignal View Post
    Heym can i ask just one more question?
    in that line of code, how can i change the color of each print ?
    For this specific case, you can move things around "if [exec awk blah] = active [color blah] else [other color blah]"

    The way I do it is to shunt the conditional stuff into a lua script (I use them for colouring temperatures and fan speeds). I'll dig it out next time I'm near my computer if you're interested.
    None but ourselves can free our minds

  6. #6
    Join Date
    Oct 2007
    Location
    Portugal
    Beans
    155
    Distro
    Ubuntu 20.04 Focal Fossa

    Re: AWK if else statment on conky

    Sure, i would love too!
    i'll try to figure it out, thanks man
    Light a candle for the sinners...set the world on fire...

  7. #7
    Join Date
    Jan 2006
    Location
    Sunny Southend-on-Sea
    Beans
    8,414
    Distro
    Kubuntu 20.04 Focal Fossa

    Re: AWK if else statment on conky

    So the way conky works with lua is that you need to declare your lua script in the conky configuration file, and I keep my conky scripts in the amazingly creatively-named ~/.conky/scripts, so my conky has a line in the header section that says

    Code:
        lua_load = '~/.conky/scripts/conky.lua',
    and in my conky lua script (called conky.lua) there's a function called conky_context_colour that takes an expression, three colours, and two numerical values as arguments, and returns the results of the expression coloured based on its value relative to those other values. So, for example, I can call
    Code:
    ${lua_parse context_colour ${hwmon 2 temp 1} ${color4} ${color2} ${color3} 45 75}${color}°C
    to display my CPU temperature as one colour if it's less than 45°C, another colour if it's more than 75°C, and a third if it's between them. I use the colorn entries in the header section, but I could have different colours for each call of context_colour if I wanted.

    The first part of conky.lua is a boilerplate section. I don't really understand exactly what it does, but it's
    Code:
    require 'cairo'
    
    function conky_main()
    if conky_window == nil then return end
        local cs = cairo_xlib_surface_create(conky_window.display, conky_window.drawable, conky_window.visual, conky_window.width, conky_window.height)
        cr = cairo_create(cs)
        cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE)
        local updates=tonumber(conky_parse('${updates}'))
        if updates>5 then
    
        end-- if updates>5
        cairo_destroy(cr)
        cairo_surface_destroy(cs)
        cr=nil
    end-- end main function
    The actual context_colour function is

    Code:
    function conky_context_colour(    expression,
                    colour1,
                    colour2,
                    colour3,
                    low_value,
                    high_value)
        local foo=conky_parse(expression)
        local bar=tonumber(foo)
        if bar < tonumber(low_value)
            then colour=colour1
            else
            if bar > tonumber(high_value)
                then colour=colour3
                else colour=colour2
            end
        end
    return colour..foo
    end
    I have that function in a lua script rather than doing it in conky because I didn't want to have to keep evaluating the expression over and over, and the nested ifs make the conky config really hard to read. You might notice that the function call from conky names the function context_colour, but in the script it's named conky_context_colour. I can't remember why that is, but it needs to be like that for some reason.

    Obviously this function is dealing with numerical values (you can see that I'm using tonumber() to make sure it's a number before I compare the values) but you can amend it to compare strings. Having already used a script for all the temperatures and fan speeds, I then had somewhere to put other random functions that do similar things, so the displayed information about the mpd that's running on my raspberry pi stereo gets a different image if it's playing or paused, and my laptop shows the signal strength of its WiFi graphically. It's all quite fun once you get started.
    None but ourselves can free our minds

  8. #8
    Join Date
    Oct 2007
    Location
    Portugal
    Beans
    155
    Distro
    Ubuntu 20.04 Focal Fossa

    Re: AWK if else statment on conky

    Hi there!

    Thanks a lot for sharing your configs, no doubt i have much to learn from there. the only experience i have with scripting is from changing values and watch what happens, and search google for codes and try to find their meaning, so its hard for me.
    I'm going to spend all day trying to figure out how to make that work with my code.
    If i get it to work, i'll come here
    cheers
    Light a candle for the sinners...set the world on fire...

  9. #9
    Join Date
    Oct 2007
    Location
    Portugal
    Beans
    155
    Distro
    Ubuntu 20.04 Focal Fossa

    Re: AWK if else statment on conky

    Ah.. i think i'll have to pass this one. It's just too hard for my skills (no skills ).
    The lua file is too much for me to handle. I also tried changing my command around, starting with the if condition, but just made a giant mess. I was trying to set a green colored "ON" when the result from |awk '{if($3 =="active"), and set the default color "OFF" when the result is inactive. Well, thanks anyway guys
    Light a candle for the sinners...set the world on fire...

  10. #10
    Join Date
    Jan 2006
    Location
    Sunny Southend-on-Sea
    Beans
    8,414
    Distro
    Kubuntu 20.04 Focal Fossa

    Re: AWK if else statment on conky

    Quote Originally Posted by UpSignal View Post
    the only experience i have with scripting is from changing values and watch what happens, and search google for codes and try to find their meaning, so its hard for me.
    I mean, that's how I did it, too.

    Quote Originally Posted by UpSignal View Post
    Ah.. i think i'll have to pass this one. It's just too hard for my skills (no skills ).
    The lua file is too much for me to handle.
    There are accessible guides around for lua scripting specifically with conky. It was a long time ago that I set this up, so I couldn't tell you what I read at the time. Maybe an example that compares text strings would help?

    Code:
    function conky_mpd_image(    image1,
                    image2,
                    image3,
                    positionx,
                    positiony)
        local status=conky_parse('${mpd_status}')
        if status == "Playing"
            then picture=image1
            else
            if status == "Paused"
                then picture=image2
                else picture=image3
            end
        end
        output="${image ~/.conky/images/"..picture.." -p "..positionx..","..positiony.."}"
    return output
    end
    I also tried changing my command around, starting with the if condition, but just made a giant mess. I was trying to set a green colored "ON" when the result from |awk '{if($3 =="active"), and set the default color "OFF" when the result is inactive. Well, thanks anyway guys
    I don't have any conky configurations with ifs in any more to crib from (because they got put in lua instead), but conky can do if and else on its own. You can see all the functions here. So you could do something like

    Code:
    ${if_match "${exec gamemoded -s | awk '{print $3;}'}" = "active"}${color5}ON${color}${else}OFF${endif}
    I haven't tested it, so there may be something up with it as written, but that's the kind of thing.
    None but ourselves can free our minds

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •