如果“NULL”然后在shell脚本中使用0 [英] If "NULL" then use 0 in shell scripting

查看:665
本文介绍了如果“NULL”然后在shell脚本中使用0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用shell脚本查询配置单元表

  last_val =`hive -eselect max(id )从$ {hivedatabase}。$ {table}`

echo var1 =$ last_val

当last_val =NULL时,我希望last_val为零



我已经尝试过,但仍然是空的

 函数值
{
if [ $ last_val=NULL];然后
echo0
elif [$ last_val!=NULL];然后
echo$ last_val
fi
}


echo var2 =$(value)

我想将这个值用于使用sqoop的增量导入,如

  select * from testing.1234abc check column id>值

如何在shell脚本中实现这一点

 #!/ h2_lin>>解决方案

bin / bash

hivedatabase = mydb
table = mytable
default_value =ZERO

last_val = $(hive -eset hive.cli .print.header = false;从$ {hivedatabase}中选择max(id)。$ {table};)

if [[$ last_val==NULL]];然后
last_val =$ default_value
fi

echo$ last_val


I am using a shell script to query a hive table

last_val="`hive -e "select max(id) from ${hivedatabase}.${table}"`"

echo var1="$last_val"

When the last_val = "NULL" then I want the last_val to be zero

I have tried like below but still I am getting Null

function value
{
   if [ "$last_val" = "NULL" ];then
        echo "0"
   elif [ "$last_val" != "NULL" ];then
        echo "$last_val"
   fi
}


echo var2="$(value)"

I want to use this value in for incremental imports using sqoop like

select * from testing.1234abc check column id > value

How can I achieve that in shell scripting

解决方案

I'm using this approach, try it:

#!/bin/bash

hivedatabase=mydb
table=mytable
default_value="ZERO"

last_val=$(hive -e "set hive.cli.print.header=false; select max(id) from ${hivedatabase}.${table};")

if [[ "$last_val" == "NULL" ]] ; then 
  last_val="$default_value"
fi

echo "$last_val"

这篇关于如果“NULL”然后在shell脚本中使用0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆