diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 06:17:24 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 06:17:24 +0000 |
commit | 9d8085074991d5c0a42d6fc96a2d1a3ee918aad1 (patch) | |
tree | c85bca1e6c11eb872edfc64c524d20f2b7e3307b /examples/functions/array-stuff | |
parent | Initial commit. (diff) | |
download | bash-9d8085074991d5c0a42d6fc96a2d1a3ee918aad1.tar.xz bash-9d8085074991d5c0a42d6fc96a2d1a3ee918aad1.zip |
Adding upstream version 5.1.upstream/5.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | examples/functions/array-stuff | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/examples/functions/array-stuff b/examples/functions/array-stuff new file mode 100644 index 0000000..e6316c7 --- /dev/null +++ b/examples/functions/array-stuff @@ -0,0 +1,122 @@ +# +# Chet Ramey <chet.ramey@case.edu> +# +# Copyright 1999 Chester Ramey +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2, or (at your option) +# any later version. +# +# TThis program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +# usage: reverse arrayname +reverse() +{ + local -a R + local -i i + local rlen temp + + # make r a copy of the array whose name is passed as an arg + eval R=\( \"\$\{$1\[@\]\}\" \) + + # reverse R + rlen=${#R[@]} + + for ((i=0; i < rlen/2; i++ )) + do + temp=${R[i]} + R[i]=${R[rlen-i-1]} + R[rlen-i-1]=$temp + done + + # and assign R back to array whose name is passed as an arg + eval $1=\( \"\$\{R\[@\]\}\" \) +} + +A=(1 2 3 4 5 6 7) +echo "${A[@]}" +reverse A +echo "${A[@]}" +reverse A +echo "${A[@]}" + +# unset last element of A +alen=${#A[@]} +unset A[$alen-1] +echo "${A[@]}" + +# ashift -- like shift, but for arrays + +ashift() +{ + local -a R + local n + + case $# in + 1) n=1 ;; + 2) n=$2 ;; + *) echo "$FUNCNAME: usage: $FUNCNAME array [count]" >&2 + exit 2;; + esac + + # make r a copy of the array whose name is passed as an arg + eval R=\( \"\$\{$1\[@\]\}\" \) + + # shift R + R=( "${R[@]:$n}" ) + + # and assign R back to array whose name is passed as an arg + eval $1=\( \"\$\{R\[@\]\}\" \) +} + +ashift A 2 +echo "${A[@]}" + +ashift A +echo "${A[@]}" + +ashift A 7 +echo "${A[@]}" + +# Sort the members of the array whose name is passed as the first non-option +# arg. If -u is the first arg, remove duplicate array members. +array_sort() +{ + local -a R + local u + + case "$1" in + -u) u=-u ; shift ;; + esac + + if [ $# -eq 0 ]; then + echo "array_sort: argument expected" >&2 + return 1 + fi + + # make r a copy of the array whose name is passed as an arg + eval R=\( \"\$\{$1\[@\]\}\" \) + + # sort R + R=( $( printf "%s\n" "${A[@]}" | sort $u) ) + + # and assign R back to array whose name is passed as an arg + eval $1=\( \"\$\{R\[@\]\}\" \) + return 0 +} + +A=(3 1 4 1 5 9 2 6 5 3 2) +array_sort A +echo "${A[@]}" + +A=(3 1 4 1 5 9 2 6 5 3 2) +array_sort -u A +echo "${A[@]}" |