bash scripting nightmare.
mwoehlke
mwoehlke@tibco.com
Thu Aug 10 19:49:00 GMT 2006
Dave Korn wrote:
> Hey all, who's any good at bash scripting?
>
> I've been trying for two-and-a-half hours now to make a fairly simple shell
> script work, and I've been through every imaginable combination of quoting,
> and nothing I've tried works. The bash FAQ doesn't have anything to say on
> the matter, nor could I figure it out from the Advanced Bash-Scripting Guide.
>
> I'm not a proud man, so I don't mind begging for help. The trouble is that
> I want to:
>
> a) declare a variable which contains the date in ANSI format, then
> b) concatenate that variable and a few others into another variable, then
> c) pass that other variable to a function, then finally
> d) invoke the command line specified in the variable within the function.
>
> Sounds easy, you think? Not quite. There's a catch: the date string
> contains a space in it. And I can't find any way at all to get that date
> string passed to the command line from within the function without that space
> breaking things totally. If I don't try and quote it, the time part of the
> datestamp is taken as a separate command line argument and cvs doesn't
> understand it. If I do try and quote it, bash mangles the variable into
> gibberish. Let me demonstrate:
> [snip examples]
It sounds like you want to read up on arrays and "$@" (quotes included).
Here is a guess:
> a) declare a variable which contains the date in ANSI format
MY_DATE="`data <format>`"
> b) concatenate that variable and a few others into another variable
Don't do this. Pass in multiple parameters instead (see below).
> c) pass that other variable to a function, then finally
myFunction "$MY_CMD" "${OPTIONS1[@]}" "$MY_DATE" $OPTIONS2
OPTIONS1 is an array that will be split and passed as positional
parameters such that each parameter ($2, $3, etc) is exactly an item in
the array. I.e. if the array is {foo, "bar none", cat}, then you will
get positional parameters e.g. $2=foo, $3="bar none", $4=cat. OPTIONS2
will be split as usual, i.e. each word will be a positional parameter.
Obviously, you could make OPTION2 an array, or OPTION1 flat, or omit one
or both, depending on your needs.
> d) invoke the command line specified in the variable in the function.
echo "Now running '$@':"
"$@"
If you need non-command-line parameters to the function I would pass
them in first, copy them to 'local's, and then 'shift'.
Does that help?
--
Matthew
vIMprove your life! Now on version 7!
More information about the Cygwin-talk
mailing list