1

I have a plugin that generates and copies to the clipboard a ready-to-paste command. For now I open a new terminal, paste the command and execute it. I want to write a script that automatically opens a new terminal and runs that command using the clipboard as input.

I tried to use xclip but can't figure out how to pipe its output to gnome-terminal, and all of the threads I saw focus on the opposite problem: saving command output to clipboard which is NOT what I want to do.

What I tried: xclip -se c -o | gnome-terminal -e

It says "Failed to parse arguments: Missing argument for -e". So at the very least I have a syntax problem.

I have a feeling that I might not be approaching this problem the right way. Complete noob here, so the more details the better!

Thanks!

1 Answers1

1

It seems like gnome-terminal can't take input from stdin, so you have to use command substitution:

gnome-terminal -e "$(xclip -se c -o)"

Also, if you want to perform word splitting and globbing on the clipboard contents before it's run, you can use this:

gnome-terminal -x $(xclip -se c -o)

If you need to hold the terminal open when the command completes, you could use this:

gnome-terminal -x bash -c "$(xclip -se c -o); read -p 'Press Enter to close.'"

Or if you want to go to an interactive prompt:

gnome-terminal -x bash -c "$(xclip -se c -o); bash"

For more ideas, see With a launcher for a terminal application, how can I keep the terminal open after the program is complete?

wjandrea
  • 14,236
  • 4
  • 48
  • 98
  • Note that gnome-terminal closes instantly once command you run exits, so one would have to spaw extra shell if one wants to keep gnome-terminal open and interactive – Sergiy Kolodyazhnyy Nov 02 '17 at 20:54
  • 1
    @SergiyKolodyazhnyy Oh yeah, I forgot I have mine set to stay open when the process exits. I would solve that with gnome-terminal -x bash -c "$(xclip -s c -o); read -p 'Press Enter to close.'" – wjandrea Nov 02 '17 at 21:15
  • OP, let me know if this works for you, since the details may change depending on what command you want to run, and how you want to run it. – wjandrea Nov 02 '17 at 21:28
  • Thanks @wjandrea, it works perfectly! For now I'm using gnome-terminal --tab-with-profile=PROFILENAME --working-directory="/PATH/" -x bash -c "$(xclip -s c -o); bash" to keep the command prompt active, I might also use your very convenient read -p 'Press Enter to close.' – MonkeyBack Nov 02 '17 at 21:36
  • Sorry I just saw that I can't edit after 5 minutes... So gnome-terminal --tab-with-profile=PROFILENAME --working-directory="/PATH/" -x bash -c "$(xclip -s c -o); bash" is fine for now, but I originally setup that specific profile to not close after command exits. However it displays a yellow header "The child process exited normally with status 0." that masks the first lines of output. I switched it back to closing after command exits and I'm using your ; read -p 'Press Enter to close.' which is more elegant and suited to my needs considering the command I'm running. – MonkeyBack Nov 02 '17 at 21:49
  • @MonkeyBack OK, great! I'll add that to the answer, then. BTW, you should be able to scroll up to see the output covered by the yellow header. – wjandrea Nov 02 '17 at 21:58
  • @MonkeyBack Also, while testing, I noticed that xclip -s is not the same as xclip -selection. It seems to always get the contents of the primary selection, regardless of what argument you give it. – wjandrea Nov 02 '17 at 22:48
  • @wjandrea Oh nice catch! I was wondering why the command gnome-terminal -x bash -c "$(xclip -se c -o); read -p 'Press Enter to close.'" was working fine when entered from a terminal, but did not work when executed from a script. Changing it to gnome-terminal -x bash -c "$(xclip -selection clipboard -o); read -p 'Press Enter to close.'" solved it so thanks again! Edit: xclip -se c -o also works fine from the script. – MonkeyBack Nov 02 '17 at 22:56