Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Hide

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

w/o pgrep/pkill

Terminal - w/o pgrep/pkill
_p(){ ps ax |grep $1 |sed '/grep.'"$1"'/d' |while read a;do printf ${a%% *}' ';printf "${a#* }" >&2;printf '\n';done;}
2012-04-01 19:46:19
User: argv
Functions: grep printf ps read sed
0
w/o pgrep/pkill

proc lister

usage: p

proc killer

usage: p patt [signal]

uses only ps, grep, sed, printf and kill

no need for pgrep/pkill (not part of early UNIX)

_p(){

ps ax \

|grep $1 \

|sed '

/grep.'"$1"'/d' \

|while read a;do

printf ${a%% *}' ';

printf "${a#* }" >&2;

printf '\n';

done;

}

p(){

case $# in

0)

ps ax |grep .|less -iE;

;;

1)

_p $1;

;;

[23])

_p $1 2>/dev/null \

|sed '/'"$2"'/!d;

s,.*,kill -'"${3-15}"' &,'|sh -v

;;

esac;

}

alas, can't get this under 255 chars.

flatcap?

Alternatives

There is 1 alternative - vote for the best!

Terminal - Alternatives

Know a better way?

If you can do better, submit your command here.

What others think

Challenge accepted.

After a bit of analysis (it wasn't a very good film I was watching), it occurs to me that you want three things:

ps

ps | grep

ps | grep | kill

I've tidied up the source and put it on pastebin:

(it's pretty crap that a code site like this can't handle code properly)

http://pastebin.com/p94bUi5P

Line 3: The old "grep -v grep"

The usual way around that is it use a regex.

This will match name, but not grep.

grep [n]ame

To avoid messing around with strings, I opted for

grep [^]]name

Lines 4,5,6: Very interesting, but that's a lot of characters in printf!

I thought about using sed to split up the ps output, then you don't need the while loop.

Line 14: What's that grep for? The only thing it /could/ do is filter out blank lines.

The ps output doesn't have any blanks, so I left out grep.

Line 14: less -iE. If you like those options, then you're better off setting the environment variable LESS

export LESS=-ie

Line 14,15 and 17,18 and 20,21. You don't need a ; at the end of the case options if you've got a ;; (saves a few characters).

Line 19: You accept 2 or 3 parameters, but it gets very confusing.

Line 20: ${3-15} I think you actually meant ${3:-15} i.e. use parameter 3 if it exists, otherwise default to the number 15 (SIGTERM).

Line 20: 2>/dev/null Because the _p() function isn't well thought out, this looks a bit ugly.

Line 20: sed. This is very strange. You seem to be performing a search on the process ids. If you only use 2 parameters, this doesn't do what you expect and in order to use 3 parameters you *must* give a search string. I dropped this from my version -- sorry, you need to decide what you want.

Finally, there's still whitespace that could be removed to save a few characters.

Also, everything can be put on one line, even the functions:

p() { }; q() { }

Also if you were desperate, you could rename _p to q which would save 3 chars!

My version works as yours does:

p pattern [signal]

It takes up 146 characters.

_x(){ ps ax;};_y(){ _x|grep [^]]$1;};_z(){ _y $1|sed 's/ .*//';};p(){ case $# in 0)_x|less;;1)_y $1;;2)X=$(_z $1);[ -n "$X" ]&&kill -$2 $X;;esac;}

I've put it on pastebin, in its expanded form.

http://pastebin.com/62CTTSZ6

Comment by flatcap 128 weeks and 5 days ago

Your point of view

You must be signed in to comment.

Related sites and podcasts