While loop with limit counter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting While loop with limit counter
# 1  
Old 01-30-2017
While loop with limit counter

Code:
#!/usr/bin/ksh

c=0
while [[ ! -f /tmp/unex || $c -lt 10 ]]; do 
    echo /tmp/unex NOT found, iter : $c;
    ((c = $c + 1));
    sleep 2; 
done

so, the above counter doesn't work, already tried both -lt & -gt, and changed || to &&

so what am I missing?

Thanks in advance
# 2  
Old 01-30-2017
change || to &&
# 3  
Old 01-30-2017
Does the file /tmp/unex exist? If it does not exist, is there some reason to think that it might be created while your script is running? If it does exist, is there some reason to think that it might be removed while your script is running?

What output are you getting?

What output do you want to get?

With no indication of what you are trying to do and no indication of the state of the conditions being tested by your script, it is hard to guess at what you might think right or wrong?
# 4  
Old 01-30-2017
Quote:
Originally Posted by Don Cragun
Does the file /tmp/unex exist? If it does not exist, is there some reason to think that it might be created while your script is running? If it does exist, is there some reason to think that it might be removed while your script is running?

What output are you getting?

What output do you want to get?

With no indication of what you are trying to do and no indication of the state of the conditions being tested by your script, it is hard to guess at what you might think right or wrong?
Ah my bad, I am trying to detect the file to see if it existed, but cannot have it run indefinitely, so I've put the counter as a limit

using || or &&, the counter still went beyond 10, in which I expect after trying to detect the file 10 times, the script will quit
# 5  
Old 01-30-2017
When I try your script with || changed to &&:
Code:
#!/bin/ksh

c=0
while [[ ! -f /tmp/unex && $c -lt 10 ]]; do 
    echo /tmp/unex NOT found, iter : $c;
    ((c = $c + 1));
    sleep 2; 
done

(note that ksh is in /bin instead of /usr/bin on my system) and the file /tmp/unex does not exist; I get the output:
Code:
/tmp/unex NOT found, iter : 0
/tmp/unex NOT found, iter : 1
/tmp/unex NOT found, iter : 2
/tmp/unex NOT found, iter : 3
/tmp/unex NOT found, iter : 4
/tmp/unex NOT found, iter : 5
/tmp/unex NOT found, iter : 6
/tmp/unex NOT found, iter : 7
/tmp/unex NOT found, iter : 8
/tmp/unex NOT found, iter : 9

and it then quits. Isn't that what you want?
# 6  
Old 01-30-2017
Quote:
Originally Posted by Don Cragun
When I try your script with || changed to &&:
Code:
#!/bin/ksh

c=0
while [[ ! -f /tmp/unex && $c -lt 10 ]]; do 
    echo /tmp/unex NOT found, iter : $c;
    ((c = $c + 1));
    sleep 2; 
done

(note that ksh is in /bin instead of /usr/bin on my system) and the file /tmp/unex does not exist; I get the output:
Code:
/tmp/unex NOT found, iter : 0
/tmp/unex NOT found, iter : 1
/tmp/unex NOT found, iter : 2
/tmp/unex NOT found, iter : 3
/tmp/unex NOT found, iter : 4
/tmp/unex NOT found, iter : 5
/tmp/unex NOT found, iter : 6
/tmp/unex NOT found, iter : 7
/tmp/unex NOT found, iter : 8
/tmp/unex NOT found, iter : 9

and it then quits. Isn't that what you want?
oh yes, thank you so much
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Avoid multiple emails being sent - Counter, Loop?

Hello, I have currently coded a bash script below in which it does the following: # Archives compressed file from another location. Basically it moves *.gz files to another location. The script also sends an email whenever a new compressed file is placed. This is the issue that i... (5 Replies)
Discussion started by: Wizard_1979
5 Replies

3. Shell Programming and Scripting

Loop usage with counter

Hi friends, I just want to try some thing with shell using loop and counter I have 30 Directory, in each directory number of files say 5, 10, 20 etc... directory_1 directory_2 directory_n what I want to do is read files from directory_1 say 5 files if my counter is like this m=2000 ... (6 Replies)
Discussion started by: Akshay Hegde
6 Replies

4. Shell Programming and Scripting

How to implement the counter in loop?

Hi, I am working on a script where I need to add one functionality i.e. to could the number of tar files at particular location...but the script is working in below way. 1) create sandbox 2) Drop old member function 3) addmember function 4) Apply checkpoint lable 5) Resync operation(This... (1 Reply)
Discussion started by: anuragpgtgerman
1 Replies

5. Shell Programming and Scripting

How to use counter to run the script to limit number?

I want to run my shell script to the limit number.Suppose I know in advance that MAX=5 then I want that my script run 5 times only.Something like below$ vi testingMAX=5COMMAND="ssh -l stpuser VHLDVWSAD001 /projects/st/utils/deploy/deployall.sh >/dev/null 2>&1 &" ; sleep 20;count=0while... (2 Replies)
Discussion started by: anuragpgtgerman
2 Replies

6. Shell Programming and Scripting

File merging based on different counter loop

hello, File 1 main Group sub group MIT VAR_1D_DATA_TYPE 23-03-2012 MIT VAR_1D_DATA_TYPE 22-03-2012 MIT VAR_10D_DATA_TYPE 23-03-2012 MIT VAR_10D_DATA_TYPE 22-03-2012 MIT ... (0 Replies)
Discussion started by: manas_ranjan
0 Replies

7. Shell Programming and Scripting

loop with a counter on a constant in bash

Hello Everyone, I'm in need of assistance on creating a script with a counter on a certain string. Basically this script opens a log file and displays certain log data. There are two key words in the log. START and FINISH. In between the START and FINISH is a variable ACTNUMBER. It will... (1 Reply)
Discussion started by: rxc23816
1 Replies

8. Shell Programming and Scripting

S# in a for loop - concatenate $(loop counter)

Hi, hope I am posting in the right section. My problem is that I have 2 or more arguments passed and I want to check if the arguments passed exists or not. The first argument should not exist and the remaining others should exist. example: ./shells.sh argument1 argument2 argument3 ... (5 Replies)
Discussion started by: fight4love
5 Replies

9. Shell Programming and Scripting

adding counter to a variable while moving in a loop

The scenario is like this : I need to read records from a file one by one and increment counter1, if a certain field matches with a number say "40"..the script should increment the counter2 and also extract a corresponding field from the same line and adding them one by one and redirecting the the... (5 Replies)
Discussion started by: mady135
5 Replies

10. Shell Programming and Scripting

Loop counter variable display

Hello everyone, how can I send output to the screen from a running script or tcl, in such a way that if a loop is executing I will see the rolling counter on my screen as the records are processed in the loop. I do not want the screen to scroll, though. In other words can a var's value be painted... (2 Replies)
Discussion started by: lifespan
2 Replies
Login or Register to Ask a Question