LDP/LDP/guide/docbook/abs-guide/array-assign.bash

89 lines
2.5 KiB
Bash

#! /bin/bash
# array-assign.bash
# Array operations are Bash-specific,
#+ hence the ".bash" in the script name.
# Copyright (c) Michael S. Zick, 2003, All rights reserved.
# License: Unrestricted reuse in any form, for any purpose.
# Version: $ID$
#
# Clarification and additional comments by William Park.
# Based on an example provided by Stephane Chazelas
#+ which appeared in an earlier version of the
#+ Advanced Bash Scripting Guide.
# Output format of the 'times' command:
# User CPU <space> System CPU
# User CPU of dead children <space> System CPU of dead children
# Bash has two versions of assigning all elements of an array
#+ to a new array variable.
# Both drop 'null reference' elements
#+ in Bash versions 2.04 and later.
# An additional array assignment that maintains the relationship of
#+ [subscript]=value for arrays may be added to newer versions.
# Constructs a large array using an internal command,
#+ but anything creating an array of several thousand elements
#+ will do just fine.
declare -a bigOne=( /dev/* ) # All the files in /dev . . .
echo
echo 'Conditions: Unquoted, default IFS, All-Elements-Of'
echo "Number of elements in array is ${#bigOne[@]}"
# set -vx
echo
echo '- - testing: =( ${array[@]} ) - -'
times
declare -a bigTwo=( ${bigOne[@]} )
# Note parens: ^ ^
times
echo
echo '- - testing: =${array[@]} - -'
times
declare -a bigThree=${bigOne[@]}
# No parentheses this time.
times
# Comparing the numbers shows that the second form, pointed out
#+ by Stephane Chazelas, is faster.
#
# As William Park explains:
#+ The bigTwo array assigned element by element (because of parentheses),
#+ whereas bigThree assigned as a single string.
# So, in essence, you have:
# bigTwo=( [0]="..." [1]="..." [2]="..." ... )
# bigThree=( [0]="... ... ..." )
#
# Verify this by: echo ${bigTwo[0]}
# echo ${bigThree[0]}
# I will continue to use the first form in my example descriptions
#+ because I think it is a better illustration of what is happening.
# The reusable portions of my examples will actual contain
#+ the second form where appropriate because of the speedup.
# MSZ: Sorry about that earlier oversight folks.
# Note:
# ----
# The "declare -a" statements in lines 32 and 44
#+ are not strictly necessary, since it is implicit
#+ in the Array=( ... ) assignment form.
# However, eliminating these declarations slows down
#+ the execution of the following sections of the script.
# Try it, and see.
exit 0