Incrementing Variable resets outside of while loop

  1. The problem statement, all variables and given/known data:
    Variable is resetting to 0 after incrementing in while loop

My bit of scripting displays the current users logged in the machine.
Then it reads in a specific username and displays the processes for that user.

The portion that I am having trouble with is the error detection. It should detect an invalid username and display an error message.

I have done this by implementing a counter inside the while loop. If the number of processes is equal to 0 then the user does not exist or isnt running any processes.

The counter seems to work it increments inside the while loop, but after "done" the variable "count" appears to reset itself to 0.

So I guess my main question is:

Why is "count" resetting (or appearing to reset) back to 0?

Secondary Question:
Is there a workaround for this? I can think of none.

Thanks for your help

  1. Relevant commands, code, scripts, algorithms:
#!/bin/sh
      count=0
      who | cut -f1 -d" " > temp
      cat temp
      rm temp
      echo "Enter the user whose processes you would like to see"
      read username
      #ps -l -u $username 2>/dev/null
      ps -acefl | while read line
      do
        carl=`echo $line | cut -f3 -d" "`        
        if [ "$carl" = "$username" ]; then
          
          #count=`expr count=$count + 1`         
          (( count= $count + 1 ))

#count is displayed correctly
          echo $count 

          echo $line
        fi
      done
      echo $count 

      #at this point "count = 0"

      if [ $count == 0 ]; then
        echo "Either that user does not exist or is not running"
      fi
  1. The attempts at a solution (include all code and scripts):

I have moved the init around, I have changed way the increment was stated.

is that just the nature of the of variables? do they reset after "done"

  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

Missouri University of Science and Technology
Rolla, MO
Josh Wilkerson
CS284
CS 284: Operating Systems

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

It's because of the pipe.

#!/bin/sh
      count=0
      who | cut -f1 -d" " > temp
      cat temp
      rm temp
      echo "Enter the user whose processes you would like to see"
      read username
      #ps -l -u $username 2>/dev/null
      ps -acefl | while read line
      do
        carl=`echo $line | cut -f3 -d" "`        
        if [ "$carl" = "$username" ]; then
          
          #count=`expr count=$count + 1`         
          (( count= $count + 1 ))

#count is displayed correctly
          echo $count 

          echo $line
        fi
      done
      echo $count 

      #at this point "count = 0"

      if [ $count == 0 ]; then
        echo "Either that user does not exist or is not running"
      fi

When you put a code block behind a pipe, the entire block executes in a subshell. The subshell inherits a copy of the shell's current variables but cannot communicate them back(much like how processes inherit environment variables). So the loop runs fine but the number you want was never in the parent shell.

You could either accomplish the loop without a pipe (perhaps by saving the output of ps to a temporary file), or have the loop output values to stdout to be read further down the pipe chain.

P.S.: You could replace

      who | cut -f1 -d" " > temp
      cat temp
      rm temp

with

      who | cut -f1 -d" "

There seems to be no point to saving it to a temp file when all you want it to do is print to the console anyway.

Thanks, I thought it may be something similar to that.

It's good to know that about pipes.

Thanks again

????
Modified ps in order to get something on my HP-UX box
It didnt like the last if test...modified also...)

#!/bin/sh
      count=0
      who | cut -f1 -d" " > temp
      cat temp
      rm temp
      echo "Enter the user whose processes you would like to see"
      read username
      #ps -l -u $username 2>/dev/null
      ps -efl | while read line
      do
        carl=`echo $line | cut -f3 -d" "`
        if [ "$carl" = "$username" ]; then

          #count=`expr count="$count" + 1`
          (( count= $count + 1 ))

                #count is displayed correctly
          echo $count

          echo $line
        fi
      done

      echo "Count : "$count
      echo "That's all"

      #at this point "count = 0"

      if [ "$count" -eq 0 ]; then
        echo "Either that user does not exist or is not running"
      fi

and so execution:

"cpt_in_while" 32 lines, 741 characters 
ant:/home/vbe $ cpt_in_while   
vbe
suni
vbe
vbe
vbe
vbe
vbe
vbe
vbe
vbe
boss
dave
Enter the user whose processes you would like to see
vbe
1
2001 S vbe 19220 19187 3 168 24 6f6b9380 995 5f0af280 Jan 20 pts/2 91:10 top
2
2001 S vbe 19185 1 7 154 24 6d0c4300 226 b52c00 Jan 20 ? 15:55 /usr/bin/X11/dtterm -displ
3
1 S vbe 14049 19288 0 154 24 9fbc9180 264 b52c40 Jan 28 pts/0 0:00 ssh ramix02
4
1 S vbe 4082 19185 0 154 24 6dd3e0c0 91 a10e0128 Jan 25 pts/9 0:00 /usr/bin/ksh
5
1 R vbe 22371 22361 2 186 24 9cbc17c0 45 - 17:06:27 pts/6 0:00 ps -efl
6
1 S vbe 19231 19185 0 158 24 810c81c0 96 6bec9040 Jan 20 pts/7 0:00 /usr/bin/ksh
7
1 R vbe 22361 9224 5 187 24 949e4d80 80 - 17:06:23 pts/6 0:00 /bin/sh ./cpt_in_while
8
1 S vbe 9224 19185 3 158 24 65bb6c40 99 8aab0040 Jan 22 pts/6 0:00 /usr/bin/ksh
9
1 S vbe 26803 26727 0 154 24 6dd3e840 296 b52c00 Jan 29 pts/11 0:00 ssh -X ramix03
10
1 S vbe 28783 19185 0 154 24 8aaa1440 97 67d7eca8 Feb 4 pts/3 0:00 /usr/bin/ksh
11
2001 S vbe 19195 19187 0 154 18 810c8300 1624 b52c80 Jan 20 pts/2 58:16 gpm
12
1 S vbe 26727 19185 0 158 24 61ec2d40 91 95038040 Jan 29 pts/11 0:00 /usr/bin/ksh
13
1 S vbe 19187 19185 0 158 24 91045880 91 95449040 Jan 20 pts/2 0:00 /usr/bin/ksh
14
1 S vbe 22604 19185 0 158 24 62e05e00 91 6a224040 Feb 11 pts/10 0:00 /usr/bin/ksh
15
1 S vbe 24918 22604 0 154 24 6bdb96c0 57 6c7e14e8 Feb 11 pts/10 0:00 more start_objectspawner
16
1 S vbe 19288 19185 0 158 24 8a724800 95 6ca60040 Jan 25 pts/0 0:00 /usr/bin/ksh
17
1 R vbe 22374 22361 1 186 24 94ed3540 6 - 17:06:27 pts/6 0:00 cut -f3 -d
18
1 R vbe 22375 22374 1 186 24 62a8e080 96 - 17:06:27 pts/6 0:00 /bin/sh ./cpt_in_while
19
1 S vbe 19224 19185 0 154 24 6bd234c0 97 6a6704e8 Jan 20 pts/4 0:00 /usr/bin/ksh
Count : 19
That's all
ant:/home/vbe $