shell脚本中使⽤if和bc⽐较变量值和指定值
##【预备知识
假如我有个⽇志⽂件,每天会往⾥追加。⽇志摘要如下——
2018-06-12 09:08:56,105 - hdfs.client - INFO - Renaming ‘/apps/wps_prt_feature/business_info/2018-06-
11/p-1528765731’ to ‘/apps/wps_prt_feature/business_info/2018-06-11/android_app’.
… …
2018-06-12 09:09:29,392 - hdfs.client - INFO - Renaming ‘/apps/wps_prt_feature/business_info/2018-06-
11/p-1528765764’ to ‘/apps/wps_prt_feature/business_info/2018-06-11/ios_app’.
我想在⾥⾯到带有当天⽇期和Renaming字样的内容,以便确认我跑的作业成功了,
那么我就想⽤shell脚本从⽇志⽂件中过滤出来。如何实现呢?下⾯是思考的过程——
[tony@test-host-10 business_info]$ today=`date +%F` # 获取当天⽇期
[tony@test-host-10 business_info]$ echo $today
2018-05-24
从包含当天⽇期的⽇志记录中进⼀步筛选出含有Renaming的⽇志
[tony@test-host-10 business_info]$ grep $today log_fifth | grep Renaming | wc -l
2
[tony@test-host-10 business_info]$ renamed_hdfsFile_result=`grep $today log_fifth | grep Renaming | wc -l`佩恩天道
[tony@test-host-10 business_info]$ echo $renamed_hdfsFile_result
2
经过bc计算,如果true(有2⾏含有Renaming的记录)则输出1,否则输出0
[tony@test-host-10 business_info]$ echo "$renamed_hdfsFile_result == 2" | bc
1
在linux shell中测试⼀下脚本
[tony@test-host-10 business_info]$ if [ `echo "$renamed_hdfsFile_result == 2" | bc` -eq 1 ]; then echo "OK~"; else echo "BAD"; fi
OK~
测试成功后,就可以把我们的业务逻辑写⼊到脚本⾥了
[tony@test-host-10 business_info]$ cat check_hdfs_dir.sh
#!/bin/bash
today=`date +%F`
renamed_hdfsFile_result=`grep $today log_fifth |grep Renaming |wc -l`
if[`echo"$renamed_hdfsFile_result == 2"|bc` -eq 1 ]
国庆节黑板报资料then
echo"Successfully renamed android_app and ios_app on our HDFS ^_^"
else
echo"Not good!"
fi
##【需求场景】
我想要在整点时间运⾏某脚本,但是不想使⽤crontab。⽐如我想在上午9点开始运⾏脚本,需要从date时间中获取⼩时数,再和9⽐较,如果⼤于等于9则开始执⾏脚本。这⾥⽤到了bc来⽐较数值⼤⼩。
如果没有安c,需要以root⽤户使⽤yum install -y bc即可安装,安装后就能直接使⽤了。⽰例脚本如下。
#!/bin/bash
function run_daily_job(){
yesterday=`date -d"1 days ago" +%Y-%m-%d`
now_hour=`date | awk -F":" '{print $1}' | awk '{print $5}'`
echo "Present hour number is____${now_hour}"
# 判断当前hour是不是9点或9点以后
if [ `echo "${now_hour} >= 9" | bc` -eq 1 ]; then
echo "Now it's $1"
echo Starting processing data on ${yesterday}
# DO WHAT YOU WANT HERE.
if [ $? -eq 0 ]; then
echo
echo "0=}========> Job finished."
exit 0
else
echo "Something wrong while running job."
exit -1
fi
else
华为手机黑白屏怎么调回来
current=$1
echo "Current time is ${current}, not the right time(09:00) to run job. Wait another 30 minutes ..."
sleep 30m
fi
}
while (true); do
current_time=`date +"%Y-%m-%d %H:%M:%S"`
黑袍是谁# 此处传参⼀定要加双引号,否则run_daily_job()中的echo "Current time is ${current} ..." 只会显⽰年⽉⽇,不显⽰时分秒
run_job "$current_time"日本留学生签证有什么
done
现在是下午14:29,此时运⾏脚本,输出结果是——
Present hour number is____14
Now it’s 2018-05-24 14:29:57
Starting processing data on 2018-05-23
0=}========> Job finished.
##【延伸】加强脚本的健壮性
如果⽆法获取到now_hour,脚本就失效了。所以,为了保证能在9点钟运⾏我的脚本,需要在没有获取到now_hour变量值的情况下给⼀个默认值。读者可以⾃⼰在上⾯脚本⾥加⼀⾏,加在引⽤now_hour之前的语句即可。
加之前,可以⾃⼰验证⼀下,我这边验证过了,请看——bbs论坛是什么
【若变量hour为空,则⽤ :- 给⼀个默认值】
[tony@test-host-10 business_info]$ hour=''  #此时hour为空
[tony@test-host-10 business_info]$ echo $hour
[tony@test-host-10 business_info]$ hour=${hour:-9}  #hour变量为空,则 :- 给⼀个默认值9
[tony@test-host-10 business_info]$ echo $hour
9
[tony@test-host-10 business_info]$ hour2=${hour2:-15}  #hour2变量为空,则 :- 给⼀个默认值15
[tony@test-host-10 business_info]$ echo $hour2
15
好了就写到这⾥,看官们觉得涨知识了,请在⽂章点个赞 _