How to use LOOP in Unix Script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to use LOOP in Unix Script?
# 1  
Old 09-20-2010
How to use LOOP in Unix Script?

Hi Unix Gurus,

I need your help in below mentioned problem:

$input_file -----> This file has number of SELECT COUNT(*) statements. Ther could be 1 statement or more than 1, it is not fixed.

[B][U]I need to implement the following logic.....

for i in `cat $input_file`
do
calculate count of SELECT COUNT(*) statement
(if possible, I need to store the count of each statement in a variable)
if count >= 200000
then
exit processing
fi
done



Thanks in advance !!!

Last edited by ustechie; 09-20-2010 at 02:19 PM..
# 2  
Old 09-20-2010
Are all your select count(*) statements single line statements?
&
Which database you are using?
# 3  
Old 09-20-2010
I am using db2 database.....
following is the example of SQLs present in the input file:


select count(*) from F0547DBA.TEST_LOAD where TRXN_CODE='1';
select count(*) from F0547DBA.TEST_LOAD where TRXN_CODE='2';
select count(*) from F0547DBA.TEST_LOAD where TRXN_CODE='3';
select count(*) from F0547DBA.TEST_LOAD where TRXN_CODE='4';
select count(*) from F0547DBA.TEST_LOAD where TRXN_CODE='5';
select count(*) from F0547DBA.TEST_LOAD where TRXN_CODE='6';
select count(*) from F0547DBA.TEST_LOAD where TRXN_CODE='7';
select count(*) from F0547DBA.TEST_LOAD where TRXN_CODE='8';
select count(*) from F0547DBA.TEST_LOAD where TRXN_CODE='9';
# 4  
Old 09-20-2010
Not sure how to connect with db2. I will give you a sample with sqlplus (connects to oracle). Please edit the corresponding code as per db2:

Code:
for i in `cat $input_file`
do
count=$(sqlplus id/password@connect_string <<EOSQL
$i
exit
EOSQL
)
[ $count -gt 200000 ] && exit
done

But this is will slow down the processing. because you have to connect and disconnect with the database in every iteration. Just check if this works in your case.
# 5  
Old 09-20-2010
thanks for helping me.....
i'll try this but I need to know the command used to execute in DB@ in place of sqlplus...
# 6  
Old 09-20-2010
Any ideas of a client tool you are using in the box to connect to the database. If, then please give the exact command with arguments. I can try based on that.
# 7  
Old 09-20-2010
i'm using db connect to <databse-name> user <user_name> using <password> command to connect to database....
i need not to connect to databse inside the loop as I am already establishing a connection in my script

all I need is the command to execute the SQL
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

UNIX shell script for loop issue

Guys, Please help on the below. sample.prm /u/test/: mail=123@gmail.com purgedays=30 zipdays=7 purgefile=log.gz zipfile=log /u/test/: mail=123@gmail.com purgedays=30 purgefile=txt.gz zipfile=txt zipdays=7 (1 Reply)
Discussion started by: AraR87
1 Replies

2. Shell Programming and Scripting

UNIX for loop

Guys, Please help me on the below.. sample.prm /u/test/: mail=123@gmail.com purgedays=30 zipdays=7 purgefile=log.gz zipfile=log /u/test/: mail=123@gmail.com purgedays=30 purgefile=txt.gz zipfile=txt zipdays=7 (2 Replies)
Discussion started by: AraR87
2 Replies

3. Shell Programming and Scripting

UNIX shell script - cut specified line and perform loop

Guys, I have a requirement as below. consider,if i use df command, its getting the below output. file system kbytes used avail %used Mounted on /dev/sample/ 45765 40000 5765 50% / /dev/filesys/ 30000 20000 1000 80% /u .... .... Now i wanted to cut the /u... (11 Replies)
Discussion started by: AraR87
11 Replies

4. Shell Programming and Scripting

foreach loop in unix script

Hi all, I have a script which searches for all sql files in the current directory and replaces all sql files with an underscore with a dash. The next part I need to do is record the number of changes made (underscore to dash) and display this value (e.g.2). This is what I have so far; find /... (17 Replies)
Discussion started by: shawi
17 Replies

5. UNIX for Dummies Questions & Answers

Help with unix for loop

Hi, I have a command that I want to translate to Unix. I mostly work with Windows and because of that I am stuck on a part. What I cannot find on the internet is skipping the first lines in a for loop and using a certain word/token. (I know how to do a normal loop with output) I need to skip... (1 Reply)
Discussion started by: flappy
1 Replies

6. Shell Programming and Scripting

How to loop use while loop in csh script?

Hi all, i got 2 text file. file.txt value.txt i want use C shell script to write out while both of the file got different limit....how i going to write it in 1 while loop? (4 Replies)
Discussion started by: proghack
4 Replies

7. Shell Programming and Scripting

for loop in Unix

This is the content of a file work.log 1 TGBUS I-US 0;15;83 i did this for i in `cat work.log` do echo $i done I wanted the out put to be 1 TGBUS I-US 0;15;83 But the output appears as 1 TGBUS I-US 0;15;83 For Loop treats space as a delimiter. Can i overrride this space as... (10 Replies)
Discussion started by: kinny
10 Replies

8. Shell Programming and Scripting

Unix Shell Script to loop over Directory and send Filesname as parameters

Hi there I'm new to UNIX scripting; I’m stuck with the following I have an Oracle SQL script that takes three parameters 1- File Name 2- File Path 3- File creation date Under UNIX I have a folder where files will be placed frequently and I need to upload those files to Oracle, what I need... (3 Replies)
Discussion started by: windjashi
3 Replies

9. Shell Programming and Scripting

for loop in unix

hi, can any one suggest why the program 2 is not working. only difference is for ab in *.txt for ab in a_DATA.txt b_DATA.txt c_DATA.txt ------------------------------------ Program :1 (working fine) #!/bin/ksh for ab in a_DATA.txt b_DATA.txt c_DATA.txt suf="_" echo "old->... (1 Reply)
Discussion started by: deep_kol
1 Replies

10. Shell Programming and Scripting

Please help with UNIX while loop...

Hi all. I am trying to execute a while loop that reads a log file containing a file listing, and it compares file sizes, etc. But I am getting an error that makes it seem like it is not reading the log file line by line. When I do a 'more' on the log file, it doesn't appear to be wrapped... ... (1 Reply)
Discussion started by: gerard1
1 Replies
Login or Register to Ask a Question