tags:

views:

18

answers:

2

I have a DB2 query in a shell script which return an integer value, but I am unable to store it in a variable.

temp1= echo db2 -x "select max(id) from work.work_tb"

I am getting this output when I run it, sh -x test.sh

  • db2 -x select max(id) from work.work_tb
  • echo 50
  • temp1= 50

So for some reason $temp1 is unable to get the value, I think its because the db2 query is returning value prefixed with \n. How do I get rid of the newline char and get the value to temp1?

A: 
emp1=$(echo db2 -x "select max(id) from work.work_tb")

or using backticks

emp1=`echo db2 -x "select max(id) from work.work_tb"`

In general, to remove newlines, you can pass it to tools like tr/sed etc

... | tr -d "\n"
ghostdog74
A: 

No, that's not why.

temp1=`db2 -x "select max(id) from work.work_tb"`
Ignacio Vazquez-Abrams
tried it , but still the same :(
Nachikethas
yaa thats right
Nachikethas
works now,I was using temp1=echo `db2 -x "select max(id) from work.work_tb"`
Nachikethas
Actually, you have a space between `=` and `echo` in your question, which means that `$temp1` is getting set to nothing before running `echo`, leading to the output you saw.
Ignacio Vazquez-Abrams