eval is evil
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Shell > eval is evil




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    eval is evil  
scott


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-23-04 11: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







[ Post a follow-up to this message ]



    Re: eval is evil  
Chris F.A. Johnson


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-23-04 11: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





[ Post a follow-up to this message ]



    Re: eval is evil  
Barry Margolin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-23-04 11: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 ***





[ Post a follow-up to this message ]



    Re: eval is evil  
scott


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-23-04 11: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 ***







[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 08:24 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register