#!/bin/bash
# 语法
# if 表达式
# then
# 语句块
# elif 表达式
# 语句块
# else
# 语句块
# fi
# -gt 大于 -lt 小于 -le 小于等于 -ge 大于等于 -eq 等于 -ne 不等于
echo "请输入2个整数,比较大小"
read -p "请输入第一个数:" a
read -p "请输入第二个数:" b
if [ $a -gt $b ]
then
echo "$a > $b"
elif [ $a -eq $b ]
then
echo "$a = $b"
else
echo "$a < $b"
fi
#!/bin/bash
echo "脚本名字: $0"
echo "脚本的第一个参数:$1"
echo "脚本的第二个参数:$2"
echo "脚本的第三个参数:$3"
echo "脚本的第四个参数:$4"
echo "脚本所有参数: $@"
echo "脚本参数的个数:$#"
#!/bin/bash
if [ "x$1" == "x" ]
then
echo "Usage: ....."
exit
fi
case $1 in
1)
echo "1"
;;
2)
echo "2"
;;
3)
echo "3"
;;
*)
echo "...."
;;
esac
#!/bin/bash
# for i in 数组或者是列表
# do
# 语句块
# done
for j in 1 2 3 4 5 6 7 8 9 10
do
for i in 1 2 3
do
echo $i
done
done
#!/bin/bash
# 1x1=1
# 1x2=2 2x2=4
# ...........
for i in $(seq 1 9)
do
for j in $(seq 1 $i)
do
echo -n "${j}x${i}=$(expr $i \* $j) "
done
echo
done
#!/bin/bash
i=0
while [ $i -le 10 ]
do
echo $i
let i++
done
#!/bin/bash
a=(aa bb cc dd ee)
len=$(expr ${#a[@]} - 1)
echo ${a[0]}
echo ${a[1]}
echo ${a[2]}
echo ${a[3]}
echo ${a[4]}
echo
#i=1
for i in $(seq 0 $len)
do
echo "a[$i]=${a[$i]}"
done
[root@localhost ~]# cat sort.sh
#!/bin/bash
arr=(5 2 7 9 8 3 1 6)
echo "排序前的值:${arr[@]}"
len=$(expr ${#arr[@]} - 1)
while [ $len -gt 0 ]
do
i=0
while [ $i -lt $len ]
do
c=$(expr $i + 1)
num1=${arr[$i]}
num2=${arr[$c]}
if [ $num1 -gt $num2 ]; then
temp=$num1
arr[$i]=$num2
arr[$c]=$temp
fi
let i++
done
let len--
done
echo "排序后的值:${arr[@]}"
#!/bin/bash
for i in 1 2 3
do
if [ $i -eq 2 ]; then
echo "跳过当前循环"
continue
fi
echo "继续循环"
done
echo $i
#!/bin/bash
# Oracle警告日志文件监控脚本
# 2015/4/14 King.
# 发送邮件
sendMail()
{
echo "$1"
mailTo=$(echo $mailTo | sed 's/,/ /g')
echo "$1" | /usr/bin/mutt -s "$(date +"%Y-%m-%d %H:%M:%S") 警告日志错误" -b ${mailTo}
}
# 判断错误信息中是否有未扫描的行
checkHis()
{
tag=0
hisLine=$(cat $errLineNumFile)
for i in $hisLine
do
if [ "$1" == "$i" ]; then
tag=1
fi
done
return $tag
}
#
scriptDir=`pwd $0`
scriptName=`basename $0`
logDir=$scriptDir/logs
logfile=$logDir/alert_error.log
errLineNumFile=$logDir/.alert_errLineNum
#设置警告日日志文件路径
alertFilePath="/u01/app/oracle/diag/rdbms/orcl/orcl/trace/alert_orcl.log"
# 设置邮件接收者,多个用逗号分隔
mailTo="
[email protected]"
# 设置邮件抄送者,多个用逗号分隔
mailCc=""
[ ! -f $alertFilePath ] && echo "[Error]: $alertFilePath no such file or directory." && exit 1
[ ! -d $logDir ] && mkdir -p $logDir
touch $errLineNumFile
echo "正在监控 $alertFilePath... "
while true
do
arrayNum=()
isError=false
# 取出警告日志中 ”ORA?“关键字所在的行
errNum=$(cat $alertFilePath | grep -n -i "ORA-")
n=0
if [ "x$errNum" != "x" ]; then
# 取出错误行号
errLineNum=$(echo "$errNum" | awk -F: '{print $1}')
for num in $errLineNum
do
#判断该行错误信息是否已扫描
if [ "x$errLineNum" != "x" ]; then
checkHis "$num"
if [ $? -eq 0 ]; then
# 如果该行错误未扫描,记录该行信息
isError=true
echo $num >> $errLineNumFile
arrayNum[$n]=$num
let n++
fi
else
#如果没有错误休眠10s后重新扫描
sleep 10s
break
fi
done
fi
# 如果发现未扫描的错误信息则根据行号取出该行信息记录日志,并且发送邮件
if [ "$isError" == "true" ]
then
echo "-------------------------------- $(date +"%Y-%m-%d %H:%M:%S") ---------------------------------------" >> $logfile
i=0
errMsg=$(
while [ $i -lt ${#arrayNum[@]} ]; do
echo "$errNum" | grep "^${arrayNum[$i]}:"
let i++
done)
echo "$errMsg" >> $logfile
sendMail "$errMsg"
fi
#每10s,扫描一次警告日志文件
sleep 10s
done