Fwd: Shell Script Question

David Bandel david.bandel
Wed Jun 1 21:05:51 PDT 2005


Wonder why this reply didn't go to the list?

---------- Forwarded message ----------
From: David Bandel <david.bandel at gmail.com>
Date: Jun 1, 2005 6:41 PM
Subject: Re: Shell Script Question
To: almaw at ieee.org


On 6/1/05, Alma J Wetzker <almaw at ieee.org> wrote:
> David Bandel wrote:
> > On 6/1/05, Brad De Vries <devriesbj at gmail.com> wrote:
> >
> >>Hey all, I have a shell scripting question I'm hoping someone can
> >>answer for me.  Why doesn't a variable that is being set within a loop
> >>retain its value when the loop is done?
> >
> >
> > try Googling on something like "variable scope".  When you declare a
> > variable in any language I'm familiar with (for example, Perl), a
> > variable declared within a program subsection is local to that
> > subsection and is destroyed when that subsection exits.  If you need a
> > variable to have global scope (please, don't start making all your
> > variables global), you need to declare it in a global context.  Perl
> > also has the notion of importing and exporting when dealing with
> > modules.
> >
> > This can get sticky if you declare a global variable, and a called
> > module uses the same variable.
> [snip]
> >>
> >>What I'm expecting is that the variables "MORE", "TOTAL_LENGTH", and
> >>"FILES" should retain the values being set within the while-loop but
> >>they aren't.
> >>
> >>Any thoughts as to why?
> >
> >
> > see my explanation above.  If you need variables to exit, you need to
> > set return values to handle those returns.
> >
> >
> >>TIA,
> >>Brad.
> >>
> >>P.S. The break command doesn't affect anything.  If I comment it out
> >>and let the loop run its course, all three variables resort back to
> >>their original values exactly like when the break executes.
> >
> >
> > All by design.  Again, if you need the values to be maintained, you
> > need to return() those values.
>
> I don't do much scripting beyond expect, can you have more than one
> return value to a script?  I counted three desired variables.  Is there
> a way to get all three out without going to global?
>
> In C, I would pass two by reference and return the third.  Can you do
> weird stuff like that in a script?

Good question.  And the answer is:  I don't have a clue.  And it could
be different between ksh, bash, bash2, and bash3.  As soon as
something really needs more than very simple scripting, I drop into
Perl mode.  In Perl I can pass several variables to a subroutine, no
reference needed (although Perl can do all those memory tricks of
passing a memory address of a variable).  And there are ways to get
them back out, too.

Brad, what shell are you calling and have you tried calling a
different one?  Last time I worked on AIX and HP-UX, they didn't have
bash.  They had borne and korn shells and that was about it (unless
you liked to abuse yourself with a C-shell -- which should only be
sold down by the sea shore).

Ciao,

David A. Bandel
--
Focus on the dream, not the competition.
            - Nemesis Air Racing Team motto


-- 
Focus on the dream, not the competition.
            - Nemesis Air Racing Team motto



More information about the Linux-users mailing list