bash - 循环 sql 以匹配 bash ksh (AIX) oracle 数据库中的数据
问题描述
我想通过 shell 检查 sqlplus 命令的两个输出是否匹配。因此,我循环直到捕获该输出的变量相等,然后结束循环,然后是下一组指令。
下面提到的循环都没有按预期运行。
我希望实时获取 和 的数据,testSeq
然后expectedSeq
进行比较以检查数据,如果它们相等,则退出并继续下一步。
testSeq=`sqlplus -S user/xxxxxxxxx@${primary} << EOF
set heading off feedback off pagesize 0 verify off echo off numwidth 15
select max(ARCHIVED_SEQ#) from v\\$ARCHIVE_DEST_STATUS;
EOF`
expectedSeq=`sqlplus -S user/xxxxxxxxx@${DR}<< EOF
set heading off feedback off pagesize 0 verify off echo off numwidth 15
select max(APPLIED_SEQ#) from V\\$ARCHIVE_DEST_STATUS;
EOF`
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
while (true)
do
while do;
testSeq=`sqlplus -S user/xxxxxxxxx@${primary} << EOF
set heading off feedback off pagesize 0 verify off echo off numwidth 15
select max(ARCHIVED_SEQ#) from v\\$ARCHIVE_DEST_STATUS;
EOF`
expectedSeq=`sqlplus -S user/xxxxxxxxx@${DR}<< EOF
set heading off feedback off pagesize 0 verify off echo off numwidth 15
select max(APPLIED_SEQ#) from V\\$ARCHIVE_DEST_STATUS;
EOF`
if [[ "$testSeq" != "$expectedSeq" ]]
then
echo "$DR sync is in Progress." | mailx -s "$DR Refresh update" xxxxx@mail.com
else
sleep 20
echo "$DR is in sync with ${PRIMARY} and ready to be switched to Snapshot DR" | mailx -s "$DR Refresh update" xxxxx@mail.com
done
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
while [[ "$testSeq" != "$expectedSeq" ]]; do
echo "$DR sync is in Progress." | mailx -s "$DR Refresh update" xxxxx@mail.com
sleep 20
done
echo "$DR is in sync with ${PRIMARY} and ready to be switched to Snapshot DR" | mailx -s "$DR Refresh update" xxxxx@mail.com
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
until [ "$expectedSeq" -eq "$testSeq" ]; do
echo "$DR sync is in Progress." | mailx -s "$DR Refresh update" xxxxx@mail.com
sleep 5
if [ $? -eq 0 ]; then
break
fi
done
echo "$DR is in sync with ${PRIMARY} and ready to be switched to Snapshot DR" | mailx -s "$DR Refresh update" xxxxx@mail.com
感谢所有的帮助。提前致谢
解决方案
这应该适用于 bash,我没有测试 ksh:
expectedSeq=`sqlplus -S user/xxxxxxxxx@${DR} <<EOF
set heading off feedback off pagesize 0 verify off echo off numwidth 15
select max(APPLIED_SEQ#) from V\\$ARCHIVE_DEST_STATUS;
EOF`
testSeq=
while [[ $testSeq != $expectedSeq ]]; do
if [[ -n testSeq ]]; then
echo "$DR sync is in Progress."
sleep 20
fi
testSeq=`sqlplus -S user/xxxxxxxxx@${primary} <<EOF
set heading off feedback off pagesize 0 verify off echo off numwidth 15
select max(ARCHIVED_SEQ#) from V\\$ARCHIVE_DEST_STATUS;
EOF`
# todo: handle failures
done
echo "$DR is in sync with ${primary} and ready to be switched to Snapshot DR"
可以在循环外定义预期输出,前提是表格在此过程中不发生变化。您应该为查询失败的情况做好准备,通过在输出中查找错误等。
推荐阅读
- java - 有没有办法记录在 hikari 中获得连接所需的时间?
- python - python django django.db.utils.OperationalError: table pybo_question has no column named content 错误?
- jsp - jsp:include 不能包含二进制文件
- regex - 消极展望的替代方案
- javascript - 试图在滚动视频上获得模糊效果
- python - 熊猫将迭代添加列添加到数据框
- soap - SSRS 报告 - XML 查询
- reactjs - 如何在打字稿中的变体“临时”的抽屉组件上设置 zIndex
- python - 如何将数据插入到 Azure SQL 数据库上的表中,并且通过 Airflow DAG 使用 Azure Key Vault 对少数列进行加密
- c - Cygwin 无法链接 libexif 程序