Simpler Navigation for Servers and Operating Systems
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
General
cancel
Showing results for 
Search instead for 
Did you mean: 

Pass local variable to global variable

Michael Yu_2
Occasional Visitor

Pass local variable to global variable

Hello All,

Please throw some lights to the new guy. I have a shell script as following: The idea of the program is to get the value of $cur_uid from the IF loop, assign it to uidcurrent. In my WRK_FILE, I only have one line. It either equal to UID1 or UID2. As you may see in my program, I couldn't get the ideal results. I have tried diffent shells. It just won't work. Your help will be highly appreciated.

WRK_FILE=$newdbs_home/bin/.configdlyidcheck
TMP_FILE=/var/tmp/tmp.txt
uidcurrent=""
flag=""
cur_uid=""

if [[ -s $WRK_FILE ]]; then
cat $WRK_FILE | while read line
do
flag=$line
echo flag: $flag
if [ $flag = $UID1 ]; then
cur_uid=$flag #### this variable is used for echo/testing only
nxt_uid=$UID2
echo current uid in $WRK_FILE is: $cur_uid, and the next uid is: $nxt_uid > $TMP_FILE
echo $nxt_uid > $WRK_FILE
pg $WRK_FILE
elif [ $flag = $UID2 ]; then
cur_uid=$flag ##### this line is for testing only
nxt_uid=$UID1
echo current uid in $WRK_FILE is: $cur_uid, and the next uid is: $nxt_uid > $TMP_FILE
echo $nxt_uid > $WRK_FILE
pg $WRK_FILE ##### this line is for testing only
else
echo "The $WRK_FILE file is damaged and please check the load shell program" > $TMP_FILE
fi
echo "The Current User ID is: $cur_uid"
# uidcurrent=$cur_uid
# echo "The UIDCURRENT is: $uidcurrent"

done

uidcurrent=$cur_uid
echo "The UIDCURRENT is: $uidcurrent"
else
echo "The $WRK_FILE file does not exist" ###> $TMP_FILE
/bin/mail -s "The $WRK_FILE is damaged and can not load the data" ${mlist} < $TMP_FILE

fi

echo "the cur_uid is: $cur_uid" > /var/tmp/test2.txt
echo "The UIDCURRENT is: $uidcurrent" >> /var/tmp/test2.txt
5 REPLIES
Alex Lavrov.
Honored Contributor

Re: Pass local variable to global variable

Yep, I ran into this annoying problem when I ported scripts from HPUX to Linux.

Here is the solution:
http://www.faqs.org/faqs/unix-faq/shell/bash/

Refer to question *E4*
I don't give a damn for a man that can only spell a word one way. (M. Twain)
Michael Yu_2
Occasional Visitor

Re: Pass local variable to global variable

Thank you so much Alex. My problem actually is to pass out the value of $cur_uid in the if loop to outside. I would say a local variable in if loop to pass its value to a global variable in the main program. In my case, I would pass the value of $cur_uid to $uidcurrent. I know some of the platform works in that way. But not in my Linux. Any thoughts
Dave Falloon
Trusted Contributor

Re: Pass local variable to global variable

When things get tough in shell I move to a more complete language such as perl or python, usually python. Setting a global variable in python is easy just prefix it with global:

global foo = bar

www.python.org

--Dave
Clothes make the man, Naked people have little to no effect on society
Alex Lavrov.
Honored Contributor

Re: Pass local variable to global variable

Well, the only way to do it is to get rid of the pipe and while loop. Try to replace it with something else (for loop for example) or just put it in some temp file and then read from it :) (ugly but effective)
I don't give a damn for a man that can only spell a word one way. (M. Twain)
Michael Yu_2
Occasional Visitor

Re: Pass local variable to global variable

Thank you guys. I have got rid of the while loop. Instead, I only use the if/then/else. It works now. But the code is really ugly.