Bash scripting question

Brad De Vries devriesbj
Mon May 17 11:47:52 PDT 2004


--- listmail at rotundus.com wrote:
> Brad De Vries wrote,
> > You could try:
> > ---------
> > TOTCNT=$#
> > for ((NUM=1; NUM <= $TOTCNT; NUM++))
> > do
> >   echo "$NUM: $1"
> >   shift
> > done
> > ---------
> 
> Wouldn't this kind of leave me stuck if I were to
> want to do something
> with any of the parameters later on?  I notice from
> the man page that you
> can't shift them back.
> 
> Thanks for the suggestion.
> 
> David Aikema

You are correct, you cannot shift the positional
parameters back onto the stack once they've been
shifted off.  I didn't realize that you wanted to
retain their position for use later on.  In that case,
I would take one of the other suggestions and simply:

#!/bin/sh
NUM=0
for i in $*; do
  let NUM=$NUM+1
  echo "$NUM: $i"
done

You might also be able to use the 'eval' builtin
command.

Brad.

__________________________________
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.
http://search.yahoo.com


More information about the Linux-users mailing list