Subject: Re: positional parameters in wrapper scripts
To: None <tech-pkg@NetBSD.org>
From: Alan Barrett <apb@cequrux.com>
List: tech-pkg
Date: 06/06/2004 21:35:42
On Sun, 06 Jun 2004, Klaus Heinz wrote:
> #! /bin/sh
> echo "number of parameters: $#"
> while test $# -gt 0; do
> if test "1" = "$1"; then shift; fi
> params="$params $1"
This loses any embedded spaces in $1.
You need to mix in a shell-quotify function and an eval,
like this:
#! /bin/sh
# shell-quotify: insert backslashes where appropriate
# to protect a string from the shell.
shell-quotify ()
{
printf "%s" "$1" | sed -e 's/\([^-=+:,./A-Za-z0-9]\)/\\\1/g'
}
echo "number of parameters: $#"
while test $# -gt 0; do
if test "1" = "$1"; then shift; fi
params="$params $(shell-quotify "$1")"
shift # ignore the case where $# was 1 and I shift two times
done
eval set -- $params
echo "number of parameters: $#"
--apb (Alan Barrett)