SoloCDM wrote:
>
> On Sun, 27 May 2001, Pierre Fortin wrote:
>
> % Date: Sun, 27 May 2001 10:05:01 -0400
> % From: Pierre Fortin <[EMAIL PROTECTED]>
> % To: [EMAIL PROTECTED], [EMAIL PROTECTED]
> %
> % SoloCDM wrote:
> % >
> % > Every time I have a bash while, for or any other type of loop,
> % > with numbers processed and saved to a variable, the numbers are
> % > not in the variable outside the loop. Although, the numbers are
> % > capable of use and manipulation in the variable, for reassigning
> % > the variable or recall, anytime before the loop ends. How can the
> % > variable's content be available outside the loop?
> %
> % bash allows access to in-loop defined/updated variables outside loops EXCEPT
> % those defined/updated inside functions; is this your problem?
> %
> % Example:
> %
> % #!/bin/bash
> %
> % i=1
> % j=10
> % li=5
> %
> % myloop() {
> % li=$li*$li # Note: li takes from global but does not affect global
> % lj=8 # Note: lj is not defined globally
> % }
> %
> % echo -n "i:k:li = "
> % while [ $i -le $j ]; do
> % let k=${i}*$li
> % echo -n "$i:$k:$li "
> % let i=$i+1
> % done
> % echo
> % echo "Note: k not defined outside the loop but is available here..."
> % echo "i,j,[k],(li) at loop exit: $i $j [$k] ($li)"
> %
> % for n in 1 2 3; do
> % let n=$n*$li
> % echo "li defined outside function gives: $n"
> % done
> %
> % # this loop gives errors 'cuz lj defined inside function
> % for n in 1 2 3; do
> % let n=$n*$lj
> % echo "lj defined INside function gives: $n"
> % done
> %
> % Which gives:
> %
> % # ./numtst
> % i:k:li = 1:5:5 2:10:5 3:15:5 4:20:5 5:25:5 6:30:5 7:35:5 8:40:5 9:45:5 10:50:5
> % Note: k not defined outside the loop but is available here...
> % i,j,[k],(li) at loop exit: 11 10 [50] (5)
> % li defined outside function gives: 5
> % li defined outside function gives: 10
> % li defined outside function gives: 15
> % ./numtst: let: n=1*: syntax error: operand expected (error token is "*")
> % lj defined INside function gives: 1
> % ./numtst: let: n=2*: syntax error: operand expected (error token is "*")
> % lj defined INside function gives: 2
> % ./numtst: let: n=3*: syntax error: operand expected (error token is "*")
> % lj defined INside function gives: 3
> %
> % Error is due to n=$n*<blank_cuz_undefined_here>
>
> Even though you don't call the function myloop, the definition for let
> in the man bash misleads a person into believing a true/false or 1/0
> will be the outcome. But, you're saying that let will allow the
> variable value to carry outside the loop or subshell . . . correct?
Sheesh... gotta learn to be more careful... sorry. No, "let" is needed to do
numeric manipulation;
otherwise it does text concatenation, like this:
i=1
let i=$i+1 # or: let i++ # no "$" in this case.
i=$i+1
echo $i
gives:
2+1 (text instead of number '3')
sigh... man bash says:
Variables local to the function may be declared with the local builtin
command. Ordinarily, variables and
their values are shared between the function and its caller.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
OK... just add "myloop" immediately following each "for" and you'll get:
# ./numtst
i:k:li = 1:5:5 2:10:5 3:15:5 4:20:5 5:25:5 6:30:5 7:35:5 8:40:5 9:45:5 10:50:5
Note: k not defined outside the loop but is available here...
i,j,[k],(li) at loop exit: 11 10 [50] (5)
li defined outside function gives: 25
li defined outside function gives: 1250
li defined outside function gives: 1171875
lj defined INside function gives: 8
lj defined INside function gives: 16
lj defined INside function gives: 24
So, it would appear that any variable is global throughout a shell unless
explicitly made local with "local" (there is no "global" in bash according to
the man).
Why don't you post a code sample which is giving you trouble...?
Pierre