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