4

I have a shell script which is opening different terminals.

After opening the terminals, I manually start different applications in the terminals. I would like to automate this by adding text (e.g. bin/binfile) to the command lines of the terminals via the shell script. Important is, that I only want to enter the text, so that I still have to press enter in the terminals for starting the applications after executing the script. I don't want to execute the commands automatically. Is that possible?

1

Assuming you are using bash, you can get close by configuring a readline shortcut to output the command you want in a single keypress.

export INPUTRC=`mktemp`
trap "rm -f $INPUTRC" EXIT
echo '"\eOP": "my command"' > $INPUTRC
xterm

INPUTRC is an environment variable used by readline. It will try to open $INPUTRC and read configuration from it.

The readline configuration "\eOP": "my command" binds the macro my command to the F1 key (which in my terminals appears as the sequence \eOP where \e is the ASCII escape character).

Now, inside the xterm pressing F1 will yield my command.

The trap ensures that the temporary configuration file is cleaned up once the terminal is closed.

(The correct readline incantation for binding to F1 may vary, so you might need to use something other than \eOP. If you already have a ~/.inputrc then you might want to include that in the temporary file created.)

Edit: Regarding comments below, xfce4-terminal works for me, with

export INPUTRC=`mktemp`
trap "rm -f $INPUTRC" EXIT
echo '"\eOQ": "my command"' > $INPUTRC
xfce4-terminal

and using F2 rather than F1.

  • Thanks, that's close. Is there a possibility of doing that with xfce4? It works with xterm for me, but not with xfce4-terminal. – Simon Oct 2 '14 at 14:52
  • @mikemodanoxxx Xfce4-terminal grabs F1 to open its help. Either use a different function key (recommended) or change the key binding for help in the xfce4-terminal preferences. – Gilles 'SO- stop being evil' Oct 2 '14 at 20:50
  • I tried that, but it did not work. For example, I replace \eOP with \e0Q (F2). And changed xterm to xfce4-terminal. The F2 key again works with xterm, but not with xfce4. Any suggestions? – Simon Oct 6 '14 at 13:05
  • It's \eOQ, not \e0Q (letter-O, not digit-0) – J Bytheway Oct 14 '14 at 1:16
  • Sorry, that was a typo. It does not work with O either. I also have an additional question: Is it possible to add more than one command? So that different commands appear when pressing F1 or F2 for example? – Simon Oct 14 '14 at 9:41
0

Use a wrapper script:

waitk.sh:

#!/bin/sh
printf "$*"
read k
exec "$@"

then prefix it to your commands

waitk.sh ls -l
  • This is also almost good. The drawback here is, that i can only execute exactly the predefined command. Sometimes I have to add some arguments before pressing enter, which does not work here. – Simon Oct 6 '14 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.