脚本将等待数据库中的条目直到一定的时间

时间:2015-08-24 13:15:50

标签: sql shell unix

我有表说x_table(只有一列 - x_column))在一段时间后使用新值更新(时间不固定)。

我想编写一个shell脚本,它将继续检查此表,直到某个值到达,然后说我想从DB中删除该值。

Like :
when x_column is not equal 10 , wait 
when x_column equal to 10 , delete from x_table where x_column=10.

另外我也想处理超时,说我的脚本等了10分钟,但是还没有到达(在这种情况下退出脚本)。

请帮帮我。提前致谢。

代码:

actual=10

db_value=`sqlplus -s $DB <<EOF
set heading off
set feedback off
SELECT x_column from x_table where x_column=$actual;
exit
EOF`;


while [ $db_value = "no rows selected" ]
do
    db_value=`sqlplus -s $DB <<EOF
    set heading off
    set feedback off
    SELECT x_column from x_table where x_column=$actual;
    exit
    EOF`;

   sleep 5;
   wait=0;
   wait=wait+5;

   if [[ $wait -eq 600 ]]; then
   exit 1;
   fi
done

sqlplus -S $DB << EOF
set echo off head off verify off feed off pages 0 
delete from x_table where x_column=$actual;
EOF


exit;

1 个答案:

答案 0 :(得分:1)

interval=600
loop_count=12
counter=0
while true
do
    $counter=`expr $counter + 1 `
    db_value=`sqlplus -s $DB <<EOF
    set heading off
    set feedback off
    SELECT x_column from x_table where x_column=$actual;
    exit
    EOF`;

   if [ $db_value -eq 10 ]; then
      sqlplus -S $DB << EOF
      delete from x_table where x_column=$actual;
      EOF
      exit 0
   else
      if [ $counter -eq $loop_count ]
      then 
         echo "No rows deleted after checking for `expr $interval * $loop_count                     ` seconds , exiting script"
         exit 1
      else
         sleep $interval
      fi
   fi
done