Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?
You must be signed in to comment.
commandlinefu.com is the place to record those command-line gems that you return to again and again. 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.
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:
grep [n]ame
To avoid messing around with strings, I opted forgrep [^]]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 LESSexport 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