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
Assign o/p of awk to a variable c2b2 Shell Programming and Scripting 8 04-01-2009 08:50 AM
assign a value to a variable Shivdatta Shell Programming and Scripting 10 01-17-2007 07:40 AM
assign value to variable using AWK HAA Shell Programming and Scripting 4 12-06-2006 11:43 AM
assign a value to variable markjason Shell Programming and Scripting 3 10-10-2006 03:05 PM
assign to variable AkumaTay UNIX for Dummies Questions & Answers 1 05-18-2002 03:11 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 10-12-2006
agustincm agustincm is offline
Registered User
  
 

Join Date: Mar 2005
Posts: 9
Question assign value to variable is not working

Hi

The next script campares two files File1-Line1 vs File2-Line1, File1-Line1 vs File2-Line2... only if line contains "AS-", if LineX is not in File2 writes in aux, but "valor" is allways=1 never changes! What is wrong?
valor changes to 0 before break, after brake is again 1

#!/bin/ksh

echo > aux

while read label
do
valor=1
echo $label | grep "AS-"
if [ $? -eq 0 ]
then
while read labelold
do
if [ "${label}" = "${labelold}" ]
then
#echo "encon" >aux2
valor=0
echo $valor
break
fi
echo $valor #<--- Allways is 1
done < hosts.old
if [$valor -eq 1]
then
echo "$label" >> aux
fi
fi
done < hosts

Last edited by agustincm; 10-12-2006 at 01:44 PM.. Reason: more info
  #2 (permalink)  
Old 10-12-2006
Corona688 Corona688 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 1,972
Whenever you pipe things into a loop, that loop becomes its own process, with it's own seperate process space -- which inherits variables you already have, but things it sets don't propogate back.
  #3 (permalink)  
Old 10-12-2006
Tomato Tomato is offline
Registered User
  
 

Join Date: Sep 2006
Location: HKSAR, China
Posts: 8
You can write the variable into a file in the loop
  #4 (permalink)  
Old 10-13-2006
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,433

Code:
#!/bin/ksh

echo > aux

while read label
do
   valor=1
   echo $label | grep "AS-"
   if [ $? -eq 0 ]
   then
      while read labelold
      do
         if [ "${label}" = "${labelold}" ]
         then
            #echo "encon" >aux2
            valor=0
            echo $valor
            break
         fi
         echo $valor #<--- Allways is 1
      done < hosts.old
      if [$valor -eq 1]
      then
         echo "$label" >> aux
      fi
   fi
done < hosts

After assigning the value 0 to your variable, you break the while loop so you never display the variable with the new value 0.

Whith KSH the modification of variable inside loops aren't lost (that is not the case with bourne shell).
Try the following script:
[CODE]var=init
ls | while read file
do
var="Last file is $file"
done



Jean-Pierre.

Last edited by aigles; 10-13-2006 at 08:46 AM..
Closed Thread

Bookmarks

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 02:53 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