Results 1 to 6 of 6
  1. #1
    Join Date
    May 2012
    Posts
    155

    Unanswered: Using output of db2 command line in shell

    Hallo everyone,

    I am running this command line for my shell variable:


    Code:
    myVar="$(db2 -x "select count(*) from mySchema.myTable")"
    Let´s say, the output is one --> 1

    I wanted to use this number for my case construction:

    Code:
    case $myVar in 
    0 ) 
    .... ;; 
    1 ) 
    .... ;; 
    esac
    But it did not work. I was wondering why, and then I found a possible reason:

    If I echo myVar, it gives me the correct answer:

    Code:
    echo ${myVar} 
    1

    But if I echo something with myVar inside, it gives me the REAL structure of myVar, which are some white spaces:
    Code:
    echo "word $myVar" 
    word               1
    I assume, this is why I cant use myVar in my case structure. Somebody any idea how to solve this problem.


    I have try:
    Code:
    ... | tr -d  ' '
    It also didnt work.

    Regards,

    Ratna

  2. #2
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713
    Try this:
    Code:
    typeset -i myVar=$(db2 -x "select count(*) from mySchema.myTable")
    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

  3. #3
    Join Date
    Nov 2010
    Posts
    99
    Someone else probably has a more elegant answer but you could write the results to a file and read the file like this:

    db2 -x "select count(*) from mySchema.myTable" > sql.txt
    myvar=sql.txt
    cat $myvar | while read fileline
    do
    cnt=`echo $fileline | awk '{print $1}'`
    done
    case ...interrogate $cnt as you wish...

  4. #4
    Join Date
    Mar 2003
    Posts
    280
    Myvar2=`echo $myvar | sed -e "s/ //g"`

    Using my phone, therefor untested

  5. #5
    Join Date
    May 2012
    Posts
    155
    Hallo lelle12, James123, and LKBrwn_DBA,

    thank you gentlemen for the advices, the trick with typeset and sed have finally helped me out to code the script like I wanted.

    Regards,

    Ratna

  6. #6
    Join Date
    Mar 2006
    Location
    Bogota, Colombia
    Posts
    21

    Subshell problem

    When you execute a command with backquotes (backtick) or $(), you are creating a subshell. In that new subshell you have to connect to the database. It does not use the current connection.

    shell

    db2 connect to sample
    db2 connect
    # Last command will say that there is a connection to sample
    var=$(db2 connect)
    # It will say that there is not any established connection, because in the subshell there were not connection.


    You can fix that with this:

    var=$(db2 connect to sample > /dev/null ; db2 connect)
    var=`db2 connect to sample > /dev/null ; db2 connect`


    These last two are equivalents.
    AngocA

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •