0

I wrote a script for resizing windows, which require orientation and value in form of fraction, like so:

resize.sh -h 1/2

and it works as expected. I also added -k flag, which means that script require user input, like so:

resize.sh -k -h

and in the script:

read -rsn 2 fraction

which I parse to get values for numerator and denominator.

This works great from command line, but idea behind this is to bind resize.sh -k -h to some key combination, and pass following two keys as input. But when I run script from keyboard, it run as a background process which is not associated with any tty, so read could not get its input. Is there any way to redirect global input to background process, after running it from keyboard.

What I tried so far:

  • Redirection to /proc/$$/fd/0, which didn't work.

  • Redirectiong currently active tty stdin to read, like so:
    read -rsn 2 fraction < /dev/pts/0

which actually worked, but problem is that not all windows are terminal, e.g. web browser.

If my question is unclear, please feel free to ask for additional clarifications or details, and thanks in advance :)

5
  • How are you binding the key combination? If you do it as a readline macro, it should just insert the command into the command line, and execute it in the foreground. Commented Sep 5, 2018 at 17:44
  • I define bindig in ./config/openbox/rc.xml, this way I can run it on any window, not just inside terminal. Commented Sep 5, 2018 at 18:02
  • What you need to do is have it display a dialogue window to get input. It doesn't make sense to try to read from the terminal -- you could have multiple terminal windows, or maybe not any terminals at all. Commented Sep 5, 2018 at 19:14
  • Exactly, that's why I need to redirect global stdin, independent of terminal or any window. If I bind script to e.g. Ctrl-h, I need next two keys to be sent to script. So, for example, key sequence should look like this: Ctrl-h 1 2, where 1 and 2 would be sent to script as input arguments. Is that even possible? Commented Sep 5, 2018 at 19:31
  • There is no "global stdin" in a windowing environment. If you need to do this in a GUI environment, you need to display a dialog window. There are shell utilities for doing this. Commented Sep 5, 2018 at 19:36

2 Answers 2

1

You can use a named pipe for the process communication. I made am example script where the background proces is a function.

#!/bin/bash
pipe_name=/tmp/mypipe$$
mkfifo "${pipe_name}"

resize()
{
   read fraction < "${pipe_name}"
   echo "Resize window to fraction=${fraction}"
}

resize &
read -p "Enter your fraction: "
echo "${REPLY}" > "${pipe_name}"

rm "${pipe_name}"
Sign up to request clarification or add additional context in comments.

Comments

0

thank you both for providing very useful information. The solution is combination of both, actually.

First I modified read command in resize.sh to get input from named pipe, as Walter suggested, than I wrote a new, kinda "wrapper" script, which executes resize.sh in background, and than, since Barmar pointed I need a gui window, it starts very small terminal window running read and passing input to named pipe. Further more, using wmctrl I manage to place small terminal window right where currently active window begins, and hide it below (thanks to openbox per-application properties), so it's technically not visible at all :)

It's really too hacky for my liking, but it was really the only option I could think of at this moment, so until I find the better way, this gets the job done. Once again, thank you both for directing me toward solution, I really appreciate it, cheers :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.