Code runs fine in shell but fails in xmobar

I hope this is the right subreddit for this and you guys can help me out. I have a script that runs in xmobar that I wrote in order to display my internet connection status. If I'm plugged in it'll display Wired and if I'm connected to wifi it will display the SSID along with some bars that indicate how strong the connection is. For instance my wifi is called petrel and I'm sitting next to the router so it looks like this: MyWifi ▂▄▆█ When I run this in my shell (zsh, also works in bash though) it outputs fine. However, when I tell xmobar to execute the program it spits an error onto my bar Could not execute command /home/ben/bin/wireless

It mysteriously doesn't work! Here's the code being run:

#!/bin/python
import re
from plumbum import local

sudo = local['sudo']
nmclid = sudo['nmcli', 'd']()
nmclidwifi = sudo['nmcli', 'd', 'wifi']()


def grepLine(pattern, lines):
    for line in lines.split('\n'):
        search = re.search(pattern, line)
        if search:
            return line

wired = grepLine("ethernet", nmclid)

if "connected" in wired:
    print("Wired")
else:
    ssid = grepLine("connected", nmclid)
    ssid = re.search("connected\s+\S+", ssid).group()
    ssid = re.search("\s+\S+", ssid).group()
    ssid = re.search("\S+", ssid).group()

    bars = grepLine("\*\s+" + ssid, nmclidwifi)
    bars = re.search("[_▂▄▆█]+", bars).group()

    print(ssid + " " + bars)

If I have an ethernet cord plugged in it prints Wired just fine and there are no issues, which narrows the problem down to that last else clause. Why would xmobar fail here when my shell doesn't? Does xmobar not use some form of exec?

edit. I think I've narrowed it down to the command

sudo nmcli d wifi

although why that fails is beyond me, considering

sudo nmcli d

works perfectly

Posted Aug 5, 2015, 4:51 AM UTC

25
BCMM•

When you run it in your shell, how do you run it? python wireless, or just ./wireless?

I ask because having python installed at /bin/python is a little unusual - I would expect /usr/bin/python on most distros. (Scripts often use #!/usr/bin/env python for portability.)

1
azy2OP•

Oh oops!! You're totally right. I have been running it with ./wireless and it's working, which is weird cause I'm pretty sure I don't have python there. But I wrote this in bash originally and parts of the script works and parts don't so that can't be the entire problem, y'know?

1
BCMM•

I wonder how that actually runs...

Does /bin/python exist on your system?

It might also be interesting to try adding

import sys
print(sys.version)

to the script, just to find out what is actually executing it.

1
azy2OP•

aw, /bin is actually a sim-link to /usr/bin

1
BCMM•

Aw, I forgot about the /usr merge. Sorry!

1