Linux 编写Shell脚本
我正在尝试编写一个shell脚本,它将基本上查询给定的条件。这是陷阱。我希望它在3分钟内重复查询。(可能运行查询并休眠2秒钟) 1分钟后,如果查询返回null,则for循环将随时中断。(主要目的是检测查询是否在3分钟内一致返回结果) 我如何在1分钟后将BREAK语句的检查合并到下面的代码中?(SPOOL是覆盖文件内容还是追加?) ((i=90;i>1;i--)的Linux 编写Shell脚本,linux,oracle,shell,conditional-statements,Linux,Oracle,Shell,Conditional Statements,我正在尝试编写一个shell脚本,它将基本上查询给定的条件。这是陷阱。我希望它在3分钟内重复查询。(可能运行查询并休眠2秒钟) 1分钟后,如果查询返回null,则for循环将随时中断。(主要目的是检测查询是否在3分钟内一致返回结果) 我如何在1分钟后将BREAK语句的检查合并到下面的代码中?(SPOOL是覆盖文件内容还是追加?) ((i=90;i>1;i--)的 做 sqlplus-s用户名/passwd@SERVICENAME最简单的方法是捕获sqlplus的输出,然后测试结果字符串是否为空。
做
sqlplus-s用户名/passwd@SERVICENAME最简单的方法是捕获sqlplus
的输出,然后测试结果字符串是否为空。为了可读性,我在函数中调用了sqlplus
。给定您正在使用的for
语句的形式,我还假设您正在使用bash
run_query () {
sqlplus -s username/passwd@SERVICENAME <<EOF
# [deleted]
EOF
}
# SECONDS is incremented each second, so can be used as
# a simple timer.
SECONDS=0
# For the first minute, just run the query
while (( SECONDS <= 60 )); do
output=$(run_query)
sleep 2
done
# After the first minute, continue running the query for the
# next two minutes, but quit if the query produces no output.
while (( SECONDS <= 180 )); do
output=$(run_query)
if [[ -z $output ]]; then
break
fi
sleep 2
done
为什么不把这些都放在一个存储包或过程中并调用它呢?有关一些想法,请参阅。注意dbname=$(…)
构造。祝你好运。@kevinsky,我不使用存储过程,因为我相信shell脚本,它将无法将结果输出到文件并将其作为通知发送出去。@Shedder,我熟悉语法,对它没有问题,但我只是不确定是否要为循环(或while-Loop)编写另一个如果在X次尝试后返回空结果,则检查并中断。谢谢Chepner!if条件中的-z表示值是否为零?长度为零,即如果$output==”
。
run_query () {
sqlplus -s username/passwd@SERVICENAME <<EOF
# [deleted]
EOF
}
# SECONDS is incremented each second, so can be used as
# a simple timer.
SECONDS=0
# For the first minute, just run the query
while (( SECONDS <= 60 )); do
output=$(run_query)
sleep 2
done
# After the first minute, continue running the query for the
# next two minutes, but quit if the query produces no output.
while (( SECONDS <= 180 )); do
output=$(run_query)
if [[ -z $output ]]; then
break
fi
sleep 2
done
while (( SECONDS <= 180 )); do
output=$(run_query)
# Don't break for any reason during the first 60 seconds
if ((SECONDS > 60)) && [[ -z $output ]]; then
break
fi
sleep 2
done
start=$(date +%s)
while now=$(date +%s); SECONDS=$(( now - start)); [ "$SECONDS" -le 180 ]; do
output=$(run_query)
if [ "$SECONDS" -gt 60 ] || [ -n "$output" ]; then
break
fi
sleep 2
done