Bash – using modified file content as arguments

bashmercurialxargs

In a script I'd like to purge a mercurial repository but be able to retain a number of (configurable) file patterns which I read from $FILENAME. The syntax for the hg command is

hg purge --all --exclude PATTERN1 --exclude PATTERN2 ...

Thus if $FILENAME contains a list of file patterns (one pattern per line), each pattern must be prepended by an "–exclude " in order to construct the command line

My current approach is to use for construction of the argument list

grep -v -E "^[[:blank:]]*$" $FILENAME | sed "s/^/--exclude /g" | xargs echo
which also will skip empty lines and those which only contain tabs or spaces which would result in an error if used to construct the above command line. Thus in total:

hg purge --all `grep -v -E "^[[:blank:]]*$" $FILENAME | sed "s/^/--exclude /g" | xargs echo`

Is there a nicer way, maybe with some xargs arguments which I'm unaware of?

Best Answer

Seems there is even a shorthand way in mercurial itself, making use of file lists (suggested by mg in #mercurial):
hg purge --all --exclude "listfile:$FILENAME"