From 6c20c8ed2cb9ab69a1a57ccb2b9b79969a808321 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 17:38:56 +0200 Subject: Adding upstream version 5.2.15. Signed-off-by: Daniel Baumann --- examples/functions/sort-pos-params | 69 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 examples/functions/sort-pos-params (limited to 'examples/functions/sort-pos-params') diff --git a/examples/functions/sort-pos-params b/examples/functions/sort-pos-params new file mode 100644 index 0000000..95acd85 --- /dev/null +++ b/examples/functions/sort-pos-params @@ -0,0 +1,69 @@ +# +# Chet Ramey +# +# Copyright 2001 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. + +# Sort the positional parameters. +# Make sure the positional parameters are passed as arguments to the function. +# If -u is the first arg, remove duplicate array members. +sort_posparams() +{ + local -a R + local u + + case "$1" in + -u) u=-u ; shift ;; + esac + + # if you want the case of no positional parameters to return success, + # remove the error message and return 0 + if [ $# -eq 0 ]; then + echo "$FUNCNAME: argument expected" >&2 + return 1 + fi + + # make R a copy of the positional parameters + R=( "${@}" ) + + # sort R. + R=( $( printf "%s\n" "${R[@]}" | sort $u) ) + + printf "%s\n" "${R[@]}" + return 0 +} + +# will print everything on separate lines +set -- 3 1 4 1 5 9 2 6 5 3 2 +sort_posparams "$@" + +# sets without preserving quoted parameters +set -- $( sort_posparams "$@" ) +echo "$@" +echo $# + +# sets preserving quoted parameters, beware pos params with embedded newlines +set -- 'a b' 'a c' 'x z' + +oifs=$IFS +IFS=$'\n' +set -- $( sort_posparams "$@" ) +IFS="$oifs" + +echo "$@" +echo $# + +sort_posparams -- cgit v1.2.3