![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
'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: > 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
>
|
|
||||
|
Quote:
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"
|
|
||||
|
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 > Last edited by alex_5161; 11-20-2008 at 01:43 PM.. |
|
||||
|
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 > Bash has been so nice for me! |
![]() |
| Bookmarks |
| Tags |
| shell script, shell scripting, unix scripting, unix scripting basics |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|