[olug] bash script help
Nick Walter
waltern at iivip.com
Thu Oct 3 19:13:10 UTC 2002
I had to make two changes to your script to make it work on my system
(Red Hat 8.0, bash 2.05b)
First, I changed
let idx=$#-1
to
let idx=$#
and also changed
set "${@:0:$idx} new-value"
to
set "\"${@:0:$idx}\" new-value"
leaving the whole script as:
let idx=$#
set "\"${@:0:$idx}\" new-value"
echo "$@"
--capture--
[ii120:~]# ./test.sh foo bar widget
"foo bar" new-value
[ii120:~]#
--capture--
Nick Walter
On Thu, 2002-10-03 at 13:58, William E. Kempf wrote:
> I've got a bit of a complex bash script problem. I need to change the value
> of the last positional argument passed to the script, in order to pass the
> arguments on to another script/program. Here's my first (naive) attempt:
>
> # script "test"
> let idx=$#-1
> set "${@:0:$idx} new-value"
> echo "$@"
>
> The problem with this is in quoting (which often makes my head hurt).
> Here's a sample usage that works:
>
> $test foo bar widget
> foo bar new-value
>
> However, this usage doesn't work correctly:
>
> $test "foo bar" widget
> foo bar new-value
>
> What I need would be this output instead:
>
> "foo bar" new-value
>
> Anyone know of a way to achieve this? I suppose I could take a brute force
> approach and loop through every argument appending them to a variable with
> quotes around it... but the extra quotes this would produce bug me a bit,
> and this seems like it would be an awfully slow way to do this.
>
> Bill Kempf
> _______________________________________________
> OLUG mailing list
> OLUG at olug.org
> http://lists.olug.org/mailman/listinfo/olug
>
More information about the OLUG
mailing list