Let's get to^H^Haway from the core problem

Richard Richard at reika.demon.co.uk
Tue Jun 27 10:14:16 PDT 2000


> > for file in $(ls $1); do
> >   echo $file
> > done
> >
>
> If $file turns out to be a directory, can I recursively call this
function?
yes

> All shell variables are global, which
> means I have to be real careful.
You can declare a variable as local, but is will be in scope of all
subroutines that do not declare it local one way or another

> Are the passed parameters local?
Yes, and with local scope

>  Does $1 in one iteration of a function equal $1 in another iteration?
Yes

The best way to learn is to play with a script. Try this:

-----------------------------------------
#!/bin/bash

recurse_demo ()
{
  local level
  if [ "$2" = "0" ]; then
    echo -n $1
  else
    level=$(($2-1))
    if [ "$1" = "x" ]; then
      recurse_demo o $level
      recurse_demo x $level
    else
      recurse_demo x $level
      recurse_demo . $level
    fi
  fi
}

for a in 0 1 2 3 4 5 6; do
  b=$a
  for a in a b c; do
    echo -n $a
  done
  echo -n "($a)"
  unset a
  echo -n "[$a] "
  a=$b # try deleting the $ on this line!
  mess=$(recurse_demo x $a)
  echo -e "2^$a=${#mess}\t$mess"
done
-----------------------------------------
With the $, sensible behaviour

abc(c)[] 2^0=1 x
abc(c)[] 2^1=2 ox
abc(c)[] 2^2=4 x.ox
abc(c)[] 2^3=8 oxx.x.ox
abc(c)[] 2^4=16 x.oxoxx.oxx.x.ox
abc(c)[] 2^5=32 oxx.x.oxx.oxoxx.x.oxoxx.oxx.x.ox
abc(c)[] 2^6=64
x.oxoxx.oxx.x.oxoxx.x.oxx.oxoxx.oxx.x.oxx.oxoxx.x.oxoxx.oxx.x.ox

Without the $, If you understand this, please explain it to me

abc(c)[] 2^b=0
abc(c)[] 2^b=2 ox
abc(c)[] 2^b=4 x.ox
abc(c)[] 2^b=8 oxx.x.ox
abc(c)[] 2^b=16 x.oxoxx.oxx.x.ox
abc(c)[] 2^b=32 oxx.x.oxx.oxoxx.x.oxoxx.oxx.x.ox
abc(c)[] 2^b=64
x.oxoxx.oxx.x.oxoxx.x.oxx.oxoxx.oxx.x.oxx.oxoxx.x.oxoxx.oxx.x.ox

Richard


--
Mail archive: http://www.pcrdallas.com/mail-archives/alfs-discuss
IRC access: server: irc.linuxfromscratch.org port: 6667 channel: #LFS
Unsubscribe: email alfs-discuss-request at linuxfromscratch.org and put
"unsubscribe" (without the quotation marks) in the body of the message
(no subject is required)



More information about the alfs-discuss mailing list