haideralipunjabi / polybar-kdeconnect

KDEConnect module for Polybar
https://blog.haideralipunjabi.com/posts/making-modules-for-polybar-shell-python/
164 stars 11 forks source link

Script variables are off by one #7

Open fuzxi opened 5 years ago

fuzxi commented 5 years ago

Hi,

The module didn't work out of the box. The menu would show up, but none of the options did anything. I could still receive pings from my paired device and send pings via shell commands.

I took a look at the code and all the numbered bash variables ($1, $2, etc.) are off by one., e.g. the script uses $2 for the deviceid variable instead of $3. I believe this is because in line 41: devices+="%{A1:. $DIR/polybar-kdeconnect.sh; show_menu $devicename $deviceid $battery:}$icon%{A}$SEPERATOR" $DIR/polybar-kdeconnect.sh is being treated as a variable.

I'm not sure why this is, or how to escape it properly. My workaround was to increment all the numbered variables, but I'd be happy to know a better way to fix it.

haideralipunjabi commented 5 years ago

I don't understand why you think deviceid should be $3 instead of $2. $0 is the function name, i.e show_menu here $1 is devicename $2 is deviceid $3 is battery.

If the name and battery status appear in the menu, that means $1 and $3 are working properly.

fuzxi commented 5 years ago

Because with the default settings, the battery status shows the device ID for me. Additionally, the other options didn't work at all until I replaced each $2 with $3.

haideralipunjabi commented 5 years ago

That is weird behaviour. I will do some tests and get back to you

haideralipunjabi commented 5 years ago

Can you provide me with info about the shell you are using? I am not extremely knowledgeable about shell behaviours but I made the following test script

#!/usr/bin/env bash
func () {
echo $0 $1 $2 $3
}

and executed

. ./script.sh; func "arg1" "arg2" "arg3"

and got the following output

func arg1 arg2 arg3

which confirms that at least on my shell (zsh, and previously bash) $0 is the name of the function, $1 is the first argument and so on. Can you test it and check what output you get?

fuzxi commented 5 years ago

I'm using zsh. Per #6, shebangs don't seem to make any difference on my system, so I just symlinked /bin/bash to /bin/sh.

Running ./script.sh; func "arg1" "arg2" "arg3" in zsh returns func arg1 arg2 arg3, but so does simply inputting func "arg1" "arg2" "arg3".

Running . ./test.sh; func "arg1" "arg2" "arg3" in bash returns bash arg1 arg2 arg3.

haideralipunjabi commented 5 years ago

In any case, in the test script, $1 is arg1. This same behaviour should be happening in the main script as well. I still have no ideas on why this is happening. I will continue working on it. Edit: Since the output of the test script is correct, can you run the main script in shell and check how the variables work there?

haideralipunjabi commented 5 years ago

I have modified the script to use getopts. Hopefully it fixes the issue.