Re: create database in shell script
Posted in 1995
jasonfb@panix.com (Jason Foster-Bey) wrote:
>A better way to do this is:
>
>cat <<EOF > tmp.sql
>create database $1 with log ansi;
>EOF
>
>This way, you can check the exit status in $? upon completion. Otherwise, you
>will always get a 1 for the exit status.
I found that the exit status of various releases of isql is unreliable. I used
to chain forms and reports together in scripts as a substitute to 4gl (which I
did not have available at the time) and used to structure, build, compile and
run queries, forms and reports in scripts on the basis of that which had gone
before.
I had to incorporate some pretty careful error checking at all stages. I was
never able to rely on the exit status. Therefore, the output of file descriptor
2 (standard error) was redirected from isql under control of the _here_
document to a file (as you have done here) which I then scanned with grep for
the key word "error". If "error" was ever encountered, an appropriate error
handling routine was invoked:
# Generate the data
isql -qr <<! >$stage.rep 2>$stage.err
database $database;
select ...!
# Check for errors
if grep -i "error" $stage.err >/dev/null
then
...error_handler...
fi
In my experience, the only time "error" ever appeared on standard error was if
there was an error. Any time there was an error, either "error" or "Error"
would appear on standard error. The error handler then parsed the report file
and error file, checking for common error types and either advised the user of
required corrective action (privileges) or corrected correctable situations
(missing tables and so on). I wrote an entire inventory acquisition financial
control system for aircraft spares using isql and the Bourne shell (including
interactive, multi level menu control).
Gary