Help with retaining variable scope

Hi,

I use Korn Shell. Searched Forum and modified the way the file is input to the while loop, but still the variable does not seem to be retaining the final count.

while read name
        do
                Tmp=`echo $name | awk '{print $9 }'`
                Count=`cat $Tmp | wc -l`
                echo $Count
                Value=`expr $Value + $Count`
                echo "Value is  $Value"
        done < filelist
        echo "Value=$Value"

$ sh file.sh
329
Value is  329
Value=0

Can anyone please tell me y the value is not retained.

Try:

./file.sh

When you invoke the script as sh file.sh the shebang line is ignored.

1 Like

Wow!! it works fine..

But im suprised at the fact that my parent shell is a Korn shell too, so even if the shebang line was ignored it should have worked the same way right...

echo $SHELL
/usr/bin/ksh
$ ./file.sh
329
Value is  329
Value=329

Thanks anyways.. ill keep this in mind!

No, unless you invoke the script as:

ksh <script_name>

sh is a KornShell variant by default on limited number of systems (it is on HP-UX for example, on Solaris it's the old Bourne shell, on Linux it's usually a link to bash (on RedHat based systems) or dash (on Debian based systems)).

Consider also that the SHELL variable usually contains your login shell which may or may not be your current shell.

1 Like

Thanks radoulov for this useful information.

I respectfully disagree with radoulov. The reason, why your value is not retained is because it is incremented in a subshell. This is not because of the shebang line being ignored or not, but because any command - and "sh" is a command too - is invoking its own process.

You will notice that inside your process the value is retained all right. Consider the following example script:

#!/bin/ksh

typeset -i iValue=0
typeset   chFile=""

print - "Value before: $iValue"

cat filelist | while read chFile ; do
     (( value += 1 ))
done

print - "Value after: $iValue"

exit 0

But you didn't try to retain the variable throughout a script but you inherited the variable from your commandline environment, modified it in your script and expected it to change it in the orginal commandline environment.

The problem is that when an environment inherits values it does not get the originals but copies of them - with the same names and values but still copies. If you modifiy them you are still modifying the copies, not the originals.

You can overcome this by using the ".", which tells the shell not to start a subshell but execute the script in the environment it already has:

# sh /some/script.sh
# . sh /some/script.sh

I hope this helps.

bakunin

Hi bakunin,
if I'm not missing something, in the OP's example the value of the variable is not incremented in a subshell
(unless executed by the Bourne shell, because it seems that sh uses a subshell for redirected loops):

while read ...
Value=`expr $Value + $Count`
...
echo "Value=$Value"

Something like:

$ unset v
$ while read ; do v=$REPLY; done<<<1
$ echo $v
1

The OP is verifying the value of the variable inside the script, not outside of it.

Your cat ... | while read ... example demonstrates another KornShell feature: the last builtin command in a pipeline being executed in the parent shell.

Consider the following examples:

$ echo ok > infile
$ sh -c 'cat infile | while read v; do myvar=$v;done; echo $myvar'

$ bash -c 'cat infile | while read v; do myvar=$v;done; echo $myvar'

$ ksh -c 'cat infile | while read v; do myvar=$v;done; echo $myvar'
ok

This is because of the previously mentioned KornShell feature.

And this is the OP's case:

$ echo ok > infile
$ sh -c 'while read v; do myvar=$v;done<infile; echo $myvar'

$ ksh -c 'while read v; do myvar=$v;done<infile; echo $myvar'
ok
$ bash -c 'while read v; do myvar=$v;done<infile; echo $myvar'
ok

In this case the old Bourne shell behaves differently.

Regards
radoulov

P.S. Actually this aspect of some implementations of the Bourne shell is covered in the FAQ.