Cover V01, I04
Article
Listing 1
Listing 2

nov92.tar


Listing 1: runsql/runsql2

#
# runsql:   Execute RDSQL script, discard stdout and stderr streams
# runsql2:  Same, do not discard stdout and stderr.
#
# Written by Leor Zolman, 7/92
#
# usages:
#       runsql.sh <database> [scriptfile]
#               (stdout and stderr stream output discarded)
#
#       runsql2.sh <database> [scriptfile] >outstream 2>errstream
#               (stderr and stdout steams passed through unchanged)
#
# Both read sql script from scriptfile, or from standard input
# if no file given.
#
# Example (from within a shell script):
#
#     runsql2 accounts >sql.out 2>sql.err <<END
#         unload to "table.txt"
#            select * from table;
#     END
#
# NOTES:
#   1. If script is fed from the standard input, no "database"
#       statement is necessary. If the filename method is used,
#       then the "database" statement IS required in the input file.
#

[ $# -lt 1 ] && echo "usage: $0 <database> [scriptfile]" && exit

if [ $# -eq 2 ]
then
if [ ! -r $2 ]
then
echo "$0: script file $1 not found." >&2
exit 1
fi

if [ `basename $2 .sql`.sql != $2 ]  # make sure file name ends in .sql
then
scriptfile=`tmpname`.sql    # if not, create a temporary file
cp $2 $scriptfile           # copy script file to it
chmod 777 $scriptfile
usingtmp=Y                  # set flag so we erase temp file later
else
scriptfile=$2
fi
else
scriptfile=`tmpname`.sql
cat >$scriptfile <<-END
database $1;
END
cat >>$scriptfile
usingtmp=Y
fi

if [ $0 = runsql2 ]
then
isql -s $1 -qr $scriptfile                  # save output streams
else
isql -s $1 -qr $scriptfile >/dev/null 2>&1  # discard output streams
fi

[ "$usingtmp" = Y ] && rm $scriptfile