tags:

views:

136

answers:

3

I have a bash script that creates a Subversion patch file for the current directory. I want to modify it to zip the produced file, if -z is given as an argument to the script.

Here's the relevant part:

zipped=''
zipcommand='>'

if [ "$1" = "-z" ]
then
   zipped='zipped '
   filename="${filename}.zip"
   zipcommand='| zip >'
fi

echo "Creating ${zipped}patch file $filename..."

svn diff $zipcommand $filename

This doesn't work because it passes the | or > contained in $zipcommand as an argument to svn.

I can easily work around this, but the question is whether it's ever possible to use these kinds of operators when they're contained in variables.

Thanks!

+5  A: 

I would do something like this (use bash -c or eval):

zipped=''
zipcommand='>'

if [ "$1" = "-z" ]
then
   zipped='zipped '
   filename="${filename}.zip"
   zipcommand='| zip -@'
fi

echo "Creating ${zipped}patch file $filename..."

eval "svn diff $zipcommand $filename"
# this also works: 
# bash -c "svn diff $zipcommand $filename"

This appears to work, but my version of zip (Mac OS X) required that i change the line:

zipcommand='| zip -@'

to

zipcommand='| zip - - >'

Edit: incorporated @DanielBungert's suggestion to use eval

Keeth
Thanks!And yeah, I had the wrong zip arguments originally, as I had adapted this from a script that piped the files given by "svn status" into "zip -@" which reads filenames from stdin.
Owen
+2  A: 

eval is what you are looking for.

# eval 'printf "foo\nbar" | grep bar'
bar

Be careful with quote characters on that.

Daniel Bungert
A: 

Or you should try zsh shell whic allows to define global aliases, e.g.:

alias -g L='| less'
alias -g S='| sort'
alias -g U='| uniq -c'

Then use this command (which is somewhat cryptic for the ones who took a look from behind ;-) )

./somecommand.sh S U L

HTH

Zsolt Botykai