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

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]

And that's as far as I could shrink things down.

Sadly, locate only seems to use basic regular expressions, and thus doesn't support alternations. But if it did, and if you didn't mind your results being slightly out of date, we could take advantage of the fact that "find /" and "locate" are the same length, but locate uses -r rather than -regex for regular expressions to bring things down even further. Sadly, it doesn't work.

locate -r "^\(${PATH//:/\|}\)/.*somecmd.*" [42 chars]

And although other solutions are shorter, I found this solution rather pleasing.

sh -Oc nullglob "echo {${PATH//:/,}}/*somecmd*" | tr \ \\n [59 chars]

Reply

iluvsheep July 27 2005, 23:21:25 UTC
*stunned look*

Umm ... yes ... well done! I hereby proclaim you champion of obscure shell syntax. Let the heralds be sent forth to the four corners of this LJ, proclaiming Jibb to be the one true master of completely arcane and incomprehensible shell expressions. Truly it is he who is understood by the shell and no one else!

(parse that last line as you will)

Reply

iluvsheep July 27 2005, 23:22:01 UTC
And, out of morbid curiousity, how long did you spend on this?

Reply

jibb July 27 2005, 23:46:21 UTC
An hour or two last night. Many man pages and online guides to shell scripting were consulted. I think I've got a much better idea of how to do shell-scripty things now.

And on a largely unrelated note, I finally finished that black roof climb at Hangar 18 that you introduced me to. So fun!

Reply

jibb July 28 2005, 23:01:38 UTC
Really, just now? I could have sworn that you can finished that one off with many other monkey-like feats.

At any rate, well done you. I know I was quite please (and sore!) the first time that I managed that climb. I was surprised at how technically easy it is. The tough things about it turn out to be the mad arm endurance required and the sheer terror of climbing along upside-down for 30 ft or so. Actually, the latter was the real sticking point with me. For the longest time I had the strength and endurance but just couldn't bring myself to do it. Actually, the day I ended up completing the roof, I was pretty sleep-depped and probably not thinking very clearly.

Reply

iluvsheep July 29 2005, 14:48:42 UTC
I case you hadn't figured it out, that was me

Reply

inferno0069 July 28 2005, 16:52:46 UTC
I must mention that, if you use zsh already, as I do, and remove some whitespace, the zsh way is

for d in $path;find $d|grep "somecmd" [37 chars]

Reply


Leave a comment

Up