Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /bin/bash
- # Testing 'coproc' feature in Bash with SQLite
- SQL_DB=${1:-test.db}
- coproc sqlproc {
- # non-existig file "none" ignores reading ~/.sqliterc
- sqlite3 -init none
- }
- SQL_IN=${sqlproc[0]}
- SQL_OUT=${sqlproc[1]}
- Sql ()
- {
- local stmt=$1
- if [[ $stmt =~ ^\. ]]; then
- : # Skip, SQLite internal ".command"
- elif [[ ! $stmt =~ \;[[:space:]]*$ ]]; then
- # Add missing semicolong
- stmt+=";"
- fi
- printf '%s\n' "$stmt" '.shell printf \\0' >&"$SQL_OUT"
- IFS= read -r -d '' -u "$SQL_IN" # output in $REPLY
- }
- SqlClose ()
- {
- Sql ".quit"
- wait
- }
- Main ()
- {
- Sql ".open $SQL_DB"
- echo "$REPLY"
- Sql ".tables"
- echo "$REPLY"
- Sql "SELECT * FROM emp;"
- echo "$REPLY"
- }
- # Close coproc on exit
- trap SqlClose EXIT
- Main "$@"
- # End of file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement