Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
This is my first time ever writing a unix shell script, so I have no idea. Here is what we have going on. We have a view in oracle that I am selecting the count from. I want to pass the count into a variable and pass it into the unix shell script. If the count is not 0 then I want the shell script to sleep for 10 minutes and then query the view again. If somebody could please provide me with some code or any kind of help I would appreciate it.
Thanks!!!!

Even if you ultimately need to execute a plsql block after determining that the view has rows, your 10-minute monitoring of the view could use sqlplus instead of plsql.
Will that approach be OK?

Yeah that is my mistake. I meant to say sqlplus. You are correct. I just want to create a unix shell script the queries a view and if the view has rows then wait 10 minutes before I query it again.
Thanks

The script below will check the view every 600 seconds, and when there is a row
count, it will <do whatever> such as run a plsql script, then break out of the while-loop and terminate.#!/bin/sh
count=0
while true
do
echo "\
set pagesize 0
select count(*) from my_view;" |
sqlplus -s scott/tiger | read count
if [ $count -gt 0 ] ; then
echo "Count detected: $count"
# <do whatever>
break # (out of the while-loop)
else
sleep 600
fi
done
exit 0Alternatively, if you want to keep monitoring every 10 minutes, then replace the above if-else with this:
if [ $count -gt 0 ] ; then
echo "Count detected: $count"
# <do whatever>
fi
sleep 600

Here is the code I have
#!/bin/sh
count=0
while true
do
echo "\
set pagesize 0
select count(*) from bus_adm.dw_unfinished_reports;" |
sqlplus -s scott/tiger | read count
if [$count -gt 0]; then
echo "count is zero"
else
echo "count is not zero"
fi
done
exit 0The error I'm getting is unfinished_reports.ksh[9]: [ERROR:: not found.
Also in the loop, what is the condition that is being evaluated. While what is true?
Thanks

Also, if I have it echo "$count"
It echos ERROR. Appears that something is not working correctly with the variable.

In the statement:
if [$count -gt 0]; then
you need a space after the left bracket and a space before the right bracket.
Also, the while-loop does not have a sleep in it, so if it were working, it would be repeatedly running sqlplus over and over with no pause.
"while true" starts a while loop (delimited with do/done) that would run forever unless you break out of it because the expression "true" always evaluates as true.
As opposed to some other condition which might evaluate as either true or false such as:
while [ $count -eq 0 ]

Jim, thanks for all of your help.
One more question. Is it possible to select multiple values from a query into variables? Select col1, col2 from table where date = sysdate....as an example. Could we then put col1 and col2 into variables?
Thanks again.

Sure. Same solution, except change two lines to get the values into $a and $b:
select sum(a), sum(b) from my_reports;" |
sqlplus -s scott/tiger | read a b

Well it kind of works. I think it might be the data I'm pulling. Here is my code.
echo "\
set pagesize 0
select to_char(sysdate,'yyyy-mm-dd hh24:mi'), to_char(trunc(sysdate) + .375, 'yyyy-mm-dd hh24:mi') from dual;" |
sqlplus -s $DB_LOGIN | read now nineamAnd it splits it into 2 variables, but the time from variable now ends up in nineam.
Example:For sysdate it pulls 9/30/2004 15:31
For sysdate + .375 it pulls 9/30/2004 9:00The variables end up like this
now 9/30/2004
nineam 15:31 9/30/2004 9:00Is it bringing the time from now down to nineam because there is a space?

The other condition I'm trying to create is time. If there are 0 records in the table or it is after 9:00 then exit 0. Otherwise keep looping.
That is why I am selecting the times from dual

Just return the row count from sqlplus. Then the while-loop can break out of the loop when the count is zero or when it determines that it is after 09:00 with:
if [ $(date +%H%M) -gt 900 ] ; then
break
fiOr use -ge for greater-than-or-equal-to.

And I intended to answer your question ...
Yes, you were outputting 4 space-delimited words from sqlplus. The read command will read one word into each variable, but if it runs short of receiving variables, it throws all of the remaining words into the last variable.
When I wnat to receive two words only, I use:
read word1 word2 garbage
That way, any words beyond two will be thrown into garbage instead of messing up my word2. And as an edit, I could check if I received any $garbage.

If the time that you wanted to check from the database happens to be in a different time zone, you will of course need to make an adjustment for that if you use the date command instead.

![]() |
how do you get pthread to...
|
ask for users age as inpu...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |