Conditions in 'for' Loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Conditions in 'for' Loop
# 1  
Old 04-19-2011
Conditions in 'for' Loop

Hey guys, I've been learning shell for a few weeks now & come across an issue in one of my for loops :

Quote:
file=/opt/test.txt
for (( i = 1 ; i <= 24; i++ ))
do
read -r line
echo $line

# Run application based commands #
done
However I wish to add a condition (maybe if loop is best ??) when, if the variable value $line is > 24 go back to the start of my 'for' loop thus skipping my application based commands and incrementing i by one.

Quote:
if [ "$line" > "24" ] ; then
I have tired using the above, but fail to get it working properly.

Thank You
# 2  
Old 04-19-2011
I am not quite sure if you want to test the value someone enters, ie. some number or, if you want 24 times some input? Or both?

Maybe something like this?
Code:
$> while read I; do if (( $I < 24 )); then echo ok; else echo err; fi; done
22
ok
25
err
28
err

---------- Post updated at 04:13 PM ---------- Previous update was at 03:57 PM ----------

After reading it again I assume I understood it now:

Code:
#!/bin/bash

COUNTER=0
MAX=5

while read INPUT; do
   if (( $INPUT > 24 )); then
      if (( $COUNTER < $MAX)); then
         COUNTER=$(($COUNTER + 1))
         echo "Increasing COUNTER to $COUNTER."
      else
         echo "Reached the maximum tries was $COUNTER, aborting."
         break
      fi
   else
      echo "Doing nothing"
   fi
done

exit 0

Output:
Code:
1
Doing nothing
2
Doing nothing
3
Doing nothing
24
Doing nothing
25
Increasing COUNTER to 1.
26
Increasing COUNTER to 2.
27
Increasing COUNTER to 3.
28
Increasing COUNTER to 4.
10
Doing nothing
30
Increasing COUNTER to 5.
12
Doing nothing
31
Reached the maximum tries was 5, aborting.


Last edited by zaxxon; 04-19-2011 at 11:39 AM.. Reason: indention cosmetics
This User Gave Thanks to zaxxon For This Post:
# 3  
Old 04-19-2011
If you are validating input to make sure you get an integer below 25 (i.e. <=24) then you could consider this in korn shell:-
Code:
#!/bin/ksh
typeset -i i=100          # Set as integer

until [ $i -ge 0 -a $i -le 24 ]
do
  read input?"Please enter a value zero to 24:- "
  i=$input
  if [ "$i" != "$input" ]
  then
     i=100
  fi
done

# Whatever comes next


Does that help? The use of variable input is just to have something to compare to to check that we have an integer input. If it's not, then the assignment to i will fail, leaving i as zero and as that is a permitted value in the loop, it would normally be accepted. I code around that by setting it 100 if the input and the integer being really tested don't match. The if statement has to use string function tests else you get syntax errors.


I hope that this is useful

Robin
Liverpool/Blackburn
# 4  
Old 04-20-2011
This is looks like it will do the trick.... however I am getting these errors :

Quote:
line 44: syntax error near unexpected token `('
line 44: ` COUNTER=(($COUNTER + 1))'
When I use the below command the scrip appears to hang Smilie
Quote:
COUNTER=$(($COUNTER + 1))
Thank You

EDIT:
Zaxxon: Found out the issue is to do with reading my 'INPUT' file....It appears it is not reading the the value in my text file. Before I was using 'read -r' to read each indivdual line for each time the the statement loops around. However I am having trouble implementing this into this loop.

Last edited by phil0316; 04-21-2011 at 07:39 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to match the below conditions?

Hello All, I have 2 files with around 2k records.. I am lost how to build a script to create another file with the below:confused::eek: Match Condition1: File1's 12th Columns data should exact match with File2's 19th Column's data Match Condition2: File1's 28th Column's data format is... (12 Replies)
Discussion started by: ailnilanjan
12 Replies

2. Shell Programming and Scripting

Conditions in if

I'm using the below one.. #!/bin/ksh File=$3 if ; then echo "Script" elif ] ;then echo "Passed k or f option" else "Please check the Input passed" fi Command line argument is "k" or -f and file is exist then... (3 Replies)
Discussion started by: Roozo
3 Replies

3. Shell Programming and Scripting

While loop reading file with multiple conditions

Hi Am trying to print the PIDs of process in a file and trying to grep any PID from that file I set the if condition as $value != "PID" and $value != "-" Assign that number to a variable Am confused since am using while loop to read the line from file and again if condition to check those... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

4. Shell Programming and Scripting

Errors in if conditions with to many OR conditions

Hi ALL I have a script where in i need to check for several values in if conditons but when i execute the script it throws error such as "TOO MANY ARGUMENTS" if then msg="BM VAR Issue :: bmaRequestVAR=$bmaRequestVAR , nltBMVAR=$nltBMVAR , bmaResponseVAR=$bmaResponseVAR ,... (10 Replies)
Discussion started by: nikhil jain
10 Replies

5. Shell Programming and Scripting

If conditions need

Dear Expert, Below code is for to take the backup of database by daily time stamp. I need vital help to make my script automatic sending me email if it sucess or fail. echo on @REM Seamonkey's quick date batch (MMDDYYYY format) @REM Setups %date variable @REM First parses month, day, and... (6 Replies)
Discussion started by: Alone
6 Replies

6. Shell Programming and Scripting

IF OR with two conditions

I have this IF working fine, testing if a char is a digit: if ; then _VALUE=$_VALUE$_CHAR else _ISDIGIT="false" fi Then I add a second condition to test if the char is either a digit or a * if ]; then _VALUE=$_VALUE$_CHAR ... (11 Replies)
Discussion started by: Flavius
11 Replies

7. Shell Programming and Scripting

While with three conditions

Currently this is what I am trying while || && ]; do I want to continue if the first condition or both the second and third are true but I am getting a too many arguments error. Can someone help me out? (5 Replies)
Discussion started by: whdr02
5 Replies

8. Shell Programming and Scripting

conditions

./script 89 The script will extract the last digit of the input parameter. example, that is 4. This will be compared to the last digit of the current day of the month ( like day 14; that is 4). A message will displayed on the screen indicating if the digits are the same or not. (1 Reply)
Discussion started by: singh is king
1 Replies

9. UNIX for Dummies Questions & Answers

Mulitiple conditions in While Do loop.

Hi guyz, I have a WHILE loop which shud DO certain jobs when it satisfies the condition and it does...but I also need to assign a different job if it doesn't satisfy the condition..how can I do this.Below is my script. if ; then echo "if statement" while # (This... (1 Reply)
Discussion started by: sud
1 Replies

10. UNIX for Dummies Questions & Answers

2 or more if conditions

Hello, I have a file as follows: col no:1 2 3 4 5 6 7 8 9 10 11 a 4 226 226 ch:95024048-95027592, 1y224 of 3545 223 224 ident b 53 235 235 ch:148398-148401255, 1y184 of 3187 180 186 ident awk... (3 Replies)
Discussion started by: dr_sabz
3 Replies
Login or Register to Ask a Question