Created
September 1, 2011 15:12
-
-
Save nrocy/1186377 to your computer and use it in GitHub Desktop.
Why doesn't this work?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this doesn't work | |
find . -type f -iname '*rhtml' -exec echo git mv {} `echo {} | sed 's/rhtml$/html.erb/i'` \; | |
# output | |
.. | |
git mv ./pictures/_form.rhtml ./pictures/_form.rhtml | |
.. | |
# but this works | |
find . -type f -iname '*rhtml' | while read i; do echo git mv $i `echo $i | sed 's/rhtml$/html.erb/i'`; done | |
# output | |
.. | |
git mv ./pictures/_form.rhtml ./pictures/_form.html.erb | |
.. |
Here's why it doesn't work..
- The backticks are evaluated by the shell beforehand.
echo {} outputs literal {}
- "{}" won't match the sed regex "rhtml$" so no substitution takes place
This makes the -exec block: echo git mv {} {}
- Find runs and replaces the {}s with filenames
Output: git mv ./pictures/_form.rhtml ./pictures/_form.rhtml
I believe the problem is that the backticked expression is evaluated by the shell before the find command is run.
Why you gotta type so quickly?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bizarrely the following works, in so far as the manually added trailing 'rhtml' is replaced properly.
find . -type f -iname '*rhtml' -exec echo git mv {}
echo {}rhtml | sed 's/rhtml$/html.erb/i'
;Makes me suspect that echo {} isn't doing what I think it is..