It is currently Tue, 17 May 2022 02:53:36 GMT



 
Author Message
 variable expansion: weird question
In article <7ucapi$o9...@bigeye.rhein-neckar.de>,

Using:
eval last_arg=\"\$$#\"
or
eval last_arg=\"\$\{$#\}\"
is slightly safer than:
eval last_arg=\${$#}
or
eval last_arg=\$$#
as the quoted (\) double quotes (") help to insure that last_arg is in
fact set to the complete last argument, including any whitespace.
Take for example:
set -- foo bar 'foobar echo uh oh'
eval last_arg=\"\$\{$#\}\"
ends up equivalent to:
last_arg='foobar echo uh oh'
whereas:
eval last_arg=\${$#}
ends up  equivalent to:
last_arg=foobar echo uh oh
which will execute echo with those arguments, ... of course if that
stuff came from a user command line, user (or typo or some program
error) might potentially specify a command and arguments more hazardous
than echo uh oh.  Use of:
eval last_arg=\"\$\{$#\}\"
will work
if [ $# -le 9 ]
with Bourne and Bourne-like shells (bash, etc.), and for
Bash (and perhaps some other Bourne-like shells) it will also handle that
even if
[ $# -gt 9 ]
If you've got to be fully backwards compatible with Borune shell and
have to worry about arbitrary whitespace in the arguments and more than
9 positional parameters, the answer isn't elegant, but eval does come to
the rescue, with something like this:
n=0
while [ $# -gt 9 ]
do
        n=`expr $n + 1`
        eval arg$n=\"\$1\"
        shift
done
eval last_arg=\"\$\{$#\}\"
while [ $n -ge 1 ]
do
        eval set -- \"\$arg$n\" \"\$@\"
        eval unset arg$n
        n=`expr $n - 1`
done
unset n

With eval, essentially it's arguments are scanned twice, so just think
about what you do and don't want interpreted on the first scan, and what
you want interpreted on the second scan.  As you can see from the above,
eval can come in handy where we need to effectively dynamically allocate
a number of shell variables where we don't know how many we'll need
ahead of time.  Such can be used to, for example, implement a crude
array - which is essentially what we do here.  This can be quite handy
if your shell doesn't handle array variables (such as the classic Bourne
shell, or some versions of ksh (I believe some older public domain
versions didn't handle arrays), etc.)
--
micha...@crl.com



 Thu, 04 Apr 2002 03:00:00 GMT   
 variable expansion: weird question
On 17 Oct 1999 18:03:21 -0700, Michael Paoli <micha...@crl.crl.com> wrote:

While the extra quoting won't hurt, it isn't required.  Word
splitting does not happen during variable assignments.

Of course, you didn't test this --- the results with and without
the quotes are identical.  The eval'd expression in the latter
case is:
  last_arg=${3}
and ${3} is expanded and copied to last_arg _without further processing_.

Again, it doesn't hurt to add the extra quotes.  Since in many
contexts the quotes are required it is perhaps easiest to just
learn to always add the quotes, but Christian's answer *is*
perfectly fine and safe.

                --Ken Pizzini



 Fri, 05 Apr 2002 03:00:00 GMT   
 variable expansion: weird question
In article <7ubmtk$r8...@panix3.panix.com>,
woz...@panix.com (J. Alan Eldridge) writes:

True Bourne shell won't allow you to access a positional parameter
greater than 9.  Bash allows you to fairly easily access the last
parameter.  But the only portable solution is to loop the args -
however,  your loop is way too inefficient. Try:

   for last:do :;done

Simple,  fast and compact.

--
Dan Mercer
damer...@uswest.net

Opinions expressed herein are my own and may not represent those of my employer.



 Fri, 05 Apr 2002 03:00:00 GMT   
 variable expansion: weird question
On 18 Oct 1999 12:34:03 GMT, Dan Mercer <damer...@mmm.com> wrote:
and with a typo :)  But you're right!

This is for Bash only, I think:

   : "$@"
   last=$_

It's fast and compact, too, and even more cryptic ;)
Just adding this as one more possibility ...

Regards,
  Martin
--
Martin Ramsch <m.ram...@computer.org> <URL: http://home.pages.de/~ramsch/ >
PGP KeyID=0xE8EF4F75 FiPr=5244 5EF3 B0B1 3826  E4EC 8058 7B31 3AD7



 Sat, 06 Apr 2002 03:00:00 GMT   
 variable expansion: weird question

So, to combine this with the last-day-in-month question that was running
in another thread, one could end up with

     : $(cal)
     echo "Number of days in this month: $_"

:-)

  //Klaus

--
[ Magnetic Ink ] ><>
[ Feeed me, Seymor! ]



 Sat, 06 Apr 2002 03:00:00 GMT   
 variable expansion: weird question
In article <7ubmtk$r8...@panix3.panix.com>,
J. Alan Eldridge <woz...@panix.com> wrote:

Well, if your looking for perversity, how about:

last_arg=`echo $@ | awk '{print $NF}'`

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,
  Stay Fit       |   @ @   | If a man waited to do it so well,
  Die Anyway     |    v    | That no one could find fault with it.
  de...@tiac.net |  \___/  | http://www.tiac.net/users/demas



 Sat, 06 Apr 2002 03:00:00 GMT   
 variable expansion: weird question
In article <slrn80o3qj.7sf.m.ram...@melian.forwiss.uni-passau.de>,
        actually, this works for any ksh-derivative, and is (imho) one
of the few really useful examples of use of the no-op command.  $_ is
the last argument given to the last synchronous command the shell
executes; since the shell hasn't finished executing your script yet,
you can't just reference $_ directly (until some commands are
executed from in a subshell, $_ holds the name the subshell was
invoked with), so you simply invoke the no-op with the same arg list,
and _then_ reference $_.

        -- sweth.

--
Sweth Chandramouli ; <sw...@gwu.edu>
<a href="http://astaroth.nit.gwu.edu/resume/">Will Work For Food.</a>
<a href="http://astaroth.nit.gwu.edu/~sweth/disc.html">*</a>



 Sat, 06 Apr 2002 03:00:00 GMT   
 variable expansion: weird question

ksh, too.

--
Christian "naddy" Weisgerber                  na...@mips.rhein-neckar.de



 Sat, 06 Apr 2002 03:00:00 GMT   
 variable expansion: weird question

This completely destroys leading, trailing, and embedded whitespace.

--
Christian "naddy" Weisgerber                  na...@mips.rhein-neckar.de



 Sat, 06 Apr 2002 03:00:00 GMT   
 variable expansion: weird question
In article <7uif0d$pm...@bigeye.rhein-neckar.de>,

yeah, but so did/does

#!/bin/ksh
: $@
last=$_
echo $last

and your other post didn't object to that.  :-)

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,
  Stay Fit       |   @ @   | If a man waited to do it so well,
  Die Anyway     |    v    | That no one could find fault with it.
  de...@tiac.net |  \___/  | http://www.tiac.net/users/demas



 Sat, 06 Apr 2002 03:00:00 GMT   
 variable expansion: weird question
On 19 Oct 1999 20:12:31 GMT, Charles Demas <de...@sunspot.tiac.net> wrote:

The other post used:
  : "$@"
  last=$_
which preserves whitespace just fine.

                --Ken Pizzini



 Sat, 06 Apr 2002 03:00:00 GMT   
 variable expansion: weird question
DON'T SEND COPIES OF FOLLOWUP ARTICLES BY E-MAIL, ESPECIALLY IF NOT
EXPLICITLY MARKED AS SUCH. IT IS EXCESSIVELY RUDE.

Yes, because Martin Ramsch actually specified

: "$@"
last=$_

which *does* preserve argument boundaries and whitespace.
Learn the difference between $*, $@, "$*", and "$@".

--
Christian "naddy" Weisgerber                  na...@mips.rhein-neckar.de



 Sun, 07 Apr 2002 03:00:00 GMT   
 
   [ 12 post ] 

Similar Threads

1. variable expansion: simple question

2. HELP! Double variable expansion question

3. tilde + variable expansion in bash

4. autoconf variable expansion

5. Variable and Pathname Expansion

6. Variable Expansion With sed

7. Expansion of variables in sh scripts

8. Variable expansion works, then doesn't

9. double quote variable expansion

10. variable expansion on the bash command line


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software