Re: SQL from korn shell
Posted in 1998
Oops, two typos in my previous reply. My example should have included
quotes around $ret_val in the test, and I typed rev_val on some lines
by mistake. Try:
Using backtics will avoid anything going to stdout. I use:
ret_val=`
dbaccess $1 - - << EOF 2>/dev/null
OUTPUT TO PIPE "cat" WITHOUT HEADINGS
SELECT $2
FROM $3
WHERE $4 = $5;
EOF
`
ret_val=`echo $ret_val`
if [ -n "$ret_val" ]; then
echo $ret_val > testfile
fi
> mr_mark@my-dejanews.com wrote:
> >
> > I am trying to get a value from a database. I couldn't get the SELECT call
> > to just return a data value without the preceding newlines so I have the
> > OUTPUT TO command sending the output to a file which I then read to eliminate
> > the newlines. All this is initiated from an awk script which has stdout
> > redirected to a file. The SELECT is in a korn shell script. The problem is:
> > even if the SQL table query returns no value I get a newline printed to
> > stdout(my file) and it's not being done by me. Somehow, a dbaccess SQL call
> > automatically behind the scene puts a newline to stdout. Is there a way to
> > supress this newline and better yet to get the original value from the SQL
> > call into a variable. My code is below: # Arguments coming include database,
> > field name being retreived, table to query # field to match key to and the
> > key gotten from the log file. These arguments # must come in this order.
> >
> > dbaccess $1 - - << EOF 2>/dev/null
> > OUTPUT TO sqlout WITHOUT HEADINGS
> > SELECT $2
> > FROM $3
> > WHERE $4 = $5;
> > EOF
> >
> > while read -r line
> > do
> > if [[ -z $line ]]
> > then
> > line=""
> > continue
> > fi
> >
> > print -n "$line" > testfile
> > break
> > done < sqlout
> > rm -f sqlout
>
> Using backtics will avoid anything going to stdout. I use:
> ret_val=`
> dbaccess $1 - - << EOF 2>/dev/null
> OUTPUT TO PIPE "cat" WITHOUT HEADINGS
> SELECT $2
> FROM $3
> WHERE $4 = $5;
> EOF
> `
> ret_val=`echo $ret_val`
> if [ -n $rev_val ]; then
> echo $rev_val > testfile
> fi
--
______________________________________________________________________
| Colin McGrath cmm@trac3000.ueci.com |
| Raytheon Engineers & Constructors, Inc. (215) 422-4144 |
| Phila, PA, USA |
| Any opinions I state are my own and not necessarily of my employer |