Unix Shell - eval is evil

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > April 2004 > eval is evil





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author eval is evil
scott

2004-04-23, 6:35 pm


for parameter in $*
do
echo $#
eval echo \${$#}
shift
done


if this is invoked as "./program a b c d" i would expext the output to be:
4
d
3
c
2
b
1
a

but it is
4
d
3
d
2
d
1
d

Can anyone explain this and help me get my expected output?

Thanks,
-Scott


Chris F.A. Johnson

2004-04-23, 6:35 pm

On Fri, 23 Apr 2004 at 21:15 GMT, scott wrote:
>
> for parameter in $*
> do
> echo $#
> eval echo \${$#}
> shift
> done
>
>
> if this is invoked as "./program a b c d" i would expext the output to be:
> 4
> d
> 3
> c
> 2
> b
> 1
> a
>
> but it is
> 4
> d
> 3
> d
> 2
> d
> 1
> d
>
> Can anyone explain this and help me get my expected output?


Shift removes the first argument not the last.

n=$#
while [ $n -gt 0 ]
do
eval echo \${$n}
n=$(( $n - 1 ))
done

--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
Barry Margolin

2004-04-23, 6:35 pm

In article <PsidnTo4doDHGhTdRVn-hQ@comcast.com>,
"scott" <speedglide@comcast.net> wrote:

> for parameter in $*
> do
> echo $#
> eval echo \${$#}
> shift
> done
>
>
> if this is invoked as "./program a b c d" i would expext the output to be:
> 4
> d
> 3
> c
> 2
> b
> 1
> a
>
> but it is
> 4
> d
> 3
> d
> 2
> d
> 1
> d
>
> Can anyone explain this and help me get my expected output?


The first time through the loop, the arguments are:

a b c d

$# is 4, and $4 is d, so it prints 4 then d.

The second time through the loop, the arguments are:

b c d

because the shift command removed the first argument. Now $# is 3, and
$3 is d, so it prints 3 then d.

Do I need to go on?

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
scott

2004-04-23, 6:35 pm

Doh....

Thank You!

-Scott

"Barry Margolin" <barmar@alum.mit.edu> wrote in message
news:barmar-F1E560.17265323042004@comcast.ash.giganews.com...
> In article <PsidnTo4doDHGhTdRVn-hQ@comcast.com>,
> "scott" <speedglide@comcast.net> wrote:
>
be:[vbcol=seagreen]
>
> The first time through the loop, the arguments are:
>
> a b c d
>
> $# is 4, and $4 is d, so it prints 4 then d.
>
> The second time through the loop, the arguments are:
>
> b c d
>
> because the shift command removed the first argument. Now $# is 3, and
> $3 is d, so it prints 3 then d.
>
> Do I need to go on?
>
> --
> Barry Margolin, barmar@alum.mit.edu
> Arlington, MA
> *** PLEASE post questions in newsgroups, not directly to me ***



Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com