Reading values from sql query

I have sql query in shell script.

select  distinct account_no  from adj  order by account_no;

This query returns account number daily.Sometimes it may return 90 rows sometime it may return 1 row only and someday it may return 0 rows
I am storing the output of this query in [b]sql_output.txt.

I am passing the account number to another shell script

bip.sh

how can i do this in a loop

bip.sh 01 account_number_1;
bip.sh 02 account_number_2; ---this bip.sh is second instance of bip.sh 

i mean i want to take 2 different account numbers at a time and pass it to
bip.sh

If you have account numbers in sql_output.txt

 
exec 5< sql_output.txt
while read acc1 <&5 ; do
read acc2 <&5
bip.sh 01 acc1
bip.sh 02 acc2
done
1 Like

Hi what does exec 5 means.what does 5 signifies.

I dont think i can use your code in my script.Currently i am passing one account number at a time

for i in `cat sql_output.txt`
do
echo "bip $i is running"

sh bip.sh 01 0 $i > sample_output.txt
sleep 100
--other stuff here
done

exec 5< will create a file handler...

I wont get it why you cant use it? the whole reason you posted here is to get a solution because your current code doesnt work that way right?

I am unable to sense the requirement here :frowning:

Let me rephrase.
Currently,I am storing account numbers in my sql_output.txt.
I am passing one account number one at a time to bip.sh(it runs in the background and genrate a number which i need to fetch).

how can i run
sh bip.sh 01 $i ---i has my first account number
sh bip.sh 01 $ --- what variable should i use here so it gets the second account number

It could be simplified by using:-

while read acc
do
   bip.sh $acc
done < sql_output.txt

This will loop for every record in your file. Be careful if you need to exclude things such as the column headings and numbers of row returned or blank lines.

If you have more than one column in the file and (for instance) the account number is in column 2 and you don't care about columns 1, 2 or three and from 5 onward, you could:-

while read col1 col2 col3 acc rest
do
   bip.sh $acc
done < sql_output.txt

You could run bip.sh 01 $acc if the 01 is a required parameter or even bip.sh 01 $acc & to run it in the background, but be careful if your file may have more than just a few records. There are limits to how many processes you should have running at the same time.

I hope that this helps

Robin
Liverpool/Blackburn
UK