[olug] bash script help

Trent Melcher tmelcher at trilogytel.com
Thu Oct 3 19:27:35 UTC 2002


This should do it

set "\"${@:0:$idx}\" new-value"

This works on a Redhat 7.2 box

Trent

-----Original Message-----
From: olug-admin at olug.org [mailto:olug-admin at olug.org]On Behalf Of
William E. Kempf
Sent: Thursday, October 03, 2002 1:59 PM
To: olug at olug.org
Subject: [olug] bash script help


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