Re: SQL from korn shell
Posted in 1998
Try something like this:
{
{
dbaccess $database << EOF
select tabname
from systables
where tabid <= 5;EOF
} 2>/dev/null | tail +5 | sed '$d'
} | while read x
do
echo "x=$x"
done
The tail gets rid of the first 4 lines, the sed removes the extra line
at the end
Cheers
Gabor
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
>
> -----------== Posted via Deja News, The Discussion Network ==----------
> http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own