· 3 years ago · Oct 25, 2021, 09:10 PM
1#!/bin/bash
2######################################################################
3#Copyright (C) 2021 Kris Occhipinti
4#https://filmsbykris.com
5
6#This program is free software: you can redistribute it and/or modify
7#it under the terms of the GNU General Public License as published by
8#the Free Software Foundation, either version 3 of the License, or
9#(at your option) any later version.
10
11#This program is distributed in the hope that it will be useful,
12#but WITHOUT ANY WARRANTY; without even the implied warranty of
13#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14#GNU General Public License for more details.
15
16#You should have received a copy of the GNU General Public License
17#along with this program. If not, see <http://www.gnu.org/licenses/>.
18######################################################################
19
20mkdir -p "$HOME/.fbk"
21conf="$HOME/.fbk/database.conf"
22
23function create_config(){
24 read -p "Database User: " user
25 read -s -p "Database Password: " password
26 echo "user=$user" > "$conf"
27 echo "password=$password" >> "$conf"
28 echo ""
29}
30
31[[ ! -f "$conf" ]] && create_config
32
33source "$conf"
34
35db="$(mysql -u $user -p$password -e "show databases;"|awk '{print $1}')"
36db="$(echo "new $db"|tr " " "\n"|fzf --prompt "Select Database:")"
37
38if [[ "$db" == "new" ]] || [[ "$db" == "" ]]
39then
40 read -p "Enter the name of your Database: " db
41 mysql -u $user -p$password -e "CREATE DATABASE $db;"
42fi
43
44table="$(mysql -u $user -p$password -e "USE $db;SHOW TABLES;"|awk '{print $1}')"
45table="$((echo "new";echo $table|tr " " "\n")|fzf --prompt "Select Table:")"
46
47if [[ "$table" == "new" ]] || [[ "$table" == "" ]]
48then
49 read -p "Enter the name of your table: " table
50fi
51
52cmd="USE $db;"
53cmd="${cmd}CREATE TABLE IF NOT EXISTS $table ( id INT AUTO_INCREMENT PRIMARY KEY )"
54mysql -u $user -p$password -e "$cmd"
55
56while [ 1 ]
57do
58 read -p "Item Name: " item
59 [[ "$item" == "" ]] && break
60 cmd="USE $db;ALTER TABLE $table ADD COLUMN $item TEXT";
61
62 mysql -u $user -p$password -e "$cmd"
63done
64
65cmd="USE $db;SHOW COLUMNS FROM $table"
66mysql -u $user -p$password -e "$cmd"
67