1

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

  • Are you actually getting the word `NULL` back from your db?. If so, is it `Null` or `NULL`? – JNevill Mar 29 '17 at 19:04
  • @JNevill I am getting NULL –  Mar 29 '17 at 19:31
  • Invocation with `bash -x yourscript` -- thus logging every command with its arguments, including your `[` commands used to run the tests -- is likely to be informative. If there's a hidden character that's throwing off the test, that'll show it. – Charles Duffy Mar 29 '17 at 20:57
  • @JNevill I believe that the NULL is a string –  Mar 29 '17 at 22:30
  • @CharlesDuffy I believe that the NULL is a string –  Mar 29 '17 at 22:31
  • @Active_user, yes, it definitely is a string, but the question is if that's the **complete** string or if there are hidden characters or such next to it. The logs with `bash -x` or `set -x` will make that clear. – Charles Duffy Mar 29 '17 at 22:31
  • @Active_user, ...in the case of `foo=$(bar)` or any equivalent thereto, `foo` is **always** a string -- there's nothing else it can be, so the assertion doesn't really add value. (`declare -i` will mark `foo` as a string that's only allowed to contain numeric values, and for which assignments are automatically interpreted in arithmetic context but it's still a string -- just a restricted one with special rules). – Charles Duffy Mar 29 '17 at 22:33
  • BTW, as an aside, `function foo {` is actually bad form: It's needlessly incompatible with POSIX-standardized syntax. `foo() {`, removing the `function` keyword and adding `()`, will work on *all* POSIX-compliant shells. – Charles Duffy Mar 29 '17 at 22:35
  • @CharlesDuffy No there are no hidden characters. I will either get a NULL or a number –  Mar 29 '17 at 22:45
  • Don't tell us, **show us** with `bash -x` logs. How do we know that you're testing for hidden characters in a way that's actually reliable and robust unless you've shown your test methodology and results? – Charles Duffy Mar 29 '17 at 22:54
  • @Active_user I don't see any problem with the individual pieces as such, though the `sqoop` command is obviously not valid. Are you running the pieces in posted order in the same script? Is it currently writing `var2="NULL"` followed by `var1="NULL"`, while you expected `var2="NULL"` followed by `var1="0"`? Please ensure that you're 100% precise with reporting what you see, because individual characters can reveal a lot. – that other guy Mar 29 '17 at 23:26
  • @thatotherguy I have edited the question Sorry for the confusion. Yes `var1="NULL"` and `var2="0"` –  Mar 29 '17 at 23:51
  • @Active_user Can you post a screenshot of the terminal instead? Your echo statements should be writing `var1=NULL`, not `var1="NULL"`. It's makes a huge difference. – that other guy Mar 30 '17 at 02:11
  • @thatotherguy `var1=NULL` sorry I put the wrong code –  Mar 30 '17 at 03:14
  • By the way -- really, seriously, use `$()` instead of backticks. There are semantic differences between them, and those differences can be important; see the comments in http://stackoverflow.com/a/9405617/14122 And, *really*, **seriously**, edit your question to include the output of reproducing the issue with `bash -x`! – Charles Duffy Mar 30 '17 at 15:06

1 Answers1

0

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"
leftjoin
  • 36,950
  • 8
  • 57
  • 116
  • 1
    `echo "$last_val"` is a truer representation of the contents of the variable than `echo $last_val` -- see [BashPitfalls #14](http://mywiki.wooledge.org/BashPitfalls#echo_.24foo). – Charles Duffy Mar 29 '17 at 20:56
  • @leftjoin The table which I query is an empty table. so when I query it gives me NULL, so what I want to do is if it is NULL then take last val as ZERO or else return lastval –  Mar 29 '17 at 20:57
  • @leftjoin I believe that the NULL is a string –  Mar 29 '17 at 22:31
  • @Charles Duffy thanks, you ere right, I have fixed it. – leftjoin Mar 30 '17 at 06:54
  • @Active_user Please see my updated answer. Just substitute variable `default_value` value with yours. I have checked it works, `echo` command prints ZERO – leftjoin Mar 30 '17 at 06:56