Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Possible Duplicate:
What is the easiest way to execute text from tail at the command line?

I was guessing the ouput of a command that fails to execute should be sent to stdout and hence can be piped. It seems I am wrong, so please correct me.

For example,

$easy_install
The program 'easy_install' is currently not installed.  You can install it by typing:
sudo apt-get install python-setuptools

So if I want to install with the command suggested, I tried

$easy_install | grep sudo | bash

i.e naively trying to pick out the last line and send it to the shell as a command. I even tried

$easy_install | tail -1 | bash

but get the same blurb without it doing anything. What am I doing wrong?

share|improve this question

marked as duplicate by jasonwryan, Michael Mrozek Apr 9 '12 at 4:09

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
What are you trying to accomplish? Just sudo apt-get install python-setuptools and be done with it. –  jw013 Apr 8 '12 at 4:00
    
@jw013 true. But I just want to know how to pipe the output of such commands for perhaps a more productive use in the future. –  kuch nahi Apr 8 '12 at 4:03
    
Type man xargs to get an idea. –  Keith Apr 8 '12 at 9:51

2 Answers 2

up vote 2 down vote accepted

The easiest way I can think of, since the line in question is a command, is:

`$easy_install 2>&1 | grep sudo`

The backticks or $(…) take the output of a command pipe and execute it as if you typed it, returning the output.

Please note that this command won't work if you're missing sudo and trying to install it. But since this is obviously Ubuntu, sudo is usually available. To avoid this, you might want to try your second choice:

`$easy_install 2>&1 | tail -n 1`

Try and wean yourself out of using tail -1, it's being replaced by the standard form tail -n 1. I find this hard myself, but I don't like deprecation warnings. :)

Warning: if $easy_install exists and you don't get this type of output, either of these commands are a massive security risk. You can end up executing arbitrary things. You can protect yourself by being more extravagant:

`$easy_install 2>&1 >/dev/null | grep '^sudo apt-get install'`

This discards stdout, and will only run anything starting with sudo apt-get install which limits things nicely, but is considerably more annoying than just typing sudo apt-get install $package yourself.

share|improve this answer

Error output typically goes to standard error, not standard output; you can catch that by adding 2>&1.

$ easy_install 2>&1 | grep sudo

That said, they're using a shell function to do that and I suspect a minor change to that function would allow it to ask you and then do it itself.

share|improve this answer

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