mirror of https://github.com/tLDP/LDP
66 lines
1.3 KiB
Bash
66 lines
1.3 KiB
Bash
#! /bin/bash
|
|
# CopyArray.sh
|
|
#
|
|
# This script written by Michael Zick.
|
|
# Used here with permission.
|
|
|
|
# How-To "Pass by Name & Return by Name"
|
|
#+ or "Building your own assignment statement".
|
|
|
|
|
|
CpArray_Mac() {
|
|
|
|
# Assignment Command Statement Builder
|
|
|
|
echo -n 'eval '
|
|
echo -n "$2" # Destination name
|
|
echo -n '=( ${'
|
|
echo -n "$1" # Source name
|
|
echo -n '[@]} )'
|
|
|
|
# That could all be a single command.
|
|
# Matter of style only.
|
|
}
|
|
|
|
declare -f CopyArray # Function "Pointer"
|
|
CopyArray=CpArray_Mac # Statement Builder
|
|
|
|
Hype()
|
|
{
|
|
|
|
# Hype the array named $1.
|
|
# (Splice it together with array containing "Really Rocks".)
|
|
# Return in array named $2.
|
|
|
|
local -a TMP
|
|
local -a hype=( Really Rocks )
|
|
|
|
$($CopyArray $1 TMP)
|
|
TMP=( ${TMP[@]} ${hype[@]} )
|
|
$($CopyArray TMP $2)
|
|
}
|
|
|
|
declare -a before=( Advanced Bash Scripting )
|
|
declare -a after
|
|
|
|
echo "Array Before = ${before[@]}"
|
|
|
|
Hype before after
|
|
|
|
echo "Array After = ${after[@]}"
|
|
|
|
# Too much hype?
|
|
|
|
echo "What ${after[@]:3:2}?"
|
|
|
|
declare -a modest=( ${after[@]:2:1} ${after[@]:3:2} )
|
|
# ---- substring extraction ----
|
|
|
|
echo "Array Modest = ${modest[@]}"
|
|
|
|
# What happened to 'before' ?
|
|
|
|
echo "Array Before = ${before[@]}"
|
|
|
|
exit 0
|