Link to home
Start Free TrialLog in
Avatar of ramble
rambleFlag for United States of America

asked on

Basic piping | question maybe?

I'd like to change to the directory automatically from the output of the which command.

Example:

$ pwd
/user/home/Ramble
$ which ls
/bin/ls
$ cd /bin

Is there a way to combined the last 2 commands (efficiently) like:

$which ls | cd

Avatar of Nisus091197
Nisus091197

Hi,

This will do it for you.  It works in Bourne, Korn and bash shells.

cd `dirname \`which ls\` `

Regards, Nisus
http://www.omnimodo.com

Avatar of ramble

ASKER

Your command works in csh too.

Ok...that works.  But I'd still like it to be as "efficient" as just:

which ls
cd /bin

Ofcouse, the longer the directory, the more efficient your command seems to be  :^)

A more general "modification" of my original question...

How could I take the output of a command, and use it as the input of another....like:

find . -print | grep -i \\.pl | grep -v \\.pl\\/ | cat <each line of the output>

or

ps -ef | grep -i aprocess | awk '{print $2}' | kill -9 <the process ID that was outputted>

or

which uname | ls -ls <uname file result>

or...well, you get the picture...right?  The last command would have to work on each individual line of the last outputted command.  Anyway, this is probably beyond the scope (although somewhat related) of the original question, and ofcourse, points will be adjusted.  But as I started thinking about it...I thought it'd be something cool to be able to do  :^)  
ASKER CERTIFIED SOLUTION
Avatar of Nisus091197
Nisus091197

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ramble

ASKER

Cool!  Thanks
my pleasure.

happy scripting!!!
Avatar of ramble

ASKER


Thanks_ramble
Avatar of ramble

ASKER

Thanks_ramble

Just Ignore the last comment