I’m working with a utility (unison, but that’s not the point) that accepts parameters like:
$ unison -path path1 -path path2 -path path3
I would like to write a sh script that I could run like this:
$ myscript path1 path2 path3
I’m hoping for a Posix compliant solution, but bash-specific would also be good.
I’m guessing it should be something like:
#!/bin/sh
unison ${*/ / -path }
But this doesn’t work.
EDIT: OK, I think I got something:
#!/bin/bash
PARAMS=
for arg in "$@"
do
PARAMS+=" -path '$arg'"
done
unison $PARAMS
The problems are this only works in bash, and I’m pretty sure there’s a better way to quote the parameters.
Unchecked, it could be as simple as:
If you don’t embed spaces in your path names, then you can deal with a variable number of arguments with:
If you have spaces in your path names, then you have to work a lot harder; I usually use a custom program called
escape, which quotes arguments that need quoting, andeval:I note that using Perl or Python would make handling arguments with spaces in them easier – but the question asks about shell.
It might also be feasible in Bash to use a shell array variable – build up the arguments into an array and pass the array as the arguments to the
unisoncommand.