Using bash coproc
You could run OracleDB client as background IO process, in order to inititate only one connection, the keep them open until end of your script.
Something like:
coproc sqlplus -with args -to -connect DB
echo >&${COPROC[1]} "SELECT 3*3 as foo;"
while read -t 1 -ru $COPROC answer ;do
echo "$answer"
done
... to start.
Then in order to avoid useless timeout (read -t 1
), you could prepare some boundary.
Unfortunately (or hopefully) I don't know Oracle DB.
The trick is to make a boundary prompt by using column header
in answer mechanism, then stop reading answer once boundary recieved:
echo >&${COPROC[1]} "$SqlQuery;"'SELECT date(now) as `myBoundary`;'
while read -ru $COPROC answer ;do
[ "$anser" = "myBoundary" ] && break
...
done
read -ru $COPROC lastAnswerDate
I've been posted a full sample at shell_connector.sh. This script could be sourced to use newConnector
function or simply run to see a little demo, using sqlite
, in action.
This script is ready to use with sqlite
, mariadb
and postgreSQL
. If you successfully create a new connector for OracleDB, feel free to share!
Using simple one way fd
If your script don't read anything back from your sql client, you could use a single fifo, then write subsequent request to them:
mkfifo /path/to/mysqlfifo
exec sqlplus -with options </path/to/mysqlfifo &
SQLPLUSPID=$!
Then you could use many time:
echo >/path/to/mysqlfifo "SELECT count(*) FROM SYNC_JOB WHERE STATUS IN('R', 'P');"
...
echo >/path/to/mysqlfifo "UPDATE SYNC_JOB set status='S' WHERE STATUS in('R', 'P')"
Don't forget to
echo >/path/to/mysqlfifo "quit;" # Or exit command regarding to your client's syntax
kill $SQLPLUSPID
Under bash you could avoid external fifo
exec {SQLCLNT}> >(exec sqlplus -with options)
SQLPLUSPID=$!
Then you could use many time:
echo >&$SQLCLNT "SELECT count(*) FROM SYNC_JOB WHERE STATUS IN('R', 'P');"
...
echo >&$SQLCLNT "UPDATE SYNC_JOB set status='S' WHERE STATUS in('R', 'P')"
And once done
echo >&$SQLCLNT "\\q"
kill $SQLPLUSPID