?
Solved

variable scopes in shell

Posted on 2005-03-09
15
Medium Priority
?
259 Views
Last Modified: 2010-04-21
Hi experts,

Consider the following piece of code

##########################################################
typeset PROCESS_FLAG=""
find $DIR  -type f -mtime +${DAYS_TO_SCAN[$j]} -size 0 -print -follow |egrep -f ${INCLUDE_FILE}|while read FILE
       do
          PROCESS_FLAG=T
          rm -f $FILE && echo "INFO: $FILE deleted" 2>/dev/null
       echo "Inside : Process flag : $PROCESS_FLAG"      
done
echo "Outside : Process flag : $PROCESS_FLAG"
############################################################3

Assuming that the find *does* find a file, that file will be deleted and the value of PROCESS_FLAG will be set to T.
What I find from the output of this is that although IT IS deleting the file, the PROCESS_FLAG value outside the loop is set to nul string..

corresponding output
#######################
Inside : Process flag : T
Outside : Process flag :
########################

Does this mean that scopes apply and inside the loop, the inner PROCESS_FLAG is updated and outside the loop, the inner PROCESS_FLAG is no longer visible......??

Manav
0
Comment
Question by:manav_mathur
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
  • +3
15 Comments
 
LVL 16

Author Comment

by:manav_mathur
ID: 13494218
This is on pdksh

0
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 200 total points
ID: 13494276
Hi manav_mathur,

The pipe for sure causes it to execute in a different shell and hence the inner PROCESS_FLAG is a different variable from outer PROCESS_FLAG.
I had had the same problem when I was relatively new ... check the link ;-)
http://www.experts-exchange.com/Programming/Programming_Platforms/Linux_Programming/Q_20681247.html

Cheers!
sunnycoder
0
 
LVL 16

Author Comment

by:manav_mathur
ID: 13494357
Is similar behaviour observed on ksh (HPUX). Actually Im testing a script designed for solaris on a local LINUX server.....

The script was supposed to be running fine on a Solaris production server and I am supposed to reconfigure and migrate it to a new HPUX.
And the script has commands like
 - trap -9 .....
 - the above problem.........

Dont know what Im gonna do..... :(

Manav


0
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
LVL 45

Expert Comment

by:sunnycoder
ID: 13494391
Hi manav_mathur,

I am not sure about behavior on each and every environment but this behavior is quite common if not general.
You can try communicating between the shells using some temporary files.

Cheers!
sunnycoder
0
 
LVL 16

Author Comment

by:manav_mathur
ID: 13494410
Fine. and thanx a lot...I'll wait if any other experts have anything else to post...especially regarding ksh on HPUX.....

Manav
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 13499805
This behaviour is typical of bash shell -- On Solaris/HP-UX this doess not happen!
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 13499884
we could be lucky that EE is mainly free of trolls, otherwise that would start the bash-flame-war again :-))
sorry for being off-topic
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 200 total points
ID: 13501295
This is a bash/sh behaviour.

$ uname -sr
SunOS 5.9
$ cat var

echo 1 | while read bar
do
  foo=set
  echo "Loop: $foo"
done

echo "Outside: $foo"
$ sh var
Loop: set
Outside:
$ bash var
Loop: set
Outside:
$ ksh var
Loop: set
Outside: set
0
 
LVL 3

Assisted Solution

by:mike_mian
mike_mian earned 200 total points
ID: 13503372
set it to an environment variable rather than typeset use export.

eg:

export PROCESS_FLAG=""
find $DIR  -type f -mtime +${DAYS_TO_SCAN[$j]} -size 0 -print -follow |egrep -f ${INCLUDE_FILE}|while read FILE
       do
          export PROCESS_FLAG=T
          rm -f $FILE && echo "INFO: $FILE deleted" 2>/dev/null
       echo "Inside : Process flag : $PROCESS_FLAG"    
done
echo "Outside : Process flag : $PROCESS_FLAG"
unset PROCESS_FLAG

that should make it work.
0
 
LVL 16

Author Comment

by:manav_mathur
ID: 13503416
mike_mian has a valid point here of using export here. But does typeset cause this problem or is it shell-related(or both)??

Manav
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 400 total points
ID: 13503657
stolen from man ksh

       Since functions are  executed  in  the  current  shell  environment,
       parameter  assignments  made  inside functions are visible after the
       function completes.  If this is not the desired effect, the  typeset
       command  can  be used inside a function to create a local parameter.
...
       If typeset  is  used  inside  a
              function, any newly created parameters are local to the func­
              tion.
...
               -x             Export attribute: parameters  (or  functions)
                              are placed in the environment of any executed
                              commands.  Exported functions are not  imple­
                              mented yet.

you may try to use  typeset -x  instead, not shure if it helps
And I guess that there is a difference between POSIX-compliant and other kshs.

Conclusion: the correct behaviour (according man-page) is as you described in your question, if export inside the function exports the variable to the outer namespace, I'd consider that a bug.

read man-pages :-))
0
 
LVL 16

Author Comment

by:manav_mathur
ID: 13503674
Probably as Im not using the -x option, the variable doesnt get exported. Hence the subshell creates its own variable and the outer variable is not affected.....am i right??

>read man-pages :-))
This is one piece of advice I should but never adhere to
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 13503846
> ...am i right?
thinks so, any other behaviour is a buggy shell, or another interpretation of what the man-page says
Keep in mind that the man-page look inside out, while your example does it outside in, which is not covered by the doc
.. another example of "gray area" ..
0
 
LVL 16

Author Comment

by:manav_mathur
ID: 13519193
>.. another example of "gray area" ..

just my luck to stumble across them :/

Anyways, thanx all for your valuable comments.

Manav
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 13519622
> just my luck to stumble across them :/
sounds like we're twins: I've the same luck, always, anywhere
;-)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question