jsonfmt.py: Don't sort the JSON, use the new -s/--sort option for that
[sunny256-utils.git] / edit-sqlite3
blob89ad78af8067e8f6d662d1876cc3193fca9296a1
1 #!/bin/sh
3 #==============================================================================
4 # edit-sqlite3
5 # File ID: d6a111fc-6a8c-11e5-b795-fefdb24f8e10
7 # Edit an SQLite database in your favourite text editor.
9 # Author: Øyvind A. Holm <sunny@sunbase.org>
10 # License: GNU General Public License version 2 or later.
11 #==============================================================================
13 progname=edit-sqlite3
14 VERSION=0.4.0
16 opt_help=0
17 opt_dry_run=0
18 opt_quiet=0
19 opt_verbose=0
20 while test -n "$1"; do
21 case "$1" in
22 -h|--help) opt_help=1; shift ;;
23 -n|--dry-run) opt_dry_run=1; shift ;;
24 -q|--quiet) opt_quiet=$(($opt_quiet + 1)); shift ;;
25 -v|--verbose) opt_verbose=$(($opt_verbose + 1)); shift ;;
26 --version) echo $progname $VERSION; exit 0 ;;
27 --) shift; break ;;
29 if printf '%s\n' "$1" | grep -q ^-; then
30 echo "$progname: $1: Unknown option" >&2
31 exit 1
32 else
33 break
35 break ;;
36 esac
37 done
38 opt_verbose=$(($opt_verbose - $opt_quiet))
40 if test "$opt_help" = "1"; then
41 test $opt_verbose -gt 0 && { echo; echo $progname $VERSION; }
42 cat <<END
44 Edit an SQLite 3.x database file in your favourite text editor. The
45 original version is backuped with the current UTC time stamp and process
46 ID in the file name.
48 Usage: $progname [options] sqlite_database_file
50 Options:
52 -h, --help
53 Show this help.
54 -n, --dry-run
55 Simulate what would happen.
56 -q, --quiet
57 Be more quiet. Can be repeated to increase silence.
58 -v, --verbose
59 Increase level of verbosity. Can be repeated.
60 --version
61 Print version information.
63 END
64 exit 0
67 db="$1"
68 sql="$db.sql"
70 test -z "$db" && {
71 echo $progname: No SQLite database specified >&2
72 exit 1
74 test -h "$db" && {
75 echo $progname: $db: File is a symlink >&2
76 exit 1
78 test -f "$db" || {
79 echo $progname: $db: File not found or is not a regular file >&2
80 exit 1
82 test -r "$db" || {
83 echo $progname: $db: File is not readable by you >&2
84 exit 1
86 test -w "$db" || {
87 echo $progname: $db: File is not writable by you >&2
88 exit 1
90 test -n "$EDITOR" || {
91 echo $progname: \$EDITOR environment variable is not defined >&2
92 exit 1
94 sqlite3 "$db" "PRAGMA integrity_check;" || {
95 echo $progname: $db: SQLite database contains errors >&2
96 exit 1
99 bck="$db.$(date -u +"%Y%m%dT%H%M%SZ")-$$.bck"
101 if test "$opt_dry_run" = "1"; then
102 echo $progname: $db: File would be edited >&2
103 exit 0
104 else
105 test -e "$sql" || # Don't overwrite an existing file
106 sqlite3 "$db" .dump >"$sql"
109 cp -p "$db" "$bck" || {
110 echo $progname: $db: Error when creating backup $bck
111 exit 1
114 sqlite_ok=0
115 until test "$sqlite_ok" = "1"; do
116 "$EDITOR" "$sql"
117 rm "$db"
118 sqlite3 "$db" <"$sql" && sqlite_ok=1 || {
119 sqlite_ok=0
120 echo -n "$progname: Press Enter to edit again, " >&2
121 echo -n or q to abort... >&2
122 unset choice
123 read choice
124 test "$choice" = "q" && exit
126 done
128 rm "$db"
129 sqlite3 "$db" <"$sql"
130 rm "$sql"
132 # vim: set ts=8 sw=8 sts=8 noet fo+=w tw=79 fenc=UTF-8 :