The Unix shell usually does a good job of doing what you expect it to do. Writing shell scripts is usually pretty straight-forward. Yes, sometimes you can go crazy quoting special characters, but for most simple file maintenance, it's not too bad.
I *think* I've used the "eval" function before today, but I can't remember why. I am confident that I haven't used it more than twice, if that many. But today I was doing something that seemed like it shouldn't be too hard, but I don't think you can do it without "eval".
RSYNC
I want to use "rsync" to synchronize some source files between hosts. But I don't want to transfer object files. So my rsync command looks somewhat like this:
rsync -a --exclude "*.o" my_src_dir/ orion:my_src_dir
The double quotes around "*.o" are necessary because you don't want the shell to expand it, you want the actual string *.o to be passed to rsync, and rsync will do the file globbing. The double quotes prevents file glob expansion. And the shell strips the double quotes from the parameter. So what rsync sees is:
rsync -a --exclude *.o my_src_dir/ orion:my_src_dir
This is what rsync expects, so all is good.
PUT EXCLUDE OPTIONS IN A SYMBOL: FAIL
For various reasons, I wanted to be able to override that exclusion option. So I tried this:
EXCL='--exclude *.o' # default
... # code that might change EXCL
rsync -a $EXCL my_src_dir/ orion:my_src_dir
But this doesn't work right. The symbol "EXCL" will contain the string "--exclude *.o", but when the shell substitutes it into the rsync line, it then performs file globbing, and the "*.o" gets expanded to a list of files. For example, rsync might see:
rsync -a --exclude a.o b.o c.o my_src_dir/ orion:my_src_dir
The "--exclude" option only expects a single file specification.
SECOND TRY: FAIL
So maybe I can enclose $EXCL in double quotes:
rsync -a "$EXCL" my_src_dir/ orion:my_src_dir
This passes "--exclude *.o" as a *single* parameter. But rsync expects "--exclude" and the file spec to be two parameters, so it doesn't work either.
THIRD TRY: FAIL
Finally, maybe I can force quotes inside the EXCL symbol:
EXCL='--exclude "*.o"' # default
... # code that might change EXCL
rsync -a $EXCL my_src_dir/ orion:my_src_dir
This almost works, but what rsync sees is:
rsync -a --exclude "*.o" my_src_dir/ orion:my_src_dir
It thinks the double quotes are part of the file name, so it won't exclude the intended files.
EVAL TO THE RESCUE
The solution is to use eval:
EXCL='--exclude "*.o"' # default
... # code that might change EXCL
eval "rsync -a $EXCL my_src_dir/ orion:my_src_dir"
The shell does symbol substitution, so this is what eval sees:
rsync -a --exclude "*.o" my_src_dir/ orion:my_src_dir
And eval will re-process that string, including stripping the double quotes, so this is what rsync sees:
rsync -a --exclude *.o my_src_dir/ orion:my_src_dir
which is exactly correct.
P.S. - if anybody knows of a better way to do this, let me know!
EDIT: The great Sahir (one of my favorite engineers) pointed out a shell feature that I didn't know about:;
Did you consider setting noglob? It will prevent the shell from expanding '*'. Something like:
EXCL='--exclude *.o' # default
set -o noglob
rsync -a $EXCL my_src_dir/ orion:my_src_dir
set +o noglob
I absolutely did not know about noglob! In some ways, I like it better. The goal is to pass the actual star character as a parameter, and symbol substitution is getting in the way. Explicitly setting noglob says, "hey shell, I want to pass a string without you globbing it up." I like code that says exactly what you mean.
In contrast, my "eval" solution works fine, but the code does not make plain what my purpose was. I would need a comment that says, "using eval to prevent the shell from doing file substitution in the parameter string." And while that's fine, I much prefer code that better documents itself.
One limitation of using noglob is that you might have a command line where you want parts of it not globbed, but other parts globbed. The noglob basically operates on a full line. So you would need to do some additional string building magic to get the right things to be done at the right time. But the same thing would be true if you were using eval. Bottom line: the shell was made powerful and flexible, but powerful flexible things tend to have subtle corner cases that must be handled in non-obvious ways. No matter what, a comment might be nice.
2 comments:
Did you consider setting noglob? It will prevent the shell from expanding '*'. Something like:
EXCL='--exclude *.o' # default
set -o noglob
rsync -a $EXCL my_src_dir/ orion:my_src_dir
set +o noglob
There are fancier ways of doing it also, like saving the current value of noglob and restoring the saved value after...but it is enabled by default.
Wow, I sure sat on this comment for a long time. BAD ME!
I did not know about setting "noglob"! That's a good thing to keep in my back pocket. And I think that is a better solution to my rsync problem.
Thanks!
Post a Comment