1

I'm getting a count of a table of a informix database. I need to store that count value in to a unix variable in side a script. I tried as follows in below two methods .

nwn@nwntt$echo "select count(*) from table1" | dbaccess testdb > $var
-bash: $var: ambiguous redirect

here is my script ,

var1=0


$INFORMIXDIR/bin/dbaccess <<SQLSTMT
        database testdb;

        select count(*) from table1;
SQLSTMT 


echo var1;

I need to store that table1 count in to var1 and echo it.

Nwn
  • 561
  • 2
  • 9
  • 33
  • 1
    How about `var=$(echo "select count(*) from table1" | dbaccess testdb)`? Definitely a duplicate after checking the link. – stephanmg Nov 07 '19 at 12:13
  • 1
    The Bash notation using `variable=$(…)` is a key part of the answer. However, DB-Access is a noisy tool. It writes progress information to standard error, and adds column headings (and blank lines) to the output. A decent approximation to what you're after is: `var=$(echo 'output to "/dev/stdout" without headings select count(*) from table1' | dbaccess testdb - 2>/dev/null)`. That includes two blank lines and the number you want in the variable, `var`; if you use that unquoted, it loses the white space (`echo $var` instead of the normally recommended `echo "$var"`). – Jonathan Leffler Nov 09 '19 at 03:15
  • 1
    An alternative to using DB-Access is to get my SQLCMD program (no relation to Microsoft's johnny-come-lately program of the same name) from the IIUG (International Informix User Group) web site's [software archive](http://www.iiug.org/software). It requires Informix CSDK and a C compiler, but when compiled it generates only the requested output — one of the reasons I wrote it was because I intensely dislike the behaviour of DB-Access and I needed a program that can be used in shell scripts. – Jonathan Leffler Nov 09 '19 at 03:30

0 Answers0