I do understand the following script is installing the packages but what I don't understand is what packages:
for package in ${d[@]};
do
rpm -ivh --quiet ${!package} >/dev/null 2>&1
What is ${d[@]}
?
${d[@]}
means all elements of array d
.
It also means that whoever wrote that script didn't write it defensively, as they carelessly failed to double-quote it. The for loop should be written as:
for package in "${d[@]}";
so that each element of d
is expanded as a separate word (i.e. argument to the for loop), no matter what it contains.
Without the double-quotes, the elements of d
are subject to normal shell word-splitting and expansion (e.g. an element with a space in it will be treated as two arguments to the for loop, not one). See Why does my shell script choke on whitespace or other special characters? for more detailed explanations.
This might be OK in the context (i.e. if d
contains only package names without spaces or other shell meta-characters) but is a bad habit for any shell programmer to indulge.
From man bash
, under the section heading Arrays
:
Any element of an array may be referenced using
${name[subscript]}
. The braces are required to avoid conflicts with pathname expansion.If subscript is
@
or*
, the word expands to all members of name. These subscripts differ only when the word appears within double quotes.If the word is double-quoted,
${name[*]}
expands to a single word with the value of each array member separated by the first character of the IFS special variable, and${name[@]}
expands each element of name to a separate word. When there are no array members,${name[@]}
expands to nothing.
(extra linefeeds to improve readability and some bolding for emphasis added by me)
!
in the${!package}
. I do understand!
is a negation. But in this case negation of package, but why. I don't get that. – user75464 Sep 09 '19 at 08:11a=10
andpackage=a
thenecho ${!package}
would output10
. IMO it's probably a typo (and they meant just${package}
) because indirection does not seem to make any sense in this for loop. – cas Sep 09 '19 at 08:33${!package}
but doesn't know basic things like "quote your variables" then IMO they're being too clever by half, or are trying to obscure what the script does, or don't really know what they're doing and shouldn't be trusted to write scripts meant to be run as root. or all of the above or worse. – cas Sep 09 '19 at 08:39