Log in

WWWWolf's LifelessJournal
Random ramblings of a random wolf
Unix shell woes 
6th-Dec-2008 01:12 pm
Okay, I'm getting really scatterbrained.

Here I am, deeply concerned that the Rails developers will declare something obsolete and dead and buried while I'm not updating my stuff, and decide to finally fix my Rails app to use Rails 2.0 .html.erb file extensions rather than Rails 1.0 .rhtml.

Before I do the move properly, I decide to go experiment a little bit...

find app -name '*.rhtml' -exec echo `echo -ne '{}' | perl -pe 's/\.rhtml$/.html.erb/gi;'` \;

And guess what I get back? A list of files with .rhtml extension. NOT .html.erb.

In my n years of perl-fu, I have never seen something this simple blow up. I can't believe my frigging eyes. I replace '{}' with '{}.rhtml' and what do you know, I get a giant bunch of files with .rhtml.html.erb extension. Twiddling with the regex doesn't do a damn thing: .rhtml, Repeat the previous command with  > imgonnaopenthisshitwithgoddamnhexeditor.txt tacked in the end, and after doing as suggested, nothing is any more clearer.

How can I possibly fail at this???

...and after good 20 minutes I notice, oh, it's that goddamn it's-interpreting-that-{}-as-literal-except-when-it-isn't thing. Basically, echo -ne '{}' here somehow echoes, um, {} instead of the file name. Doesn't really matter if it's quoted or not, it somehow does it anyway. Perl somehow reads that in and the whole shell expression still manages to spit out the original file name somehow.

It's probably some part of a shell / find(1) weirdness that I haven't yet understood. Over 10 years and I still don't understand the subtleties of find -exec. *sigh*

So here's what actually works:

for i in `find app -name '*.rhtml'`; do git mv "$i" `echo -ne "$i" | perl -pe 's/\.rhtml$/.html.erb/gi;'`; done

Not as clever, but got the job done...
6th-Dec-2008 11:50 am (UTC)
Backticks get evaluated by the shell before the argument gets passed to find.
7th-Dec-2008 06:19 pm (UTC)

(A blindingly obvious fact, now that I think of it. Thanks for explaining it. =)
This page was loaded Feb 21st 2017, 5:09 am GMT.