Skip to content

Instantly share code, notes, and snippets.

@lazypwny751
Last active July 20, 2025 15:47
Show Gist options
  • Select an option

  • Save lazypwny751/53f0e250bf9ef9e1ebc88efff3cc824e to your computer and use it in GitHub Desktop.

Select an option

Save lazypwny751/53f0e250bf9ef9e1ebc88efff3cc824e to your computer and use it in GitHub Desktop.
a simple shell script to create database and tables also it makes defaulting via sqlite.
#!/bin/sh
# This script prepared for dev.to article, please visit and vote us:
# https://dev.to/pinguftw/shell-scriptleri-ile-sqlite-17nk
set -e
DATABASE="${1:-mydb.db}"
read_file() {
if [ ! -f "${1}" ] ; then
echo "file \"${1}\" not found."
return 1
fi
while IFS= read -r line ; do
echo "$line"
done < "${1}"
}
sqlite_comm() {
if [ "${#}" -lt 2 ] ; then
echo "sqlite_comm: required 2 parameters which is file and sql context, usage: sqlite_comm <file> <sql>"
return 1
fi
if ! command -v sqlite3 > /dev/null ; then
echo "sqlite not found in your path, please install or link it as sqlite3"
return 1
fi
sqlite3 "${1}" "${2}"
}
sqlite_wrapper() {
_default_comm="VACUUM;"
sqlite_comm "${DATABASE}" "${1:-$_default_comm}"
}
for s in "scheme/"*".sql" ; do
if [ -f "${s}" ] ; then
printf "%s...[" "${s##*/}"
sqlite_wrapper "$(read_file ${s})"
echo "done]"
fi
done
default_user() {
if [ "${#}" -lt 3 ] ; then
echo "default_user: need to specify 3 parameters: <username> <mail> <password>."
return 1
fi
printf "%s...[" "${1}"
if ! sqlite_comm "$DATABASE" "SELECT 1 FROM user WHERE username = '${1}' LIMIT 1;" | grep -q 1 > /dev/null ; then
sqlite_wrapper "INSERT INTO user (username, email, password) VALUES ('${1}', '${2}', '${3}');"
echo "added]"
else
echo "exists]"
fi
}
for d in "scripts/"*"_user.sh" ; do
( . "./${d}" )
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment