?
Solved

Basic piping | question maybe?

Posted on 2003-02-20
7
Medium Priority
?
177 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
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
Course of the Month9 days, 20 hours left to enroll

762 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