[prog] getting parameters into scripts
John Clarke
johnc+linuxchix at kirriwa.net
Mon Mar 29 23:30:26 UTC 2010
On Mon, Mar 29, 2010 at 12:07:01PM +1000, Miriam English wrote:
Hi Miriam,
> head until this morning I hit on the idea of doing something like this:
>
> for i in $@; do
> eval "$i"
> done
[snip]
> Anybody know how to get parameters with spaces into those variables?
Well, this works the way you want, but only for parameters of the form
var=value:
for i in "$@"
do
i=`echo $i|sed -e 's/=\(.*\)/="\1"/'`
eval $i
done
It restores quotes around everything after the equals sign before the
eval is done. Note the quotes around $@, this is essential to get the
behaviour you want.
You could use bash's built-in pattern substitution if you want. Instead
of the sed command, use this:
i="${i/=/=\"}\""
(a bit ugly because bash doesn't support back-references, so the closing
quote has to be added after the pattern substitution)
If you wanted to only eval parameters of the form var=value, try this
(bash only):
shopt -s extglob
for i in "$@"
do
case $i in
+([[:word:]])=*)
i=`echo $i|sed -e 's/=\(.*\)/="\1"/'`
eval $i
;;
*)
# do whatever you want with other parameters here
;;
esac
done
(remove the shopt and replace "+([[:word:]])=*" with "*=*" if you need
to use sh).
> Or should I just give up on trying to be a mental contortionist? :)
Think of it as a learning experience :-)
John
--
> Cars _are_ hardware. If you can kick it, without it caring, it's hardware.
Funny, I always thought my boss fell into the category of "wetware."
-- Dave Buckles
More information about the Programming
mailing list