![]() |
|
|
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 |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|