?
Solved

Basic piping | question maybe?

Posted on 2003-02-20
7
Medium Priority
?
179 Views
Last Modified: 2013-12-27
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

0
Comment
Question by:ramble
  • 4
  • 3
7 Comments
 
LVL 5

Expert Comment

by:Nisus091197
ID: 7989267
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

0
 

Author Comment

by:ramble
ID: 7989599
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  :^)  
0
 
LVL 5

Accepted Solution

by:
Nisus091197 earned 200 total points
ID: 7997514
Hi,

The way I usually process results as in the first two examples is with a for loop, e.g.:

for i in `ps -ef | grep -i aprocess | awk '{print $2}'`; do
  kill $i
done

or ...

ps -ef | grep -i aprocess | awk '{print $2}' | xargs kill -9

the 3rd example you give:

ls -ls `which uname`

There are many ways to skin a rabbit as they say.  Mine suit me and over the years I change them back and forth.

My website has a link to lots of my scripts, feel free to browse.

Regards, Nisus
http://www.omnimodo.com
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:ramble
ID: 7998765
Cool!  Thanks
0
 
LVL 5

Expert Comment

by:Nisus091197
ID: 8002597
my pleasure.

happy scripting!!!
0
 

Author Comment

by:ramble
ID: 10716860

Thanks_ramble
0
 

Author Comment

by:ramble
ID: 10893467
Thanks_ramble

Just Ignore the last comment
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question