linux:shell:执⾏hive查询并将返回值赋值给shell变量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"
shell脚本返回执行结果#这⾥切记要注意赋值给变量时,=号左右两侧不要有空格!!同时执⾏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