[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Using terminal output as input



On Thu, May 14, 2009 at 10:55:35AM EDT, Dotan Cohen wrote:
> I am using a Debian-based distro (Ubuntu). Often I need to use the
> output of one terminal command as the input for another. A classic
> example is the  which command:
> $ which firefox
> /usr/bin/firefox
> $
> 
> Now, I would like to use that output as input, to start firefox. Other
> than manually typing it in, is there a way for the user to use the
> output directly?
> 
> Another example is when the OS lets the user know that she needs to
> install a program and gives her the command to install it:
> $ ekiga
> The program 'ekiga' is currently not installed.  You can install it by typing:
> sudo apt-get install ekiga
> bash: ekiga: command not found
> $

Off-hand, the only thing that comes to mind is using "history -s" to add
the command's output to the bash session's history list so that you can
retrieve it via Ctrl-P (or up-arrow) and use the readline editor to
"extract" the actual command:

$ history -s $(ekiga) 

To make things a bit more useable, maybe there's a way to bind a key
combo to a bash function that would do the above for _any_ command:

$ ekiga + Ctrl-whatever 

.. would execute:

$ add-output-to-history ekiga

With the bash function coded something like:

add-output-to-history () {history -s $($*)}      # UNTESTED !!

Since this ugly hack would seriously pollute the bash session's history
list, you would need to write a program/script that runs automaticaly
when exiting the bash session and removes all the crap before it gets
appended to the ~/.bash_history file.

> In contrast to the "which" example, the text that the user needs is
> buried in the output. Is there a way to use it anyway, without
> retyping (and without using the mouse, which I often do not have).

For stuff like that where the output is totally unpredictable, I doubt
anything beats the flexibility of gnu/screen's copy/paste mechanism.

:-)

CJ


Reply to: