Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 261
  • Last Modified:

variable scopes in shell

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
manav_mathur
Asked:
manav_mathur
  • 6
  • 4
  • 2
  • +3
4 Solutions
 
manav_mathurAuthor Commented:
This is on pdksh

0
 
sunnycoderCommented:
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
 
manav_mathurAuthor Commented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
sunnycoderCommented:
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
 
manav_mathurAuthor Commented:
Fine. and thanx a lot...I'll wait if any other experts have anything else to post...especially regarding ksh on HPUX.....

Manav
0
 
MikeOM_DBACommented:
This behaviour is typical of bash shell -- On Solaris/HP-UX this doess not happen!
0
 
ahoffmannCommented:
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
 
TintinCommented:
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
 
mike_mianCommented:
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
 
manav_mathurAuthor Commented:
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
 
ahoffmannCommented:
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
 
manav_mathurAuthor Commented:
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
 
ahoffmannCommented:
> ...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
 
manav_mathurAuthor Commented:
>.. another example of "gray area" ..

just my luck to stumble across them :/

Anyways, thanx all for your valuable comments.

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

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 6
  • 4
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now