Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Script variables are off by one #7

Open
fuzxi opened this issue Apr 23, 2019 · 7 comments
Open

Script variables are off by one #7

fuzxi opened this issue Apr 23, 2019 · 7 comments

Comments

@fuzxi
Copy link

fuzxi commented Apr 23, 2019

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
Copy link
Owner

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
Copy link
Author

fuzxi commented Apr 24, 2019

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
Copy link
Owner

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

@haideralipunjabi
Copy link
Owner

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
Copy link
Author

fuzxi commented Apr 25, 2019

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
Copy link
Owner

haideralipunjabi commented Apr 26, 2019

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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants