Sponsored Content
Homework and Emergencies Homework & Coursework Questions Incrementing Variable resets outside of while loop Post 302394493 by ratzlaff on Thursday 11th of February 2010 02:42:32 PM
Old 02-11-2010
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


2. Relevant commands, code, scripts, algorithms:


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



3. 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"

4. 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).

Last edited by pludi; 02-11-2010 at 04:03 PM.. Reason: code tags, please...
 

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
TPING(1)							   LAM COMMANDS 							  TPING(1)

NAME
tping - Send echo messages to LAM nodes. SYNOPSIS
tping [-hv] [-c count] [-d delay] [-l length] nodes OPTIONS
-h Print the command help menu. -v Turn OFF verbose mode. -c count Send count messages. -d delay Delay delay seconds between each message. -l length Each message is length bytes long. DESCRIPTION
The tping command sends messages to, and collects replies from, a list of nodes, via the LAM echo server. It is similar to the UNIX ping(8) command, and is used as a quick diagnosis of the LAM network. Unless options are specified, tping sends a 1 byte message an infinite number of times, displaying the roundtrip time of each message as it completes, with a delay of 1 second between roundtrips. After the loop is broken (with keyboard interrupt, eg: ^C), tping prints statis- tics about all roundtrip messages. EXAMPLES
tping h Echo messages to the local node. tping -v n7 -l 1000 -c 10 Echo 1000 byte messages to node 7. Stay silent while working. Stop after 10 roundtrips and report statistics. BUGS
There is no built-in timeout and tping will wait forever to receive an echo. If no echo is received, due to a dead link or node, tping hangs. Stop the process with a keyboard suspend signal (eg: ^Z) and terminate LAM with lamhalt(1) or lamwipe(1) (although the use of lamwipe(1) is deprecated). SEE ALSO
lamhalt(1), lamwipe(1) LAM 7.1.4 July, 2007 TPING(1)
All times are GMT -4. The time now is 08:40 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy