Sponsored Content
Homework and Emergencies Homework & Coursework Questions Incrementing Variable resets outside of while loop Post 302394500 by Corona688 on Thursday 11th of February 2010 03:13:16 PM
Old 02-11-2010
It's because of the pipe.

Code:
#!/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
Code:
      who | cut -f1 -d" " > temp
      cat temp
      rm temp

with
Code:
      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.

Last edited by Corona688; 02-11-2010 at 04:29 PM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

incrementing a for loop

I have, LIST="a b c d e" for word in $LIST do echo $word done would give me a b c d e With the first iteration of the for loop, I get "a" as the result. Is it possible that I get both "a" and "b" in only the first iteration. In the next iteration I get "c" and "d" and so on.... (2 Replies)
Discussion started by: run_time_error
2 Replies

2. Shell Programming and Scripting

New iteration of for-loop without incrementing?

Another question, is it possible to, in a for-loop incrementing until it reaches a certain number, to have it loop again without incrementing? Just have it drop what it is doing when it reaches this command and start again at the same number it was at? I know I could make a while loop and just... (0 Replies)
Discussion started by: jeriryan87
0 Replies

3. Shell Programming and Scripting

Need help with incrementing date in while loop.

I need to execute a KornShell (SunOS 5.9) script against a range of dates: endDate=20080804 extractDate=20080401 while ; do batch < scripts/myshellscript.sh $extractDate ## add 1 day to extractDate ## done My question is how do I increment the extractDate variable and still have it... (3 Replies)
Discussion started by: Robert W.Mills
3 Replies

4. Linux

Incrementing the date stored in the variable

Hi all, I have a variable with date as 20080831 . Now I want to increment it as 20080901 and so on.Is there any command for this. Please help me. thanks rameez (1 Reply)
Discussion started by: rameezrajas
1 Replies

5. Shell Programming and Scripting

Incrementing a variable is not happening

Hi All, Iam trying to increment a variable Following is the code #!/usr/bin/ksh i=1; i='expr $i+1'; echo $i; Output: expr $i+1 not able to understand why its happening in that way i was expecting result as 2... if the above method is worng .. can you help how i can get... (3 Replies)
Discussion started by: kiranlalka
3 Replies

6. Shell Programming and Scripting

Incrementing in while loop

echo "Enter Starting id:" echo "" read rvst_strt_idxx echo "" echo "Enter Closing id:" echo "" read rvst_clsn_idxx FIELD1=$rvst_strt_idxx FIELD2="USER" FIELD3="TEST" FIELD4="12345" FIELD5="00000" echo "" echo "INSERT INTO TABLE( FIELD1, FIELD2, FIELD3, FIELD4, ... (7 Replies)
Discussion started by: ultimatix
7 Replies

7. UNIX for Dummies Questions & Answers

Incrementing Variable Names

Hi, I am using BASH. I have encountered a situation where the following is necessary (but I am not sure how to do it): #Define multiple arrays, whose names only differ by a number: ARRAY_1=(1 2 3) ARRAY_2=(4 5 6) ARRAY_3=(7 8 9) #Define ARRAY_AMOUNT, the number of arrays. In this case... (1 Reply)
Discussion started by: msb65
1 Replies

8. UNIX for Dummies Questions & Answers

Incrementing variable in for

Hi, want to increment a variable in a for loop like this: for (( c=$total-1; c>=0; c-- )) do if ; then maximo=$valores fi done But it gives the error: No such file or directory How can i do this only incrementing the c variable? Thanks (8 Replies)
Discussion started by: limadario
8 Replies

9. Shell Programming and Scripting

incrementing the variable name along with the data?

Hello folks. I am trying to increment my variable names to match a counter that is to be used later on... Basically, i have a for loop that lists directories (for example TEST_OS DVP_OS PROD_OS ) but this loop is not static, it may contain 3 directory once and the next run 5 directories. I... (6 Replies)
Discussion started by: Stephan
6 Replies

10. Shell Programming and Scripting

Loop counter resets by itself

Hi! Can anyone explain this? The counter CDR_count should go on forever, but it suddenly resets at every step of the FOR loop (I know this because $_file has exactly 378 records). However, the counter reset is OUTSIDE the FOR loop. What's going on?? #!/bin/bash if || ; then echo ... (8 Replies)
Discussion started by: Flavius
8 Replies
GETBDDVARINDEX(3)						   BDD FUNCTIONS						 GETBDDVARINDEX(3)

NAME
getbddvarindex - converts a variable number in a bdd index. ORIGIN
This software belongs to the ALLIANCE CAD SYSTEM developed by the ASIM team at LIP6 laboratory of Universite Pierre et Marie CURIE, in Paris, France. Web : http://asim.lip6.fr/recherche/alliance/ E-mail : alliance-users@asim.lip6.fr SYNOPSYS
#include "bdd101.h" bddindex getbddvarindex( BddSystem, Variable ) bddsystem *BddSystem; bddvar Variable; PARAMETERS
BddSystem The bdd system. Variable The variable number to convert. DESCRIPTION
getbddvarindex converts the variable number Variable to a bdd index, in the bdd system BddSystem. If a null pointer is given, the default bdd system is used. The function uses simply the translation table BddSystem->VAR_TO_INDEX. RETURN VALUE
getbddvarindex returns the bdd index of Variable. ERRORS
"variable xxx out of range, error !" The Variable parameter must be less or equal to the BddSystem->NUMBER_VAR field. EXAMPLE
#include "bdd101.h" bddsystem *BddSystem; bddnode *BddNode; BddSystem = createbddsystem( 100, 1000, 100, 50000 ); BddNode = addbddvar( (bddsystem *)0, 0 ); /* displays 2 */ printf( "%d", getbddvarindex( (bddsystem *)0, 0 ) ); destroybddsystem( (bddsystem *)0 ); SEE ALSO
bdd(1) BUG REPORT
This tool is under development at the ASIM department of the LIP6 laboratory. We need your feedback to improve documentation and tools. ASIM
/LIP6 October 1, 1997 GETBDDVARINDEX(3)
All times are GMT -4. The time now is 01:06 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy