#!/usr/bin/env bash # https://perlmonks.org/?node_id=11150229 # SQLite llil query script. # Usage: bash llil_sql_qry1 hash1.db > out.txt # SQL function make_script () { read -r -d '' var << 'EOF' -- Set SQLite pragmas. -- .output /dev/null PRAGMA cache_size = 10000; PRAGMA page_size = 4096; PRAGMA journal_mode = OFF; PRAGMA synchronous = OFF; PRAGMA temp_store = MEMORY; .output -- A query index is not needed for one-time use. -- -- CREATE INDEX IF NOT EXISTS "query_idx" ON "kv_store" ( -- "value" DESC, "name" -- ); -- Output records. -- .mode csv .headers off .separator ROW "\n" .separator "\t" SELECT "name", "value" FROM "kv_store" ORDER BY "value" DESC, "name"; EOF echo "$var" } # RUN DB="$1"; shift if [[ ! "$DB" =~ .db$ ]]; then echo "usage: bash $0 hash1.db" >&2 exit 1 elif [[ ! -r "$DB" ]]; then echo "$DB: cannot open '$DB' (No such file)" >&2 exit 1 fi SQLCMND=$( command -v sqlite3 ) if [[ -z "$SQLCMND" ]]; then echo "sqlite3: command not found" >&2 exit 1 fi SQLSCRIPT="$( make_script )" exec "$SQLCMND" "$DB" < <(echo "$SQLSCRIPT")