ksh/Linux: Variable scoping issue? Pl. help!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh/Linux: Variable scoping issue? Pl. help!
# 1  
Old 09-28-2005
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  
Old 09-28-2005
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  
Old 09-30-2005
Thanks it worked.


J
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash - concatenate string - strange variable scoping

Hello, I am trying to concatenate a string in a bash script like this: runCmd="docker run -e \"IMAGE_NAME=$IMAGE_NAME\" " env | grep "$ENV_SUFFIX" | while read line; do envCmd="-e \"${line}\" " runCmd=$runCmd$envCmd echo $runCmd # here concatenation works fine done echo... (3 Replies)
Discussion started by: czabak
3 Replies

2. Shell Programming and Scripting

ksh - variable to be set to windows path issue

Greetings Experts, I need to pass a parameter to ksh and the value is windows path eg: sh abc.txt C:\Users\chill3chee\Desktop No matter I try with \ delimiter, still could not get this exact value assigned to the shell variable which was checked with echo. Tried with using... (2 Replies)
Discussion started by: chill3chee
2 Replies

3. Shell Programming and Scripting

ksh while read issue

Hello, I have used a chunk of ksh script similar to this in many places without any issue: while : do print; read OPTION?"Enter a number (q to quit): " expr ${OPTION} + 1 >/dev/null 2>&1 CHECKVAL=$? if }" != ${OPTION} ]; then ... (2 Replies)
Discussion started by: port43
2 Replies

4. Shell Programming and Scripting

Variable value substitution issue with awk command issue

Hi All, I am using the below script which has awk command, but it is not returing the expected result. can some pls help me to correct the command. The below script sample.ksh should give the result if the value of last 4 digits in the variable NM matches with the variable value DAT. The... (7 Replies)
Discussion started by: G.K.K
7 Replies

5. Shell Programming and Scripting

Variable to command to Variable Question KSH

Hello, First post for Newbie as I am stumped. I need to get certain elements for a specific PID from the ps command. I am attempting to pass the value for the PID I want to retrieve the information for as a variable. When the following is run without using a variable, setting a specific PID,... (3 Replies)
Discussion started by: Coyote270WSM
3 Replies

6. Shell Programming and Scripting

-z in ksh issue

Hi, Can any body please tell me what does -z do in ksh? I need to understand what does below code do? FILE_LIST is an array which store multiple number of files... if } ]]; then echo "EVDO file not found fi I hope I'm clear on my query (3 Replies)
Discussion started by: annybase
3 Replies

7. Shell Programming and Scripting

AWK Variable assignment issue Ksh script

Hi There, I am writing a ksh script which assigns variable values from file "A" and passes that variables to file "B". While passing the parameters an additional "$" sign is being assigned to awk -v option. Could any one help me with this please. #!/bin/ksh head -1... (3 Replies)
Discussion started by: Jeevanm
3 Replies

8. Shell Programming and Scripting

Shell variable scoping

This may be a stupid question, but was wondering if it is possible to make a variable local to a particular script and invisible to an external script that may source the script where it is defined? Thanks as always (2 Replies)
Discussion started by: stevensw
2 Replies

9. Shell Programming and Scripting

I can't stop the scoping problem (perl)

I hate to post something so n00bish, but I'm pretty n00bish when it comes to perl so here it goes: $var=1; while(1){ if ($var == 2){ last; } $var=2; } works the way I intend it to, breaking the infinite loop after the first time through, but: (2 Replies)
Discussion started by: thmnetwork
2 Replies

10. Shell Programming and Scripting

ksh: A part of variable A's name is inside of variable B, how to update A?

This is what I tried: vara=${varb}_count (( vara += 1 )) Thanks for help (4 Replies)
Discussion started by: pa3be
4 Replies
Login or Register to Ask a Question