Debian Codegraph @

CopyArray.sh

raw symbols SCIP
1 #! /bin/bash
2 # CopyArray.sh
3 #
4 # This script written by Michael Zick.
5 # Used here with permission.
6
7 # How-To "Pass by Name & Return by Name"
8 #+ or "Building your own assignment statement".
9
10
11 CpArray_Mac() {
12
13 # Assignment Command Statement Builder
14
15 echo -n 'eval '
16 echo -n "$2" # Destination name
17 echo -n '=( ${'
18 echo -n "$1" # Source name
19 echo -n '[@]} )'
20
21 # That could all be a single command.
22 # Matter of style only.
23 }
24
25 declare -f CopyArray # Function "Pointer"
26 CopyArray=CpArray_Mac # Statement Builder
27
28 Hype()
29 {
30
31 # Hype the array named $1.
32 # (Splice it together with array containing "Really Rocks".)
33 # Return in array named $2.
34
35 local -a TMP
36 local -a hype=( Really Rocks )
37
38 $($CopyArray $1 TMP)
39 TMP=( ${TMP[@]} ${hype[@]} )
40 $($CopyArray TMP $2)
41 }
42
43 declare -a before=( Advanced Bash Scripting )
44 declare -a after
45
46 echo "Array Before = ${before[@]}"
47
48 Hype before after
49
50 echo "Array After = ${after[@]}"
51
52 # Too much hype?
53
54 echo "What ${after[@]:3:2}?"
55
56 declare -a modest=( ${after[@]:2:1} ${after[@]:3:2} )
57 # ---- substring extraction ----
58
59 echo "Array Modest = ${modest[@]}"
60
61 # What happened to 'before' ?
62
63 echo "Array Before = ${before[@]}"
64
65 exit 0