bash-snippets/sqlite3.sh

76 lines
2.1 KiB
Bash

#!/bin/bash
database_path=" "
exit_condition=false
printLastCassette()
{
last_id=$(sqlite3 "${database_path}" "SELECT MAX(id) FROM cassete;")
if [ -z ${last_id} ]
then
echo "Couldn't find cassete entries in \"${database_path}\"."
exit 0
fi
printCassetteById $last_id
}
printCassetteById()
{
local cassette_id=$1
local deposit_ids=$(sqlite3 "${database_path}" "SELECT id FROM deposit WHERE cassete_id = \"${cassette_id}\";")
local validation_id=$(sqlite3 "${database_path}" "SELECT MAX(id) FROM deposit WHERE cassete_id = \"${cassette_id}\";")
if [ -z ${validation_id} ]
then
echo "Couldn't find deposits by cassete id ${cassette_id} in \"${database_path}\"."
else
local cassette_sum=0
local cassette_bills_amount=0
for i in $deposit_ids
do
deposit_sum=$(sqlite3 "${database_path}" "SELECT SUM(value) FROM bills WHERE deposit_id = \"${i}\";")
if [ ! -z ${deposit_sum} ]
then
cassette_sum=$(($cassette_sum+$deposit_sum))
operand=$(sqlite3 "${database_path}" "SELECT COUNT(id) FROM bills WHERE deposit_id = \"${i}\";")
cassette_bills_amount=$(($cassette_bills_amount+$operand))
else
echo "Deposit ${i} is registered but also empty."
fi
done
echo "Sum of cash: ${cassette_sum}"
echo "Amount of bills: ${cassette_bills_amount}"
exit_condition=true
fi
}
if [ ! -f "$database_path" ]
then
echo "Missing \"${database_path}\" database."
exit 0
fi
while [ "$exit_condition" == false ]
do
echo "Show information from the last registered cassete? (y/n)"
read input_char
if [ "$input_char" == "y" ] || [ "$input_char" == "Y" ]
then
printLastCassette
elif [ "$input_char" == "n" ] || [ "$input_char" == "N" ]
then
echo "Input specific cassete id: "
read cassette_id
printCassetteById $cassette_id
else
echo "Please input y to show information about the last active cassete or n to specify cassete id on your own."
echo " "
fi
done