while循环的使用,
if循环的使用,
执行hive语句并赋值给shell变量
grep过滤无效字符的使用
#!/bin/bash
start_date=$1
end_date=$2
company=$3
#while循环的使用,注意[] 这两个符号内侧都要留有空格
while [ "$start_date" -le "$end_date" ];
do
current_date=`date -d "$start_date" +%Y-%m-%d`
echo $company
echo "current_date====$current_date"
#这里切记要注意赋值给变量时,=号左右两侧不要有空格!!同时执行hive语句,这里使用反引号给引起来(数字1按键旁边那个符号),以下是直接在本脚本中执行hive语句
counts0=`hive -e "set hive.cli.print.header=false;select case when count(1) > 0 then 1 else 0 end count from dwb.mid_organizationsnapshot_1 where effectivedate='${current_date}' and company='${company}';" | grep -v WARN`
echo $counts0
#if循环的使用 ,注意[] 这两个符号内侧都要留有空格
if [ $counts0 != 1 ];then
echo "***********************************************"
echo "* $company"
echo "* ${current_date} IS NULL THEN INSERT INTO TABLE"
echo "***********************************************"
#以下是执行sql文件,文件中含有sql语句
hive -hiveconf company=$company -hiveconf current_date=$current_date -f /var/lib/hadoop-hdfs/spride_sqoop_beijing/bi_table/tang/qt/organizationSnapshotsql.sql
fi
counts1=`hive -e "set hive.cli.print.header=false;select case when count(1) > 0 then 1 else 0 end count from dwb.mid_organizationsnapshot_1 where effectivedate='${current_date}' and company='${company}';" | grep -v WARN`
echo "${current_date}====${counts1}"
#if循环的使用 ,注意[] 这两个符号内侧都要留有空格
if [ $counts1 == 1 ];then
echo "${current_date} IS NOT NULL"
start_date=$(date -d "$start_date+1days" +%Y%m%d)
else
echo "${current_date} IS NULL"
fi
done