The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
While loop - The sum seems to be local eagercyber Shell Programming and Scripting 2 03-26-2008 04:06 AM
How to make variables in script function local? alex_5161 Shell Programming and Scripting 5 03-07-2008 02:03 PM
Problem with global and local variables qzv2jm Shell Programming and Scripting 2 03-04-2008 01:18 PM
rsh with local variables jo_aze UNIX for Dummies Questions & Answers 1 10-07-2002 10:54 AM
change the IPAddress and local name lapnguyen UNIX for Dummies Questions & Answers 2 07-10-2002 02:49 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-20-2008
alex_5161 alex_5161 is offline
Registered User
  
 

Join Date: Jan 2007
Location: Detroit
Posts: 124
'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 (permalink)  
Old 11-20-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,770
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 (permalink)  
Old 11-20-2008
alex_5161 alex_5161 is offline
Registered User
  
 

Join Date: Jan 2007
Location: Detroit
Posts: 124
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 (permalink)  
Old 11-20-2008
alex_5161 alex_5161 is offline
Registered User
  
 

Join Date: Jan 2007
Location: Detroit
Posts: 124
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!
Closed Thread

Bookmarks

Tags
shell script, shell scripting, unix scripting, unix scripting basics

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:26 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0