You are not logged in.

#1 2019-08-10 20:39:02

Abendstolz
Member
Registered: 2019-08-10
Posts: 4

Ion shell script not working via dmenu but fine otherwise

Hello!

While this is not the first shell script I wrote, I am certainly far from being an expert, so please bear with me.

Today I wrote a tiny script to allow me to switch my 4k monitor to full hd and vice versa.
The script is written for the ion shell (https://github.com/redox-os/ion)

#!/usr/bin/env ion
let current_resolution = $(swaymsg -t get_outputs | jq '.[] | select(.current_mode.width==3840) | .rect | .width') 

if eq $current_resolution 1920
		echo "We are at 2k resolution! Switching to 4k!"
		swaymsg -t command "output DP-1 pos 0 0 res 3840x2160 scale 1"
		swaymsg -t command "output DP-2 pos 3840 0 res 1920x1200"
else
		echo "We are at 4k resolution! Switching to 2k!"
		swaymsg -t command "output DP-1 pos 0 0 res 3840x2160 scale 2"
		swaymsg -t command "output DP-2 pos 1920 0 res 1920x1200"
end

It resides in my script folder is named "42k" and works like a charm if invoked manually via terminal.
When I run it via dmenu though, nothing happens but dmenu does return.

If I run it via terminal like so

dmenu | 42k

it is invoked and does it's job but doesn't return.

What I tried so far (being a noob at shell programming):

* Renaming 42k to not start with a number (just in case there is some rule regarding dmenu I missed)
* Adding return statements to my script

I assume I made an obvious mistake that I just miss, hence I ask for help

PS: @Mods I saw the Dmenu Hacking Thread but felt like it was the wrong place to discuss such a specific (ion, sway) problem. If I was wrong, I apologize and would kindly ask for this topic to be closed and my post being ported over.

Last edited by Abendstolz (2019-08-10 20:39:38)

Offline

#2 2019-08-10 21:00:34

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,452
Website

Re: Ion shell script not working via dmenu but fine otherwise

How do you launch dmenu?  Is it from a key binding, or from directly from the terminal emulator?  If you haven't yet, try running dmenu from a terminal emulator, then 42k from there.

You say this script is in your "script folder".  Where is that?  When does it get added to the PATH variable?  I'd bet it is added to the path after sway starts (e.g., in a shellrc file) thus it is available for running 42k from the terminal, but it is not available to dmenu when dmenu is run from a key binding (but then in turn should be when dmenu is launched from a terminal).


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2019-08-11 09:12:21

Abendstolz
Member
Registered: 2019-08-10
Posts: 4

Re: Ion shell script not working via dmenu but fine otherwise

Trilby wrote:

How do you launch dmenu?  Is it from a key binding, or from directly from the terminal emulator?  If you haven't yet, try running dmenu from a terminal emulator, then 42k from there.

You say this script is in your "script folder".  Where is that?  When does it get added to the PATH variable?  I'd bet it is added to the path after sway starts (e.g., in a shellrc file) thus it is available for running 42k from the terminal, but it is not available to dmenu when dmenu is run from a key binding (but then in turn should be when dmenu is launched from a terminal).

Hey, thanks for taking the time to answer!

I launch via keybinding (sway $mod+d)

The scripts-folder (

~/scripts

) is added to the path in my ion initrc like so:

export PATH="${PATH}:$HOME/.cargo/bin:$HOME/scripts:"

As ion is my default shell and I start sway from tty sway should indeed have access to this $PATH.

Also other scripts in this very folder work just fine when being invoked from dmenu (via shortcut)

Also note that dmenu does FIND the script (and it is only searching in $PATH afaik) so it's visible to it, just nothing happens.

Trilby wrote:

If you haven't yet, try running dmenu from a terminal emulator, then 42k from there.

I ran

dmenu | 42k

from my alacritty terminal emulator and my default shell ion - is that what you're asking for?

Offline

#4 2019-08-11 12:45:21

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,452
Website

Re: Ion shell script not working via dmenu but fine otherwise

Abendstolz wrote:

I ran

dmenu | 42k

from my alacritty terminal emulator and my default shell ion - is that what you're asking for?

No.  Honestly I ignored that from your first post as it looks like gibberish to me.  What do you expect that command to do?  That will run a normal dmenu instance, then pass whatever you select in dmenu to the stdin of 42k - I have no idea what to expect as a result of this.

Just run dmenu from a terminal emulator:

dmenu

Then in dmenu, type `42k`.

Given what you've said about the path variable being set earlier, I now suspect this will still not work, but it's worth confirming.

If it fails, try logging the output from 42k.  Start dmenu, the type `42k > ~/42k.log 2>&1`.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#5 2019-08-11 20:21:39

Abendstolz
Member
Registered: 2019-08-10
Posts: 4

Re: Ion shell script not working via dmenu but fine otherwise

I am sorry!

Of course you're totally right about

dmenu | 42k

It is gibberish.

One interesting note though: it does not open a dmenu and allow me to select anything and pass it to 42k.

In fact, what it does is:

dmenu | 42k
We are at 4k resolution! Switching to 2k!

The resolution changes (so the script is executed right away and not waiting for any input from dmenu)
Then the terminal is stuck, waiting.

But dmenu did not open like it does with dmenu_run.

I've tried running the command you've requested and nothing happens. No file is being created, the terminal is just stuck waiting until I abort.

I've rewritten the script in "plain shell" and this one works right away in dmenu and everywhere:

#!/usr/bin/sh 

current_resolution="$(swaymsg -t get_outputs | jq '.[] | select(.current_mode.width==3840) | .rect | .width')"

if [ "$current_resolution" -eq "1920" ]; then
		echo "We are at 2k resolution! Switching to 4k!"
		swaymsg -t command "output DP-1 pos 0 0 res 3840x2160 scale 1"
		swaymsg -t command "output DP-2 pos 3840 0 res 1920x1200"
else
		echo "We are at 4k resolution! Switching to 2k!"
		swaymsg -t command "output DP-1 pos 0 0 res 3840x2160 scale 2"
		swaymsg -t command "output DP-2 pos 1920 0 res 1920x1200"
		sway reload
fi

I am still curious what's causing the wrong behaviour with the ion script and dmenu though.

Last edited by Abendstolz (2019-08-11 20:30:03)

Offline

#6 2019-08-11 20:33:26

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,452
Website

Re: Ion shell script not working via dmenu but fine otherwise

Ah, well that's progress.  Do you have any other ion scripts that work properly when run from dmenu?  Or could you create a simple one to test.

You could also check whether the shebang matters `#!/usr/bin/env ion` vs `#!/usr/bin/ion` (assuming ion is under /usr/bin).


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#7 2019-08-23 21:16:43

Abendstolz
Member
Registered: 2019-08-10
Posts: 4

Re: Ion shell script not working via dmenu but fine otherwise

#!/usr/bin/env ion

vs.

#!/usr/bin/ion

makes no difference.

#!/usr/bin/env ion

swaynag -t warning -m 'This is a test' -b 'Abort the test' 'echo nope'

works just fine so I gradually increased the complexity again:

#!/usr/bin/env ion

let current_resolution = $(swaymsg -t get_outputs | jq '.[] | select(.current_mode.width==3840) | .rect | .width') 
swaynag -t warning -m 'This is a test' -b 'Abort the test' 'echo nope'

worked just fine as well - the problems began once I added

if eq $current_resolution 1920

Still curious what is different when dmenu executes the script

Offline

Board footer

Powered by FluxBB