More Syntax Abuse

Jul 26, 2005 13:31

> for f in `perl -e '$, = " "; print split /:/, $ENV{PATH};'`; do find $f | grep 'somecmd'; done

In other news, for those of you for whom the above is not complete gibberish. There has got to be a better way to do this. All I am trying to do is find all the commands in my path with a certain substring. One might think this would be a job for ( Read more... )

Leave a comment

Comments 14

jibb July 26 2005, 18:18:16 UTC
for d in `echo $PATH | tr : ' '`; do ls $d | grep 'somecmd'; done

Reply

iluvsheep July 26 2005, 20:38:50 UTC
Stay all in one language will you! But, yes, well done, many characters shorter. Too short in fact!

Actually, it is, by two characters. Your change uses ls which only will return the name of the actual command, but I want the full path, which find so conveniently provides.

Er ... well done, ol' bean!

Reply

derakon July 26 2005, 21:03:01 UTC
I just did some searching to see if ls can do full pathnames. Apparently it can't. How bizarre. find really should be overkill for this kind of thing; I'm sad that it isn't.

Reply

jibb July 27 2005, 20:33:24 UTC

Well, actually only 1 character too short. Replace ls with find, but then replace "' '" with "\ "...

for d in `echo $PATH | tr : \ `; do find $d | grep 'somecmd'; done [66 chars]

But really, must we use echo and tr to extract the paths in $PATH (and waste 10 characters)? No!

for d in ${PATH//:/ }; do find $d | grep 'somecmd'; done [56 chars]

But there are shorter solutions yet! To start, there's inferno0069's solution, which can have the variable dir trimmed to d to get it to...

zsh -c 'for d in $path; find $d | grep "somecmd"' [49 chars]

Can this be beat? If you don't mind running the command as root, or ignoring the "Permission denied" errors on stderr, we can just let find search the whole filesystem!

find / -regex "^\(${PATH//:/\|}\)/.*somecmd.*" [46 chars]

And that's as small as I've gotten, unless you resort to removing whitespace as if you were trying to fail CS 70. Then, replace "-regex" with "| grep" (same number of characters), and then you can trim off two pesky spaces.

find /|grep "^\(${PATH//:/\|}\)/.*somecmd.*" [44 chars]

... )

Reply


inferno0069 July 27 2005, 01:14:37 UTC
zsh -c 'for dir in $path ; find $dir | grep "somecmd"'

Reply

iluvsheep July 27 2005, 02:31:39 UTC
You know, the more I hear about the 1337ness of zsh, the more I think it is what I should be using. Out of curiousity, how does it know to split on colons in $path?

Reply

inferno0069 July 28 2005, 16:54:26 UTC
The path variable is an array that's kept in sync with the PATH variable. Arrays just expand that way. It might not work if you have spaces in some directory name, though. (I had forgotten about that, it just doesn't happen!)

Reply

jibb July 27 2005, 20:36:18 UTC
I came up with shorter ways, but only by a 2-3 characters, and quite a bit of work. I should check out this zsh.

Reply


Leave a comment

Up