我正在尝试设置一个bash脚本,在该脚本中与Oracle实例的连接以检查计划的各种dba作业,并且如果某个特定的作业未运行,则需要显示错误消息。
这里是我编写的脚本,它部分起作用,该脚本仅在到达IF语句之前起作用
#!/usr/bin/env bash
#write output of the sql query to an output file
output_file="/tmp/datatool_output.csv"
> $output_file
echo "
SET FEED OFF
SET PAGESIZE 0
SET COLSEP ,
SET linesize 100
SET TRIMSPOOL ON
SET TRIMOUT ON
SELECT JOB_NAME, STATE FROM DBA_SCHEDULER_JOBS where job_name ='run_LoadDatatoolStore';
exit" | sqlplus -s sqlplus -s "username/password@dbip:dbport/SID" >> $output_file
a_var=($(awk -F',' '{print $2}' $output_file | sed 's/\s//g' | xargs))
for item in ${a_var[@]}
do
val=$(echo $item | awk -F',' '{print $1}')
if [[ $val -eq "SCHEDULED" ]] || [[ $val -eq "RUNNING" ]] ; then
echo "OK: The job LoadDatatool is either Scheduled or in the Running state"
exit 0 # this is used for interpretation by nagios to display no alert alert
else [[ $val -eq "BLOCKED" ]]
echo "CRITICAL: The Db scheduler job LoadDatatool is in a Blocked state"
exit 2
fi
我知道
test.sh: line 24: syntax error: unexpected end of file
当我运行脚本时,感谢您帮助解决此问题。
谢谢,卡尔提克
您应该以这种方式重写if
:
if [[ $val -eq "SCHEDULED" ]] || [[ $val -eq "RUNNING" ]] ; then
成为
if [[ $val = "SCHEDULED" ]] || [[ $val = "RUNNING" ]] ; then
或
if [[ $val = "SCHEDULED" || $val = "RUNNING" ]] ; then
[-eq
用于比较数字,=
用于比较字符串
P.S。另外,可以使用sqlplus
的SPOOL
功能代替sqlplus
的重定向输出。而不是丑陋的echo
,您可以在文件中写入sql
并从命令行执行它,例如:
sqlplus -s "username/password@dbip:dbport/SID" @sql_script.sql
在脚本末尾添加完成