(clarification and aside about quoting and scripting 8-) )
Robert White
Thu Jul 17 19:50:15 2014
On 07/17/2014 01:15 AM, Roy Marples wrote:
I was running into a stupid integration problem with gentoo and the
startup scripts. There was no way to pass the empty script value --
e.g. "" -- reliably through the openrc system.
-- does not mean an empty value, -- means "options stop here"
"" or '' are empty values in the shell.
I was using the double-minus as the English language em-dash to set
apart the "e.g." phrase, not the programmatic optargs(3) feature.
That is, if you are writing a sentence with examples -- for instance
this phrase -- the em-dash (originally m-dash or "middle-length dash")
is preferred over the parenthetical, which is generally more properly
used for citations. [meta for the win. 8-)]
[ASIDE: the shell doesn't actually care about -- when parsing words,
it's the individual programs argument parsers that do or don't treat a
double minus as coercing the end of arguments.]
My problem was that the startup script contains
command_args=(whatever)
And it was just a guess about quoting depth to get the empty argument
through the various command line expansions implicit in that system.
so once neither of the following worked
command_args="--script ''"
command_args='--script ""'
I didn't want to get into the how-many-backslashes-do-I-need game. 8-)
So I went with
command_args="--script /dev/null"
Which finally worked.
====
I despise that OpenRC -- the gentoo boot script system -- has abandoned
bash. The bash scripting system has an _unbeatable_ means for the
step-wise construction of a command line. Using arrays and array
expansion completely removes this issue with simple elegance.
DHCP_COMMAND=( dhcpcd )
DHCP_COMMAND+=( --script "" )
DHCP_COMMAND+=( --background )
DHCP_COMMAND+=( ${EXPANDED_TERM_NOW} )
DHCP_COMMAND+=( '${EXPAND_LATER_AT_INVOCATION}' )
# unknown, possibly variable, number of additions here
# then invoke by expansion
"${DHCP_COMMAND[@]}"
The elements of the array are only evaluated during their
actual append, and when the final command is invoked -- the expansions
of which is in double-quotes to re-quote the elements that may have
spaces or empty values -- empty arguments and delayed expansions do the
right thing at the predictable time. All without nested backslashes and
otherwise quoted quotes.
That is stable compared to the typical and problematic string munging
done in the other interpreters.
DHCP_COMMAND="$DHCP_COMMAND --script ''"
DHCP_COMMAND="$DHCP_COMMAND --background"
and you don't have to guess at whitespace like using += on strings,
which can get super tricky as well.
I use the array concatenation and expansion command thing in lots of
scripts, particularly those with "if then else fi" dynamic composition.
I've never seen anybody else use it except for a couple of people I
showed it to, but I don't imagine I'm the first person to think to use
it that way. 8-)
I particularly like using it to shim other commands while easily passing
through invocation options. I do this for my QEMU virtual machine
startup scripts so that I can easily tack on invocation time things
(like attaching the CD drive when doing maintenance.
e.g.
...
QEMU_COMMAND+=( "$@" )
...
invoked as "XP_Instance.bash -cdrom /dev/sr0" only when I need the drive.
-- Rob
Archive administrator: postmaster@marples.name