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
Variable Substitution Issue calredd SUN Solaris 4 02-16-2008 11:42 AM
sed variable issue wisher115 Shell Programming and Scripting 2 11-27-2006 05:34 PM
linux firewall / dns issue frankkahle UNIX for Advanced & Expert Users 1 06-13-2006 01:53 AM
Variable for -name causing issue in Find command ParNone UNIX for Dummies Questions & Answers 2 03-24-2006 06:48 PM
Security issue with TCP SYN packets on Linux bert.n Linux 2 03-22-2005 12:38 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 09-28-2005
jasmeet100 jasmeet100 is offline
Registered User
  
 

Join Date: Sep 2005
Posts: 3
ksh/Linux: Variable scoping issue? Pl. help!


Code:
user_account()
{
  set -x
  nodename=$1
# set userid to user0
  userid="user0"
  echo outside:pid $$
  cat $MY_DIR/user_accounts | while read line
     do
#       line="node1 user1"
        echo inside do: pid $$ line:$line userid:$userid
        poss_node=`echo $line |awk '{print $1}'`
        echo after node: pid $$:userid:$userid
        userid=`echo $line | awk '{print $2}'`
        echo inside useraccount: pid $$: userid $userid
# print userid here - prints user1 & user2 in the 2 iterations.
        if [[ $poss_node = $nodename ]]
          then
            echo before break: pid $$: userid : $userid
#prints user2 - fine.
            break
           fi
     done
  echo End: pid $$ : userid:$userid
#prints user0 here ???????????
}

After this function is called, userid is left as "user0". Although inside the while loop, if I print userid, it printd "user1" and "user2".

Note: MY_DIR/user_accounts is a text file with 2 lines:

node1 user1
node2 user2

If I comment out cat & read and instead manually assign line as "node1 user1", userid gets set to user1 correctly. What could be wrong here? Is it a variable scoping issue? I printed pid after each line, and it is the same pid. Also, this script runs fine in Solaris. It has issues only in Linux. Pl. help.
-Jasmeet

Last edited by Perderabo; 09-28-2005 at 04:16 PM.. Reason: Disable simlies and add code tags for readability
  #2 (permalink)  
Old 09-28-2005
tmarikle tmarikle is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2005
Posts: 683
Your "read" is occurring in a subshell and your variable is not visible to the parent.

There are several ways to work around this. Depending on the size of the data, I will use an array.

Code:
IFS='
'
set -A ARRAY $(< MY_DIR/user_accounts)
for i in ${ARRAY[@]}
do
   ...
done

  #3 (permalink)  
Old 09-30-2005
jasmeet100 jasmeet100 is offline
Registered User
  
 

Join Date: Sep 2005
Posts: 3
Thanks it worked.


J
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 09:04 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