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/
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.
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
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:
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?
There are 10 alternatives - vote for the best!
If you can do better, submit your command here.
You must be signed in to comment.
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]ameTo avoid messing around with strings, I opted for
grep [^]]nameLines 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=-ieLine 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