how to create a loop in an if statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to create a loop in an if statement
# 1  
Old 01-20-2012
Java how to create a loop in an if statement

Hey guys,
Code:
a=`cat abc | wc -l`
b=`cat def | wc -l`
 
if $a== $b
then
echo "a"
else 
echo "b"
fi

I want the if condition to retry itself , untill a==b.

I can't use goto statemt.
Please help.

Thanx in advance.

Moderator's Comments:
Mod Comment Please use next time code tags for your code and data

Last edited by vbe; 01-20-2012 at 10:34 AM..
# 2  
Old 01-20-2012
1. use code tags
2. wrap the conditional with while loop but this would never end ! If it matches, break... But it would never end if you dont try to change the content in else statement.
Code:
while true
do
    if [ $a == $b ]
    then
        echo "a"
        break
    else
        echo "b"
    fi
done

# 3  
Old 01-20-2012
Hey,

I need to retry the if condition.

The count in a and b may vary with time.

So if he count of both the files is different, then my else condition will work.

I need the if condition to retry itself, until there is a change in count of a and b so that i reach the else condition.
# 4  
Old 01-20-2012
Code:
while true
do
    a=`wc -l <abc`
    b=`wc -l <def`

    if [ $a == $b ]
    then
        echo "a"
        break
    else
        echo "b"
        break
    fi
    sleep 5  # Sleep for 5 seconds before retry
done

Jean-Pierre.
# 5  
Old 01-20-2012
Here's my 2 cents using ksh93 on Solaris:
Code:
#!/usr/dt/bin/dtksh

#Declare constants
typeset -r FILE1=abc
typeset -r FILE2=def

# Declare integer variables.
typeset -i a
typeset -i b

 # Loop forever (could be risky, but that was the requirement).
   while :
   do
    # Make sure both fles exist and have size.
    if [[ -s $FILE1 ]] && [[ -s $FILE2 ]]; then
      # Count lines in each file.
      a=$(wc -l < $FILE1)
      b=$(wc -l < $FILE2)

      if (( $a == $b )); then
        print "a"
        break
      else
        print "b"
        sleep 2
      fi
    else
      print "File $FILE1 or $FILE2 does not exist"
      exit 1
    fi
  done

exit 0


Last edited by gary_w; 01-20-2012 at 11:14 AM..
# 6  
Old 01-20-2012
Its not workin aegsis.

I need retry the if condition and once the if condition is not satisfied i need to go to the else condtion and end the script.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to create variables to pass into a bash loop to create a download link

I have created one file that contains all the necessary info in it to create a download link. In each of the lines /results/analysis/output/Home/Auto_user_S5-00580-6-Medexome_67_032/plugin_out/FileExporter_out.67... (8 Replies)
Discussion started by: cmccabe
8 Replies

2. Shell Programming and Scripting

How to create a for loop statement for removing files listed in Oracle table?

Hello Frens, I am a newbie to shell scripting. I need a help on creating a for loop script (shell script) for removing files. I have a table called a_table with column name fil_name which contains all the files that need to be removed. Thank you in advance (6 Replies)
Discussion started by: manisha_singh
6 Replies

3. Shell Programming and Scripting

How to create SQL statement out of data using shell script?

Table TAB1 contains following example data (its a tree sitting in table data format & its driven based CHILD & PARENT column pick the RULE condition to generate the below SQL: CHILD PARENT SS MID MNM VNM RULE FLG 1 ? S1 ? ? V1 rule004 I 2 1 S1 ? ? V1 0 Z 3 1 S1 ? ? V1 1 Z ... (6 Replies)
Discussion started by: gksenthilkumar
6 Replies

4. Shell Programming and Scripting

Create table within awk-if statement

Hi I am trying to create a table within an awk if statement awk -F, '{ if ($8 ~ /Match/) BEGIN{print "<table>"} {print "<tr>";for(i=1;i<=NF;i++)print "<td>" $i"</td>";print "</tr>"} END{print "</table>"}' SN1.csv | mailx -s "Your details" abc@123.com But this doesnt work.. Please suggest (8 Replies)
Discussion started by: sidnow
8 Replies

5. Shell Programming and Scripting

While loop within if statement

Hi, I'm a rookie who is trying to learn this stuff. What I need help with is putting together a non complicated "while" loop within the below "if" statement. I also need the while loop to keep looping until the user types a key to end the loop. Please reveal the proper insertion points. Thank... (4 Replies)
Discussion started by: jefferj54
4 Replies

6. Shell Programming and Scripting

awk if statement in a for loop?

I need to match multiple values in a single column in a file: example: Source file: abd,123,one def,232,two ghi,987,six Target file: 12345,abcde,123 09876,zxvyr,566 56789,lmnop,232 Variable: var1=`grep 2 sourcefile | awk '{print$1}' essentially, echo "$var1" would read:... (2 Replies)
Discussion started by: kopfgeldjagar
2 Replies

7. Shell Programming and Scripting

Help with IF statement with loop Shell Script

Hello I am very new to shell and I bought some books and trying to learn it. I started trying to write a script that will take a number and count it down to 1 with commas in between. This number can only be one argument. If lower than one or higher than one argument it sends an error message. ... (4 Replies)
Discussion started by: zero3ree
4 Replies

8. Shell Programming and Scripting

Variable problem in for loop with if statement

Hi, Again a little problem. Do not understand good why an empty string is not detected. Here is the program: #!/bin/ksh APR=`date | grep Apr | awk '{print $2$3}'` MAY=`date | grep May | awk '{print $2$3}'` JUN=`date | grep Jun | awk '{print $2$3}'` echo "Variable Apr has value:... (6 Replies)
Discussion started by: ejdv
6 Replies

9. UNIX for Dummies Questions & Answers

if statement in a while loop

#!/usr/bin/ksh echo Please enter while read n do echo $n >> datafile done question: How can I enject an if statement that if the users enter 0 (zero) the program will exit? this is what I have but not working #!/usr/bin/ksh echo Please enter number while read n do if $n=0 then... (2 Replies)
Discussion started by: bobo
2 Replies

10. UNIX for Dummies Questions & Answers

if statement in for loop of a string

I am attempting to pass a string into awk and loop through it, and then for every occurrance of a certain character perform an action. In this case, for example, echo 1 for each time character r is found in the string. Except I can't get it to work. Could someone please tell me why? echo... (7 Replies)
Discussion started by: Sniper Pixie
7 Replies
Login or Register to Ask a Question