📄 array-append.bash
字号:
#! /bin/bash# array-append.bash# Copyright (c) Michael S. Zick, 2003, All rights reserved.# License: Unrestricted reuse in any form, for any purpose.# Version: $ID$## Slightly modified in formatting by M.C.# Array operations are Bash-specific.# Legacy UNIX /bin/sh lacks equivalents.# Pipe the output of this script to 'more'#+ so it doesn't scroll off the terminal.# Subscript packed.declare -a array1=( zero1 one1 two1 )# Subscript sparse ([1] is not defined).declare -a array2=( [0]=zero2 [2]=two2 [3]=three2 )echoecho '- Confirm that the array is really subscript sparse. -'echo "Number of elements: 4" # Hard-coded for illustration.for (( i = 0 ; i < 4 ; i++ ))do echo "Element [$i]: ${array2[$i]}"done# See also the more general code example in basics-reviewed.bash.declare -a dest# Combine (append) two arrays into a third array.echoecho 'Conditions: Unquoted, default IFS, All-Elements-Of operator'echo '- Undefined elements not present, subscripts not maintained. -'# # The undefined elements do not exist; they are not being dropped.dest=( ${array1[@]} ${array2[@]} )# dest=${array1[@]}${array2[@]} # Strange results, possibly a bug.# Now, list the result.echoecho '- - Testing Array Append - -'cnt=${#dest[@]}echo "Number of elements: $cnt"for (( i = 0 ; i < cnt ; i++ ))do echo "Element [$i]: ${dest[$i]}"done# Assign an array to a single array element (twice).dest[0]=${array1[@]}dest[1]=${array2[@]}# List the result.echoecho '- - Testing modified array - -'cnt=${#dest[@]}echo "Number of elements: $cnt"for (( i = 0 ; i < cnt ; i++ ))do echo "Element [$i]: ${dest[$i]}"done# Examine the modified second element.echoecho '- - Reassign and list second element - -'declare -a subArray=${dest[1]}cnt=${#subArray[@]}echo "Number of elements: $cnt"for (( i = 0 ; i < cnt ; i++ ))do echo "Element [$i]: ${subArray[$i]}"done# The assignment of an entire array to a single element#+ of another array using the '=${ ... }' array assignment#+ has converted the array being assigned into a string,#+ with the elements separated by a space (the first character of IFS).# If the original elements didn't contain whitespace . . .# If the original array isn't subscript sparse . . .# Then we could get the original array structure back again.# Restore from the modified second element.echoecho '- - Listing restored element - -'declare -a subArray=( ${dest[1]} )cnt=${#subArray[@]}echo "Number of elements: $cnt"for (( i = 0 ; i < cnt ; i++ ))do echo "Element [$i]: ${subArray[$i]}"doneecho '- - Do not depend on this behavior. - -'echo '- - This behavior is subject to change - -'echo '- - in versions of Bash newer than version 2.05b - -'# MSZ: Sorry about any earlier confusion folks.exit 0
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -