Download Advanced Bash-scripting Guide
Transcript
Advanced Bash-Scripting Guide
Embedded arrays in combination with indirect references create some fascinating possibilities
Example 27-12. Embedded arrays and indirect references
#!/bin/bash
# embedded-arrays.sh
# Embedded arrays and indirect references.
# This script by Dennis Leeuw.
# Used with permission.
# Modified by document author.
ARRAY1=(
VAR1_1=value11
VAR1_2=value12
VAR1_3=value13
)
ARRAY2=(
VARIABLE="test"
STRING="VAR1=value1 VAR2=value2 VAR3=value3"
ARRAY21=${ARRAY1[*]}
)
# Embed ARRAY1 within this second array.
function print () {
OLD_IFS="$IFS"
IFS=$'\n'
# To print each array element
#+ on a separate line.
TEST1="ARRAY2[*]"
local ${!TEST1} # See what happens if you delete this line.
# Indirect reference.
# This makes the components of $TEST1
#+ accessible to this function.
# Let's see what we've got so far.
echo
echo "\$TEST1 = $TEST1"
# Just the name of the variable.
echo; echo
echo "{\$TEST1} = ${!TEST1}" # Contents of the variable.
# That's what an indirect
#+ reference does.
echo
echo "-------------------------------------------"; echo
echo
# Print variable
echo "Variable VARIABLE: $VARIABLE"
# Print a string element
IFS="$OLD_IFS"
TEST2="STRING[*]"
local ${!TEST2}
# Indirect reference (as above).
echo "String element VAR2: $VAR2 from STRING"
# Print an array element
TEST2="ARRAY21[*]"
Chapter 27. Arrays
445
Related documents
thesisPivot - University of New Mexico
BenchKit - CosyVerif
A Technical History of Apple`s Operating Systems
- HSR - Institutional Repository
Licensing Information User Manual
OpenCL™ Optimization Guide
The Alchemy System for Statistical Relational AI: User Manual
VLBA Pipeline: Outline of Data Reduction Heuristics 1 Introduction
USER`S MANUAL Price Computing Label Scale
CA du 13 novembre 2014 - Le panier de Germaine