'while' loop does not change local variables?!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting 'while' loop does not change local variables?!
# 1  
Old 11-20-2008
'while' loop does not change local variables?!

(I think this question desearves separate thread..)
I have a problem with 'while'
I am trying to set variables by 'while' and it is fine inside, but after completting the loop all changes are lost:
Code:
> bb="kkkk - 111\nlllll - 22222\nbbbb - 4444"
> echo "$bb"
kkkk - 111
lllll - 22222
bbbb - 4444
> nn=""
> echo "$bb"|while read ln; do 
  nn=$nn", "$(echo $ln|cut -d- -f2); 
  echo $nn; 
done; 
echo "otside: \n$nn"
, 111
, 111, 22222
, 111, 22222, 4444
otside:

>

After 'while' internal changes are lost!
Is it how it should be?
It seems as 'while' is processing in separated shell.
Is here any way to make it works for local variables?

The 'for ..' loop works different"
Code:
> nn=""
> for ln in $(echo "${bb// /_}"); do 
  nn=$nn", "$(ec ${ln//_/ }|cut -d- -f2); 
  echo $nn; 
done; 
echo "otside: \n$nn"
, 111
, 111, 22222
, 111, 22222, 4444
otside:
,  111,  22222,  4444
>

# 2  
Old 11-20-2008
Quote:
csadev:/home/jmcnama> test.sh
before while n=1
inside while n=2
after while n=2
before for loop n=2
inside for loop n=4
inside for loop n=5
outside for loop n=5
Code:
#!/bin/ksh
n=1
echo "before while n=$n"
while true 
do
    n=2
    echo "inside while n=$n"
    break
done
echo "after while n=$n"

echo "before for loop n=$n"
for n in 4 5
do
   echo "inside for loop n=$n"
done
echo "outside for loop n=$n"

This is korn shell. What shell do you have?
# 3  
Old 11-20-2008
I am using bash (sorry, forget to mention.)

I've checked your example - it is fine: the $n is set for outside.

Could you check my example with 'read' lines?

Here is my check:
Code:
> n=0; while true; do n=$n", "2; break; done; echo "after $n"
after 0, 2
> n=0; echo "11\n222\n333"| while read ll; do echo "now $ll"; n=$n", "2; echo "inside $n"; done; echo "after $n"
now 11
inside 0, 2
now 222
inside 0, 2, 2
now 333
inside 0, 2, 2, 2
after 0
>

While-true works, but while-read doesn't!

Last edited by alex_5161; 11-20-2008 at 01:43 PM..
# 4  
Old 11-20-2008
Ok, it is resolved with jlliagre help:
"I guess you are using bash which use a subshell for the wrong (IMHO) side of a pipe ..."
So, it is bash glitch:

Code:
> ksh
> n=0; cat ff|while read ll; do echo "now $ll"; n=$n", "$ll; done; echo $n
now 11
now 222
now 333
0, 11, 222, 333
> ^D
> it is bash now
bash: it: command not found
> n=0; cat ff|while read ll; do echo "now $ll"; n=$n", "$ll; done; echo $n
now 11
now 222
now 333
0
> n=0; while read ll; do echo "now $ll"; n=$n", "$ll; done <ff; echo $n
now 11
now 222
now 333
0, 11, 222, 333
>

Sad!
Bash has been so nice for me!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Listing all local variables for unset

I have tried to thoroughly search other threads before posting this question... I have a shell script (bsh) that I'd like to "re-execute" if the user chooses to. Before the program is executed again the local variables (those set within the script) need to be unset. Is there a command that... (6 Replies)
Discussion started by: powwm
6 Replies

2. Shell Programming and Scripting

Using a find command in ssh but using local variables?

I have a script like this (Yes, I know the DAY6 number isn't right - I'm just testing at this point): DAY0=`date -I` DAY1=`date -I -d "1 day ago"` DAY6=`date -I -d "2 days ago"` if then ssh root@synology1 nohup rm -rf "/volume1/Fileserver/$DAY6" fi I've tested the line to remove the... (5 Replies)
Discussion started by: Orionizer
5 Replies

3. Debian

How to change local IP address?

I have a new Ethernet device that has a default IP address on a different subnet and need to change it. I have a Debian 6.0 host connected to the device with a crossover cable and have changed the host /etc/hosts and /etc/networks files to the same subnet as the new device and rebooted. Ping... (2 Replies)
Discussion started by: snorkack59
2 Replies

4. Programming

Thread function local variables

As I know threads share the memory. But, what about the local variables in the thread function? if i call multiple threads would they allocate seperate local variables for themselves? like thread_func() { int i, j; string... } Are the above local variables defined for each of... (1 Reply)
Discussion started by: saman_glorious
1 Replies

5. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

6. Shell Programming and Scripting

How to make variables in script function local?

Is it possible to make function variables local? I mean for example, I have a script variable 'name' and in function I have declared variable 'name' I need to have script's 'name' have the same value as it was before calling the function with the same declaration. The way to preserve a... (5 Replies)
Discussion started by: alex_5161
5 Replies

7. Shell Programming and Scripting

Problem with global and local variables

Guys, how can I define global variables in sorlaris...cause I lose the values outside the scope. Rite now wat I do is,I redirect variable value to a file n then get it back outside the function...:o....theres obviously a better way of doing this...I now this is a basic question....but please... (2 Replies)
Discussion started by: qzv2jm
2 Replies

8. UNIX for Dummies Questions & Answers

rsh with local variables

Hi, I am trying to do an rsh and execute the same script on a distant unix computer. The problem is that I need to get all the local variables of the distant computer to launch correctly my script. I'm working on AIX 4.3.3 I try to execute .profile in the rsh but it seems not to be... (1 Reply)
Discussion started by: jo_aze
1 Replies

9. UNIX for Dummies Questions & Answers

change the IPAddress and local name

I just installed Red Hat Linux 7.3 on one of our server. Everything works fine except couple things I want to change: Could someone give me the full instruction of how to change the IPadress and the name of the computer name please??? Please give me the full instructions b/c I am new with... (2 Replies)
Discussion started by: lapnguyen
2 Replies

10. UNIX for Dummies Questions & Answers

shell script, reading and resetting local variables

Hello, I have a problem with trying to run a shell script that reads in user input, validates, and sets to a 'default' value if the input is not valid. I cannot get the portion of resetting to a default value to work. These lines are skipped, and the value of x is still whatever the user... (1 Reply)
Discussion started by: b888c
1 Replies
Login or Register to Ask a Question