how to: Expand the array example with a loop
This commit is contained in:
parent
9831c0bf52
commit
55fc9ed370
@ -115,24 +115,34 @@ The syntax is verbose, but get over it. This bashism is reason alone to drop pos
|
|||||||
|
|
||||||
Good:
|
Good:
|
||||||
|
|
||||||
array=(
|
files=(
|
||||||
a
|
a
|
||||||
b
|
b
|
||||||
)
|
)
|
||||||
array+=(c)
|
duplicates=()
|
||||||
if [ ${#array[@]} -gt 0 ]; then
|
for f in "${files[@]}"; do
|
||||||
rm -- "${array[@]}"
|
if cmp "$f" other/"$f"; then
|
||||||
|
duplicates+=("$f")
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
if [ "${#duplicates[@]}" -gt 0 ]; then
|
||||||
|
rm -- "${duplicates[@]}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
Bad:
|
Bad:
|
||||||
|
|
||||||
pseudoarray=" \
|
files=" \
|
||||||
a \
|
a \
|
||||||
b \
|
b \
|
||||||
"
|
"
|
||||||
pseudoarray="$pseudoarray c"
|
duplicates=
|
||||||
if ! [ "$pseudoarray" = '' ]; then
|
for f in $files; do
|
||||||
rm -- $pseudoarray
|
if cmp "$f" other/"$f"; then
|
||||||
|
duplicates+=" $f"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
if ! [ "$duplicates" = '' ]; then
|
||||||
|
rm -- $duplicates
|
||||||
fi
|
fi
|
||||||
|
|
||||||
Here is why arrays are such a basic feature for a shell: [Command arguments are fundamentally arrays](http://manpag.es/RHEL6/3p+exec)
|
Here is why arrays are such a basic feature for a shell: [Command arguments are fundamentally arrays](http://manpag.es/RHEL6/3p+exec)
|
||||||
|
Loading…
Reference in New Issue
Block a user