Shell Variable visibility


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell Variable visibility
# 1  
Old 01-13-2017
Shell Variable visibility

Dear All,

Saying, I have two distinct functions with the same goal (counting lines containing a specific pattern in a file MyFile).

To perform that operation, I used a "while loop" with two different syntax ("grep" command would be much more better in that case but this is not the concern in our case) !:
(1) cat MyFile | while read -r line;do; ... ;done;

(2) while read -r line; do; ...;done < MyFile;
Inside the while loop, I used a variable var to store the result.

My questions:

(1) is that the expected behaviour to not be able to get the value of var outside the while for the first syntax ?
(2) what kind of system's difference between both syntax ?

Thanks a lot for your attention, Smilie

cf. examples below:

Code:
[x004191a@xsnl11p317a log]$ echo $SHELL
/bin/bash
[x004191a@xsnl11p317a log]$ fnct_dae_V1 ()
> {
>
>   var=0
>
>   cat "STG_INSTNCE_COMPLTED_01_451_20170112193018.log" | while read -r line
>   do
>
>     if [[ $(echo "$line" | grep -i 'completed') ]]
>     then
>
>       ((var++))
>
>     echo "- INSIDE WHILE:var:$var"
>
>     fi
>
>   done
>
>   echo "- OUTSIDE WHILE:var:$var"
>
> }
[x004191a@xsnl11p317a log]$ fnct_dae_V1
- INSIDE WHILE:var:1
- INSIDE WHILE:var:2
- INSIDE WHILE:var:3
- INSIDE WHILE:var:4
- OUTSIDE WHILE:var:0
[x004191a@xsnl11p317a log]$
[x004191a@xsnl11p317a log]$
[x004191a@xsnl11p317a log]$
[x004191a@xsnl11p317a log]$
[x004191a@xsnl11p317a log]$ fnct_dae_V2 ()
> {
>
>   var=0
>
>   while read -r line
>
>   do
>
>     if [[ $(echo "$line" | grep -i 'completed') ]]
>     then
>
>       ((var++))
>
>     echo "- INSIDE WHILE:var:$var"
>
>     fi
>
>   done < STG_INSTNCE_COMPLTED_01_451_20170112193018.log
>
>   echo "- OUTSIDE WHILE:var:$var"
>
> }
[x004191a@xsnl11p317a log]$ fnct_dae_V2
- INSIDE WHILE:var:1
- INSIDE WHILE:var:2
- INSIDE WHILE:var:3
- INSIDE WHILE:var:4
- OUTSIDE WHILE:var:4
[x004191a@xsnl11p317a log]$

# 2  
Old 01-13-2017
In the first example, pipe | creates a subshell outside your current shell to execute a loop.
After the while loop is done, var is 0 in your current shell, since the increment was done in subshell.

The second example increments the var in current shell, so everything is known to the program.

Hope that helps
Regards
Peasant.
These 2 Users Gave Thanks to Peasant For This Post:
# 3  
Old 01-13-2017
A way to get around this (for demonstration sake, because
Code:
done < STG_INSTNCE_COMPLTED_01_451_20170112193018.log

is the superior approach to UUOC and there are better ways than using the grep -i inside the loop):
Code:
fnct_dae_V1 ()
{
  var=0
  cat "STG_INSTNCE_COMPLTED_01_451_20170112193018.log" | 
  {
    while read -r line
    do
      if [[ $(echo "$line" | grep -i 'completed') ]]
      then
        ((var++))
        echo "- INSIDE WHILE:var:$var"
      fi
    done
    echo "- OUTSIDE WHILE:var:$var"
  }
}

These 2 Users Gave Thanks to Scrutinizer For This Post:
# 4  
Old 01-13-2017
If you do not have a file but a command to be processed in the loop the corresponding construct will be helpful.

Instead of < file use < <( command )
This User Gave Thanks to stomp For This Post:
# 5  
Old 01-13-2017
Many thanks to all of you for your answers ...

Few comments I would like to add:

1. Peasant, I do not think a subshell is created when using that syntax: in fact, I show the PID for both functions and I do not notice a specific PID for "while loop" with the first function:

fnct_dae_V1:


Code:
[x004191a@xsnl11p317a log]$ fnct_dae_V1 ()
> {
>
>   echo "PID_Fonction: $$"
>
>   var=0
>
>   cat "STG_INSTNCE_COMPLTED_01_451_20170112193018.log" | while read -r line
>   do
>
>     if [[ $(echo "$line" | grep -i 'completed') ]]
>     then
>
>       ((var++))
>
>     echo "- INSIDE WHILE:var:$var"
>     echo "PID_WHILE: $$"
>
>     fi
>
>   done
>
>   echo "- OUTSIDE WHILE:var:$var"
>
> }
[x004191a@xsnl11p317a log]$ fnct_dae_V1
PID_Fonction: 15468
- INSIDE WHILE:var:1
PID_WHILE: 15468
- INSIDE WHILE:var:2
PID_WHILE: 15468
- INSIDE WHILE:var:3
PID_WHILE: 15468
- INSIDE WHILE:var:4
PID_WHILE: 15468
- OUTSIDE WHILE:var:0
[x004191a@xsnl11p317a log]$


fnct_dae_V2:


Code:
[x004191a@xsnl11p317a log]$ fnct_dae_V2 ()
> {
>
>   echo "PID_Fonction: $$"
>
>   var=0
>
>   while read -r line
>
>   do
>
>     if [[ $(echo "$line" | grep -i 'completed') ]]
>     then
>
>       ((var++))
>
>     echo "- INSIDE WHILE:var:$var"
>     echo "PID_WHILE: $$"
>
>     fi
>
>   done < STG_INSTNCE_COMPLTED_01_451_20170112193018.log
>
>   echo "- OUTSIDE WHILE:var:$var"
>
> }
[x004191a@xsnl11p317a log]$ fnct_dae_V2
PID_Fonction: 15468
- INSIDE WHILE:var:1
PID_WHILE: 15468
- INSIDE WHILE:var:2
PID_WHILE: 15468
- INSIDE WHILE:var:3
PID_WHILE: 15468
- INSIDE WHILE:var:4
PID_WHILE: 15468
- OUTSIDE WHILE:var:4
[x004191a@xsnl11p317a log]$

2. Scrutinizer, I applied your suggestion (illustration below with fnct_dae_V3) but, in that case, it seems that I can not keep the value of the variable with that syntax:

fnct_dae_V3:


Code:
[x004191a@xsnl11p317a log]$ fnct_dae_V3 ()
> {
>
>   echo "PID_Fonction: $$"
>
>   var=0
>
>   cat "STG_INSTNCE_COMPLTED_01_451_20170112193018.log" |
>
>   {
>
>     while read -r line
>     do
>
>       if [[ $(echo "$line" | grep -i 'completed') ]]
>       then
>
>         ((var++))
>
>       echo "- INSIDE WHILE:var:$var"
>       echo "PID_WHILE: $$"
>
>       fi
>
>     done
>
>   }
>
>   echo "- OUTSIDE WHILE:var:$var"
>
> }
[x004191a@xsnl11p317a log]$ fnct_dae_V3
PID_Fonction: 15468
- INSIDE WHILE:var:1
PID_WHILE: 15468
- INSIDE WHILE:var:2
PID_WHILE: 15468
- INSIDE WHILE:var:3
PID_WHILE: 15468
- INSIDE WHILE:var:4
PID_WHILE: 15468
- OUTSIDE WHILE:var:0
[x004191a@xsnl11p317a log]$

So, I allow myself to ask you one more time: how is it possible to lose the value of the variable if the whole execution use a unique PID (cf. fnct_dae_V1 and I assume fnct_dae_V3) ?

Thanks again, Smilie
# 6  
Old 01-13-2017
This User Gave Thanks to jgt For This Post:
# 7  
Old 01-14-2017
Quote:
Originally Posted by dae
Many thanks to all of you for your answers ...

Few comments I would like to add:

1. Peasant, I do not think a subshell is created when using that syntax: in fact, I show the PID for both functions and I do not notice a specific PID for "while loop" with the first function:

fnct_dae_V1:


Code:
[x004191a@xsnl11p317a log]$ fnct_dae_V1 ()
> {
>
>   echo "PID_Fonction: $$"
>
>   var=0
>
>   cat "STG_INSTNCE_COMPLTED_01_451_20170112193018.log" | while read -r line
>   do
>
>     if [[ $(echo "$line" | grep -i 'completed') ]]
>     then
>
>       ((var++))
>
>     echo "- INSIDE WHILE:var:$var"
>     echo "PID_WHILE: $$"
>
>     fi
>
>   done
>
>   echo "- OUTSIDE WHILE:var:$var"
>
> }
[x004191a@xsnl11p317a log]$ fnct_dae_V1
PID_Fonction: 15468
- INSIDE WHILE:var:1
PID_WHILE: 15468
- INSIDE WHILE:var:2
PID_WHILE: 15468
- INSIDE WHILE:var:3
PID_WHILE: 15468
- INSIDE WHILE:var:4
PID_WHILE: 15468
- OUTSIDE WHILE:var:0
[x004191a@xsnl11p317a log]$


fnct_dae_V2:


Code:
[x004191a@xsnl11p317a log]$ fnct_dae_V2 ()
> {
>
>   echo "PID_Fonction: $$"
>
>   var=0
>
>   while read -r line
>
>   do
>
>     if [[ $(echo "$line" | grep -i 'completed') ]]
>     then
>
>       ((var++))
>
>     echo "- INSIDE WHILE:var:$var"
>     echo "PID_WHILE: $$"
>
>     fi
>
>   done < STG_INSTNCE_COMPLTED_01_451_20170112193018.log
>
>   echo "- OUTSIDE WHILE:var:$var"
>
> }
[x004191a@xsnl11p317a log]$ fnct_dae_V2
PID_Fonction: 15468
- INSIDE WHILE:var:1
PID_WHILE: 15468
- INSIDE WHILE:var:2
PID_WHILE: 15468
- INSIDE WHILE:var:3
PID_WHILE: 15468
- INSIDE WHILE:var:4
PID_WHILE: 15468
- OUTSIDE WHILE:var:4
[x004191a@xsnl11p317a log]$

2. Scrutinizer, I applied your suggestion (illustration below with fnct_dae_V3) but, in that case, it seems that I can not keep the value of the variable with that syntax:

fnct_dae_V3:


Code:
[x004191a@xsnl11p317a log]$ fnct_dae_V3 ()
> {
>
>   echo "PID_Fonction: $$"
>
>   var=0
>
>   cat "STG_INSTNCE_COMPLTED_01_451_20170112193018.log" |
>
>   {
>
>     while read -r line
>     do
>
>       if [[ $(echo "$line" | grep -i 'completed') ]]
>       then
>
>         ((var++))
>
>       echo "- INSIDE WHILE:var:$var"
>       echo "PID_WHILE: $$"
>
>       fi
>
>     done
>
>   }
>
>   echo "- OUTSIDE WHILE:var:$var"
>
> }
[x004191a@xsnl11p317a log]$ fnct_dae_V3
PID_Fonction: 15468
- INSIDE WHILE:var:1
PID_WHILE: 15468
- INSIDE WHILE:var:2
PID_WHILE: 15468
- INSIDE WHILE:var:3
PID_WHILE: 15468
- INSIDE WHILE:var:4
PID_WHILE: 15468
- OUTSIDE WHILE:var:0
[x004191a@xsnl11p317a log]$

So, I allow myself to ask you one more time: how is it possible to lose the value of the variable if the whole execution use a unique PID (cf. fnct_dae_V1 and I assume fnct_dae_V3) ?

Thanks again, Smilie
You did not apply Scrutinizer's suggestion. And, a subshell and a separate process are two distinct things. And, again, the response is exactly the same. Using cat and a pipeline to feed input into your while read loop places the while read loop in a subshell (when you are using bash and many other shells). Get rid of the unneeded cat (which creates an extra process, drains system resources, and slows down your script without serving any useful purpose) and the subshell, and it will work just fine. Or, if you use a Korn shell (ksh) instead of the shell you're currently using, the last stage of a pipeline will be run in the current shell execution environment instead of in a subshell environment. (But, I haven't checked to see if you are using any other extensions to the POSIX standard shell requirements that might be treated differently by ksh than they are in the shell you're currently using.)
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 Replies

2. Programming

Visibility of X11 windows

Does anyone know if it is possible to check whether a window in x11 (using xlib) is visible or not? I tried XGetWindowProperty, but that doesn't work, because the windows host (dtwm on Solaris 10) does not support EWMH. (5 Replies)
Discussion started by: JenniferKuiper
5 Replies

3. Shell Programming and Scripting

Script to convert csv file to html with good visibility

Hi, I have Below script which converts csv file to html succesfully.but the visiblity is simple in black n white. I want to have better visibilty of each columns in different colours(like green).As it is a Database report suppose some tablespace available space is less than 20% then it should... (7 Replies)
Discussion started by: sv0081493
7 Replies

4. Shell Programming and Scripting

[SHELL: /bin/sh] For loop using variable variable names

Simple enough problem I think, I just can't seem to get it right. The below doesn't work as intended, it's just a function defined in a much larger script: CheckValues() { for field in \ Group_ID \ Group_Title \ Rule_ID \ Rule_Severity \ ... (2 Replies)
Discussion started by: Vryali
2 Replies

5. Shell Programming and Scripting

Not able to store command inside a shell variable, and run the variable

Hi, I am trying to do the following thing var='date' $var Above command substitutes date for and in turn runs the date command and i am getting the todays date value. I am trying to do the same thing as following, but facing some problems, unique_host_pro="sed -e ' /#/d'... (3 Replies)
Discussion started by: gvinayagam
3 Replies

6. Shell Programming and Scripting

Shell assign variable to another variable

How can I assign a variable to an variable. IE $car=honda One way I can do it is export $car=honda or let $car=2323 Is there any other ways to preform this task (3 Replies)
Discussion started by: 3junior
3 Replies

7. Shell Programming and Scripting

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 Replies

8. Shell Programming and Scripting

visibility of a variable in Perl script.

I am writing a script to cross check the dbscript. For that I am searching the SQL manipulators in the dbscript as shown below. But my problem is the variable $pattern is coming as null when comes out of the foreach loop. File content: ========= vi /home2/niroj_p/dbscript.sql -------... (1 Reply)
Discussion started by: Niroj
1 Replies

9. Shell Programming and Scripting

Enviornment Variable in B shell (I call it nested variable)

#!/bin/sh APP_ROOT_MODE1=/opt/app1.0 APP_ROOT_MODE2=/opt/app2.0 APP_ROOT=${APP_ROOT_${APP_MODE}} # enviornment variable APP_MODE will be exported in the terminal where # we run the applciation, its value is string - MODE1 or MODE2 # My intension is: # when export APP_MODE=MODE1... (4 Replies)
Discussion started by: princelinux
4 Replies

10. Shell Programming and Scripting

set variable with another variable? c shell

okay, this shouldn't be difficult but I can't figure it out. How can I set a variable with another variable. I have the following: foreach pe ($dir $sp) set tpe = `echo $pe | grep M` if ($tpe == M) then set ${$pe} = M <--- This doesn't work else endif end In this case what... (2 Replies)
Discussion started by: wxornot
2 Replies
Login or Register to Ask a Question