Value of shell variable cannot be retrieved.

Value of shell variable cannot be retrieved.

Post by Patrick L » Thu, 13 Sep 2001 18:01:51



Hi,

I got the following codes in my shell script (using /bin/sh) :

#!/bin/sh

this_count=0;export this_count
while read ip other
do
   if [ $PREV_IP != $ip ]
   then
      PREV_IP=$ip;export PREV_IP
      printf "IP %s has visited\n" $ip
      echo $this_count
      this_count=`expr $this_count + 1`;export this_count
   fi
done < $TEMP_FILE
echo value of count is $this_count

I expected that I could get the value of count "this_count" after loop
but actually, I only got "0" as the value, no matter how much iteration
the loop has performed.

Can anyone tell me what's happened to this shell script? How can I
retrieve values from the loop?

Thanks a lot.

--
Patrick

 
 
 

Value of shell variable cannot be retrieved.

Post by Vincent Smeet » Thu, 13 Sep 2001 18:47:37




> #
> #   #!/bin/sh
> #
> #   this_count=0;export this_count
> #   while read ip other
> #   do
> #      if [ $PREV_IP != $ip ]
> #      then
> #         PREV_IP=$ip;export PREV_IP
> #         printf "IP %s has visited\n" $ip
> #         echo $this_count
> #         this_count=`expr $this_count + 1`;export this_count
> #      fi
> #   done < $TEMP_FILE
> #   echo value of count is $this_count
> #
> #   I expected that I could get the value of count "this_count" after loop
> #   but actually, I only got "0" as the value, no matter how much iteration
> #   the loop has performed.

The problem is that the while loop is started in a sub-shell because of
the input redirection. The variable is set in the sub-shell but doesn't
get out to the parent-shell.

Quote:

> Either put an "exec <$TEMP_FILE" before the loop and
> take it off the "done", or use ksh.

-- Vincent Smeets                    SchlumbergerSema
-- Tel. : +49(5931)805-461           Competence Center Informatik GmbH
-- Fax  : +49(5931)805-175           Postfach 1225

-- PGP fingerprint: E2437E38AAA9CA7D A31E7D751F1B6454 8AED7B76

 
 
 

Value of shell variable cannot be retrieved.

Post by Cameron Ke » Fri, 14 Sep 2001 14:31:18


|>

In addition to the other fixes...

|> #
|> #   #!/bin/sh
|> #
|> #   this_count=0;export this_count

You shouldn't need to export these variables.

|> #      if [ $PREV_IP != $ip ]

In the first iteration. $PREV_IP will evaluate to nothing, and you will
get a syntax error. Reference it like this

        if [ "$PREV_IP" != $ip ]

It wouldn't hurt to do this with ip as well.

./tmp.sh: [: !=: unary operator expected

|> #   done < $TEMP_FILE

TEMP_FILE is undeclared. Perhaps is is meant to be provided in the
environment. Either way, you should check for its presence when the script
starts up.

$ TEMP_FILE=tmp.tmp ./tmp.sh
IP 123.123.123.123 has visited
0
IP 234.24.324.243 has visited
1
value of count is 2

--

Website (recently moved) http://homepages.paradise.net.nz/~cameronk/
--

 
 
 

Value of shell variable cannot be retrieved.

Post by Patrick L » Sat, 15 Sep 2001 15:09:15





> > #
> > #   #!/bin/sh
> > #
> > #   this_count=0;export this_count
> > #   while read ip other
> > #   do
> > #      if [ $PREV_IP != $ip ]
> > #      then
> > #         PREV_IP=$ip;export PREV_IP
> > #         printf "IP %s has visited\n" $ip
> > #         echo $this_count
> > #         this_count=`expr $this_count + 1`;export this_count
> > #      fi
> > #   done < $TEMP_FILE
> > #   echo value of count is $this_count
> > #
> > #   I expected that I could get the value of count "this_count" after loop
> > #   but actually, I only got "0" as the value, no matter how much iteration
> > #   the loop has performed.

> The problem is that the while loop is started in a sub-shell because of
> the input redirection. The variable is set in the sub-shell but doesn't
> get out to the parent-shell.

If so, what can I do in order to get the value of variable $this_count?

Besides, I would like to supplement the information that the variable $TEMP_FILE
has been defined in the shell but I did not include the statement in my sample.

--
Patrick

 
 
 

1. Better Way to Get Value of Value of Variable in Bourne shell?

Hi

I need to create a Bourne shell function that can change/manipulate the values
of the given variable names, e.g.

  $ P1=John
  $ P2=Jane
  $ P3=Paul

  $ echo "1=$P1, 2=$P2, 3=$P3."
  1=John, 2=Jane, 3=Paul.

  $ add_prefix Hi_ P1 P2 P3

  $ echo "1=$P1, 2=$P2, 3=$P3."
  1=Hi_John, 2=Hi_Jane, 3=Hi_Paul.

After several trial-and-error, I finally got the add_prefix() working, but I
think my solution is too complicated and inefficient, specifically the
"eval $var='$prefix`eval echo \\$$var`'" that takes 2 evals and 1 command
substitution, as shown below.

  add_prefix () {
    prefix=$1
    shift
    for var in $*; do
      eval $var='$prefix`eval echo \\$$var`'
    done
  }

Can any Bourne shell programming guru show me a more concise, efficient and
elegant way in writing the add_prefix() function without using any external
command?

Is there any shorter syntax like ${$var} in Bourne shell that can replace
`eval echo \\$$var` ?  Something concise and simpler will be helpful because
the actual function I need to write is much more complicated than add_prefix()
function shown above.

Thanks.  I appreciate your help.

Regards
Hon-Chi

2. sendmail help

3. c shell script; Howto use environement variables to build environment variables and take their value

4. Does Linux run on Apple's PowerBook?

5. detecting difference between unset variable and variable whose value is null in Korn Shell

6. please help me find out what happened to my system since I upgr

7. Expanding a shell variable that may contain shell variables.

8. POP Mail Server - How to get ?

9. Why rsh can't retrun value of the shell variable of the remote machine ?

10. How do you set a variable's initial value in Bourne or tcsh shells?

11. Assigning an exit value to a Bourne shell variable?

12. Variables getting the return value in Korn shell?

13. URGENT: C-shell Default Value for a Variable