├── DropTable.sh ├── InsertTable.sh ├── ListTables.sh ├── Readme.md ├── connect.sh ├── createTable.sh ├── createdb.sh ├── dbs └── company │ ├── students │ └── students.types ├── deleteFrom.sh ├── dropdb.sh ├── entry.sh ├── helpers.sh ├── listdbs.sh ├── main.sh ├── screenshots ├── Operations.jpeg ├── createTable.jpeg ├── fieldName.jpeg ├── fieldType.jpeg ├── insertFields.jpeg ├── insertFields2.jpeg ├── insertTableName.jpeg ├── noFields.jpeg └── primaryKey.jpeg ├── selectFrom.sh └── updateTable.sh /DropTable.sh: -------------------------------------------------------------------------------- 1 | clear 2 | kdialog --title "Table Name" --inputbox "What is the name of the Table you want to remove?" > out 3 | 4 | for choice in $(cat out) ;do 5 | if [ -f dbs/"$connectDbName"/$choice ];then 6 | rm dbs/"$connectDbName"/$choice 7 | rm dbs/"$connectDbName"/$choice.types 8 | else 9 | kdialog --sorry "Table Name doesnt exist" 10 | 11 | fi 12 | 13 | done 14 | -------------------------------------------------------------------------------- /InsertTable.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/bash 2 | function checkDataType(){ 3 | case "$1" in 4 | "int") 5 | if [[ "$2" =~ ^-?[0-9]+$ ]];then 6 | echo "true" 7 | else 8 | echo "false" 9 | fi 10 | ;; 11 | "str") 12 | if [[ "$2" =~ [^a-zA-Z0-9] ]];then 13 | echo "false" 14 | else 15 | echo "true" 16 | fi;; 17 | *) echo "false" 18 | ;; 19 | esac 20 | } 21 | echo "##############################" 22 | echo $connectDbName 23 | echo "##############################" 24 | tableName=$(kdialog --title "Table Name" --inputbox "Please Enter Table Name") 25 | if [ -f dbs/"$connectDbName"/$tableName ];then 26 | 27 | number=$(wc -l dbs/"$connectDbName"/"$tableName.types" | awk '{print $1}') 28 | number=$(( $number-1 )) 29 | 30 | 31 | output="" 32 | for ((i=0;i<"$number";i++));do 33 | #skip the first line 34 | lineNumber=$(( $i+2 )) 35 | fieldName=$(awk -F, -v lineNumber="$lineNumber" '{if (NR==lineNumber) print $1 }' dbs/"$connectDbName"/"$tableName.types") 36 | fieldType=$(awk -F, -v lineNumber="$lineNumber" '{if (NR==lineNumber) print $2 }' dbs/"$connectDbName"/"$tableName.types") 37 | fields=$(kdialog --title "Fields" --inputbox "Please Enter $fieldName ") 38 | if [ $i -eq 0 ];then 39 | exitflag=0 40 | isUnique=`awk -F',' -v id="$fields" '{if($1==id) print}' dbs/$connectDbName/$tableName` 41 | while [ $exitflag -eq 0 ];do 42 | if [ -z $isUnique ];then 43 | exitflag=1 44 | 45 | else 46 | kdialog --sorry "Primary Key should be unique ,Enter another one please" 47 | fields=$(kdialog --title "Fields" --inputbox "Please Enter $fieldName ") 48 | isUnique=`awk -F',' -v id="$fields" '{if($1==id) print}' dbs/$connectDbName/$tableName` 49 | fi 50 | done 51 | fi 52 | type=`checkDataType $fieldType $fields ` 53 | pk=$(awk -F, '{if (NR==1) print $2 }' dbs/"$connectDbName"/"$tableName.types") 54 | 55 | while [ $type = "false" ];do 56 | echo "in while condition" 57 | fields=$(kdialog --title "Fields" --inputbox "Please Enter $fieldName ") 58 | type=`checkDataType $fieldType $fields` 59 | 60 | 61 | 62 | done 63 | output+="$fields," 64 | 65 | done 66 | echo "$output" >> dbs/"$connectDbName"/"$tableName" 67 | sed -i 's/,$/\ /' dbs/"$connectDbName"/$tableName"" 68 | else 69 | kdialog --sorry "Table Name doesnt exist" 70 | fi 71 | -------------------------------------------------------------------------------- /ListTables.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/bash 2 | #@todo , handle cancel event and add a nice decription of the tables in the cli 3 | 4 | var=$(ls -I "*.types" dbs/$connectDbName) 5 | 6 | zenity --list --title="List Tables" --column="Tables" $var 7 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # Bash Shell DBMS 2 | 3 | ``` 4 | | __ ) __ _ ___| |__ | _ \| __ )| \/ / ___| 5 | | _ \ / _ / __| '_ \ | | | | _ \| |\/| \___ \ 6 | | |_) | (_| \__ \ | | | | |_| | |_) | | | |___) | 7 | |____/ \__,_|___/_| |_| |____/|____/|_| |_|____/ 8 | 9 | 1) Press 1 to create database 4) press 4 to drop database 10 | 2) press 2 to list databases 5) press 5 to exit 11 | 3) press 3 to connect to database 12 | Please enter a choice: 13 | ``` 14 | ## Getting Started 15 | ### Prerequisites 16 | Before starting you will need to install kdialog for the gui app to start 17 | 18 | if you start the scripts without having kdialog installed you will be canceled out and prompted to install kdialog 19 | 20 | ### For Ubuntu: 21 | ``` 22 | sudo apt-get kdialog 23 | ``` 24 | ### For Centos: 25 | ``` 26 | sudo yum install kdialog 27 | ``` 28 | ### Warning :no_entry_sign: : 29 | 1. Please make sure the dbs folder exists in your directory. 30 | 2. if you dont have an empty folder named dbs it will not work 31 | ## Running: 32 | for starting up you will need to start with the entry.sh Bash script file 33 | ``` 34 | ./entry.sh 35 | ``` 36 | you will be propmted by a list of choices as mentioned above. 37 | 38 | 1. Enter 1 to create your first database :sunglasses: :hammer: 39 | 40 | :no_entry_sign: Warning : you can't Enter a Database name with special characters or spaces in between, if you do enter it you will be prompted with an error message. 41 | 42 | now you can connect to your database through the "3" option and entering your database name 43 | ``` 44 | ____ _ ____ ____ __ __ ____ 45 | | __ ) __ _ ___| |__ | _ \| __ )| \/ / ___| 46 | | _ \ / _ / __| '_ \ | | | | _ \| |\/| \___ \ 47 | | |_) | (_| \__ \ | | | | |_| | |_) | | | |___) | 48 | |____/ \__,_|___/_| |_| |____/|____/|_| |_|____/ 49 | 50 | Press 1 to create database 51 | Press 2 to list databases 52 | Press 3 to connect to database 53 | Press 4 to drop database 54 | Press 5 to exit 55 | please enter a choice: 1 56 | Please enter the database name: company 57 | Database created successfully 58 | please enter a choice: 3 59 | Please enter the name of the database you want to connect to 60 | Name: company 61 | ``` 62 | 2. you have succefully connected to the database to perform your operations 63 | 64 | 3. operations include 65 | * Create Table 66 | * Update Table 67 | * Drop Table 68 | * Select from Table 69 | * Drop Table 70 | * Insert into Table 71 | * List Tables 72 | * Delete From Table 73 | 74 | Now you Will be prompted with a a list of these operations to choose from 75 | ![Image of Operations](screenshots/Operations.jpeg) 76 | 77 | ## Create Table 78 | 79 | Lets start with Create Table ,You will be prompted to Enter your Table name. 80 | 81 | ### :no_entry_sign: Warning : you can't Enter a Table name with special charachters or spaces in between, if you do enter it you will be prompted with an error message. 82 | ![Image of Operations](screenshots/createTable.jpeg) 83 | 84 | Now it will ask you to enter the number of Fields in the table, it should be an integer number 85 | ![Image of Operations](screenshots/noFields.jpeg) 86 | 87 | After that you will be filling the name and data types of each Field 88 | 89 | The only data types are: 90 | 1. int 91 | 2. str 92 | 93 | Field Name | Field Type 94 | ------------ | ------------- 95 | ![Image of Operations](screenshots/fieldName.jpeg)| ![Image of Operations](screenshots/fieldType.jpeg) 96 | 97 | ### Field Names Should be unique , You can't enter more than one Field with the same name 98 | 99 | You will be asked to enter the name of the primary key field you want 100 | 101 | ![Image of Operations](screenshots/primaryKey.jpeg) 102 | 103 | ### You have Successfully created your first table :partying_face: , now it's time to insert some data into the table you created. 104 | 105 | ### Lets connect to our Database again to perform some inserts 106 | 107 | 108 | ## Insert into Table 109 | 110 | lets Enter our table name we want to insert into: 111 | ![Image of Operations](screenshots/insertTableName.jpeg) 112 | 113 | you will now start filling the fields: 114 | 115 | ![Image of Operations](screenshots/insertFields.jpeg) ![Image of Operations](screenshots/insertFields2.jpeg) 116 | ### We have inserted into our table, lets update our table to change the name from mahmood to islam 117 | 118 | ## Update Table 119 | 120 | upon connecting to the database and selecting the table you want to update you will see the fields you have in you table right now and will be asked to enter the primary key which is the first field 121 | 122 | Now we enter the number of field we which want to update in our case its the second field so we enter 2 , and enter the new value islam 123 | 124 | ``` 125 | Please enter a table name: students 126 | #################################### 127 | 1,mahmood 128 | #################################### 129 | Please Enter row's primary key that you want to update 130 | Primary Key: 1 131 | Please Enter a valid field number: 2 132 | Please Enter the new value: islam 133 | Row updated successfully 134 | 135 | ``` 136 | ### Now lets check your table file you will find it updated with the new value ,lets now try to insert another field and select it with the Primary key 137 | 138 | ## Select From Table 139 | 140 | You can select a specific record using the primary key field, meaning if the primary key is the id then you need to provide the id of the record you want to select . 141 | 142 | ``` 143 | Please enter a table name: students 144 | Please Enter row's primary key that you want to select 145 | Primary Key: 2 146 | ########################## 147 | 2,mahmood 148 | ########################## 149 | 150 | ``` 151 | ### We have successfully fetched the second record, now lets try to delete that record 152 | 153 | ## Delete From Table 154 | 155 | Similar to update , if you want to delete a record you need to provide the primary key of that record to delete it from our Database 156 | 157 | ``` 158 | Please enter a table name: students 159 | #################################### 160 | 1,islam 161 | 2,mahmood 162 | #################################### 163 | Please Enter row's primary key that you want to delete 164 | Primary Key: 2 165 | Row deleted Successfully 166 | 167 | ``` 168 | ### Checking your file you will notice that record deleted 169 | 170 | ### You have now performed all the CRUD operations in our database :partying_face: 171 | 172 | ### Try dropping this Existing table and creating one on your own :nerd_face: -------------------------------------------------------------------------------- /connect.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/bash 2 | 3 | echo "Please enter the name of the database you want to connect to" 4 | echo -n "Name: " 5 | read name 6 | 7 | exitflag=0 8 | 9 | 10 | while [ $exitflag -eq 0 ];do 11 | 12 | if [ -z $name ];then 13 | echo -n "Please enter a name: " 14 | read name 15 | continue 16 | fi 17 | if [ -d dbs/$name ];then 18 | connectDbName=$name 19 | exitflag=1 20 | else 21 | echo "No such Database found" 22 | echo -n "Enter another name: " 23 | read name 24 | fi 25 | done 26 | 27 | . ./main.sh -------------------------------------------------------------------------------- /createTable.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/bash 2 | clear 3 | title=$(kdialog --title "Table Name" --inputbox "What is the name of your Table?") 4 | if [ -z "$title" ];then #check if cancel was clicked 5 | 6 | 7 | . ./main.sh #returning to main menu 8 | break 9 | fi 10 | #checks if tablename conatians special cahracters 11 | if [[ $title == *['!''?'@\#\$%^\&*()-+\.\/';']* ]] 12 | then 13 | kdialog --sorry "! @ # $ % ^ () ? + ; . - are not allowed!" 14 | . ./main.sh 15 | break 16 | 17 | fi 18 | #checks if tablename contains spaces 19 | if [ -z "$title" ];then #check if cancel was clicked 20 | 21 | . ./main.sh #returning to main menu 22 | break 23 | fi 24 | if [[ $title = *" "* ]]; then 25 | kdialog --sorry "spaces are not allowed!" 26 | . ./main.sh 27 | break 28 | fi 29 | echo "$title" > out 30 | 31 | 32 | 33 | for choice in $(cat out) ;do 34 | echo "$choice" 35 | if [ -f dbs/"$connectDbName"/$choice ];then 36 | kdialog --sorry "Table Name Already exists" 37 | exit 38 | else 39 | touch dbs/"$connectDbName"/$choice 40 | fi 41 | 42 | 43 | done 44 | 45 | continueFlag=0 #checks if there are no errors in datatype 46 | tableName=`cat out` 47 | input=$(kdialog --title "Number of Table Fields" --inputbox "Please Enter the number of fields") 48 | if [ -z "$input" ];then #check if cancel was clicked 49 | 50 | rm dbs/"$connectDbName"/"$tableName" 51 | #exit 52 | . ./main.sh 53 | break 54 | fi 55 | 56 | type=`checkDataType int $input ` #checks if no of fields is of type integer 57 | while [ $type = "false" ];do 58 | echo "in while condition" 59 | input=$(kdialog --title "Number of Table Fields" --inputbox "Please Enter the number of fields") 60 | if [ -z "$input" ];then #check if cancel was clicked 61 | 62 | rm dbs/"$connectDbName"/"$tableName" 63 | . ./main.sh 64 | break 65 | fi 66 | type=`checkDataType int $input` 67 | 68 | 69 | 70 | done 71 | 72 | 73 | 74 | for ((i=0;i<"$input";i++)) ; do 75 | number=$(( $i+1 )) 76 | name=$(kdialog --title "Field Names" --inputbox "Please Enter the $number field name") 77 | isUnique=$(awk -F',' -v id="$name" '{if($1==id) print}' dbs/$connectDbName/"$tableName.types") 78 | echo "$isUnique" 79 | 80 | if [ -z "$name" ];then #check if cancel was clicked 81 | 82 | rm dbs/"$connectDbName"/"$tableName" 83 | if [ -f dbs/"$connectDbName"/"$tableName.types" ];then #if user canceled in second iteration the tablename.types should be removed 84 | rm dbs/"$connectDbName"/"$tableName.types" 85 | fi 86 | . ./main.sh 87 | break 88 | fi 89 | exitFlag=0 90 | while [ $exitFlag -eq 0 ];do 91 | 92 | if [ -z $isUnique ];then 93 | 94 | exitFlag=1 95 | 96 | else 97 | 98 | kdialog --sorry "Please Enter a unique Field Name" 99 | name=$(kdialog --title "Field Names" --inputbox "Please Enter the $number field name") 100 | if [ -z "$name" ];then #check if cancel was clicked 101 | 102 | rm dbs/"$connectDbName"/"$tableName" 103 | if [ -f dbs/"$connectDbName"/"$tableName.types" ];then #if user canceled in second iteration the tablename.types should be removed 104 | rm dbs/"$connectDbName"/"$tableName.types" 105 | fi 106 | exit #breaks from program if entering a unique field fails 107 | 108 | fi 109 | isUnique=$(awk -F',' -v id="$name" '{if($1==id) print}' dbs/$connectDbName/"$tableName.types") 110 | fi 111 | done 112 | 113 | 114 | type=$(kdialog --title "Field Types" --inputbox "Please Enter the $number field int or str") 115 | 116 | if [ -z "$type" ];then #check if cancel was clicked 117 | 118 | rm dbs/"$connectDbName"/"$tableName" 119 | if [ -f dbs/"$connectDbName"/"$tableName.types" ];then #if user canceled in second iteration the tablename.types should be removed 120 | rm dbs/"$connectDbName"/"$tableName.types" 121 | fi 122 | #exit 123 | . ./main.sh 124 | break 125 | fi 126 | case "$type" in 127 | "str") echo "$name,$type" >>dbs/"$connectDbName"/"$tableName.types";; 128 | "int") echo "$name,$type" >>dbs/"$connectDbName"/"$tableName.types";; 129 | 130 | *) 131 | kdialog --sorry "Please Enter a valid data type"; 132 | rm dbs/"$connectDbName"/"$tableName"; 133 | continueFlag=1; 134 | break; 135 | ;; 136 | esac 137 | 138 | done 139 | if [ $continueFlag -eq 0 ];then 140 | pk=$(kdialog --title "Field Names" --inputbox "Please Enter the Primary key") 141 | #check if the key entered is in valid fields otherwise show error message and read key again 142 | awk -F, '{print $1};' dbs/"$connectDbName"/"$tableName.types">>keys 143 | flag=1 144 | while [ $flag -eq 1 ] ;do 145 | if grep -Fxq "$pk" keys 146 | then 147 | #if key is found 148 | key=$(awk /$pk/ dbs/"$connectDbName"/"$tableName.types") #gets the key 149 | sed -i "1 i\\$key" dbs/"$connectDbName"/"$tableName.types" 150 | line=$(awk -F',' -v pk="$pk" '{if($1==pk) print NR}' dbs/"$connectDbName"/"$tableName.types" | tail -1) 151 | sed -i "$line d" dbs/"$connectDbName"/"$tableName.types" #deletes the line 152 | sed -i "1 i\pk,$pk" dbs/"$connectDbName"/"$tableName.types" 153 | 154 | 155 | flag=0 156 | else 157 | #if key is not found 158 | kdialog --sorry "Please Enter a valid key name" 159 | pk=$(kdialog --title "Field Names" --inputbox "Please Enter the Primary key") 160 | fi 161 | done 162 | fi 163 | if [ -f keys ];then 164 | rm keys 165 | fi 166 | -------------------------------------------------------------------------------- /createdb.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/bash 2 | 3 | echo -n "Please enter the database name: " 4 | read name 5 | exitflag=0 6 | 7 | 8 | while [ $exitflag -eq 0 ];do 9 | 10 | if [[ $name == *['!''?'@\#\$%^\&*()-+\.\/';']* ]] 11 | then 12 | echo "! @ # $ % ^ () ? + ; . - are not allowed!" 13 | echo -n "Please enter a valid name: " 14 | read name 15 | continue 16 | fi 17 | 18 | if [[ $name = *" "* ]]; then 19 | echo "spaces are not allowed!" 20 | echo -n "Please enter a valid name: " 21 | read name 22 | continue 23 | fi 24 | 25 | if [ -z $name ];then 26 | echo -n "Please enter a name: " 27 | read name 28 | continue 29 | fi 30 | if [ -d dbs/$name ];then 31 | echo "Database already exists" 32 | echo -n "Please enter another name: " 33 | read name 34 | else 35 | mkdir dbs/$name/ 36 | echo "Database created successfully" 37 | exitflag=1 38 | fi 39 | done 40 | -------------------------------------------------------------------------------- /dbs/company/students: -------------------------------------------------------------------------------- 1 | 1,islam 2 | -------------------------------------------------------------------------------- /dbs/company/students.types: -------------------------------------------------------------------------------- 1 | pk,id 2 | id,int 3 | name,str 4 | -------------------------------------------------------------------------------- /deleteFrom.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/bash 2 | 3 | getTableName 4 | 5 | listTableContents 6 | 7 | echo "Please Enter row's primary key that you want to delete" 8 | echo -n "Primary Key: " 9 | read pk 10 | 11 | fieldNum=`awk -F',' -v pk="$pk" '{if($1==pk) print NR}' dbs/$connectDbName/$tableName` 12 | 13 | exitflag=0 14 | 15 | while [ $exitflag -eq 0 ];do 16 | if [ -z $fieldNum ];then 17 | echo -n "Please enter a valid primary key: " 18 | read pk 19 | fieldNum=`awk -F',' -v pk="$pk" '{if($1==pk) print NF}' dbs/$connectDbName/$tableName` 20 | else 21 | exitflag=1 22 | fi 23 | done 24 | 25 | rowNum=`awk -F',' -v fn="$pk" '{if($1==fn) print NR}' dbs/$connectDbName/$tableName` 26 | 27 | awk -F',' -v rn="$rowNum" '{if(NR!=rn) print}' dbs/$connectDbName/$tableName > dbs/$connectDbName/transitionFile 28 | cat dbs/$connectDbName/transitionFile > dbs/$connectDbName/$tableName 29 | rm dbs/$connectDbName/transitionFile 30 | echo "Row deleted Successfully" -------------------------------------------------------------------------------- /dropdb.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/bash 2 | echo -n "Please enter the database name that you want to drop: " 3 | read name 4 | 5 | echo "##########################################" 6 | if [ -d dbs/$name ];then 7 | rm -r dbs/$name 8 | echo " Database dropped" 9 | else 10 | echo "no such database found" 11 | fi 12 | echo "##########################################" 13 | printMainMenu 14 | -------------------------------------------------------------------------------- /entry.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/bash 2 | 3 | ###Entry point for DBMS 4 | 5 | clear 6 | . ./helpers.sh 7 | PS3="Please enter a choice: " 8 | connectDbName="" 9 | 10 | echo " ____ _ ____ ____ __ __ ____ 11 | | __ ) __ _ ___| |__ | _ \| __ )| \/ / ___| 12 | | _ \ / _ / __| '_ \ | | | | _ \| |\/| \___ \ 13 | | |_) | (_| \__ \ | | | | |_| | |_) | | | |___) | 14 | |____/ \__,_|___/_| |_| |____/|____/|_| |_|____/ " 15 | 16 | echo 17 | options=("Press 1 to create database" "press 2 to list databases" "press 3 to connect to database" "press 4 to drop database" "press 5 to exit") 18 | select choice in "${options[@]}" 19 | do 20 | case $REPLY in 21 | 1) . ./createdb.sh;; 22 | 2) . ./listdbs.sh;; 23 | 3) . ./connect.sh 24 | printMainMenu;; 25 | 4) . ./dropdb.sh;; 26 | 5) echo "Bye" 27 | exit;; 28 | *) echo "Please enter a valid option";; 29 | esac 30 | done 31 | -------------------------------------------------------------------------------- /helpers.sh: -------------------------------------------------------------------------------- 1 | function printMainMenu(){ 2 | 3 | echo " ____ _ ____ ____ __ __ ____ 4 | | __ ) __ _ ___| |__ | _ \| __ )| \/ / ___| 5 | | _ \ / _ / __| '_ \ | | | | _ \| |\/| \___ \ 6 | | |_) | (_| \__ \ | | | | |_| | |_) | | | |___) | 7 | |____/ \__,_|___/_| |_| |____/|____/|_| |_|____/ " 8 | 9 | PS3="please enter a choice: " 10 | echo "Press 1 to create database" 11 | echo "Press 2 to list databases" 12 | echo "Press 3 to connect to database" 13 | echo "Press 4 to drop database" 14 | echo "Press 5 to exit" 15 | } 16 | 17 | function checkDataType(){ 18 | case "$1" in 19 | "int") 20 | if [[ "$2" =~ ^-?[0-9]+$ ]];then 21 | echo "true" 22 | else 23 | echo "false" 24 | fi 25 | ;; 26 | "str") 27 | if [[ "$2" =~ [^a-zA-Z0-9] ]];then 28 | echo "false" 29 | else 30 | echo "true" 31 | fi;; 32 | *) echo "false" 33 | ;; 34 | esac 35 | } 36 | 37 | function getTableName(){ 38 | exitflag=0 39 | 40 | tableName="" 41 | while [ $exitflag -eq 0 ];do 42 | 43 | if [ -z $tableName ];then 44 | echo -n "Please enter a table name: " 45 | read tableName 46 | continue 47 | fi 48 | if [ -f dbs/$connectDbName/$tableName ];then 49 | exitflag=1 50 | else 51 | echo "No such Table found" 52 | echo -n "Enter another table name: " 53 | read tableName 54 | fi 55 | done 56 | } 57 | 58 | function listTableContents(){ 59 | echo "####################################" 60 | for val in `cat dbs/$connectDbName/$tableName`;do 61 | echo $val 62 | done 63 | echo "####################################" 64 | } 65 | -------------------------------------------------------------------------------- /listdbs.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/bash 2 | 3 | clear 4 | index=1 5 | echo "#################################" 6 | for db in `ls dbs/` 7 | do 8 | echo $index - $db 9 | index=$[$index + 1] 10 | done 11 | echo "#################################" 12 | printMainMenu -------------------------------------------------------------------------------- /main.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/bash 2 | #checks if kdialog is installed or not 3 | installFlag=0 4 | if ! command -v kdialog &> /dev/null 5 | then 6 | echo "a key command was not found on your system ,Please Install kdialog and rerun the script" 7 | exit 8 | fi 9 | 10 | 11 | clear 12 | kdialog --menu "Operations Menu" "1" "Create Table" "2" "List Tables" "3" "Drop Table" "4" "Insert into Table" "5" "Select From Table" "6" "Delete From Table" "7" "Update Table" "0" "Exit" > out 13 | 14 | for choice in $(cat out); do 15 | 16 | case $choice in 17 | 1) 18 | . ./createTable.sh;; 19 | 2) 20 | . ./ListTables.sh;; 21 | 3) 22 | . ./DropTable.sh;; 23 | 4) 24 | . ./InsertTable.sh;; 25 | 5) . ./selectFrom.sh;; 26 | 6) . ./deleteFrom.sh;; 27 | 7) . ./updateTable.sh;; 28 | 0) kdialog --sorry "YOU CHOSE CANCEL";; 29 | *) 30 | kdialog --msgbox "Sorry, invalid selection" 31 | esac 32 | done 33 | if [ -f out ];then 34 | rm out 35 | fi -------------------------------------------------------------------------------- /screenshots/Operations.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mahmoodfathy/Bash-Shell-DBMS/7d324c2d8d0119c8aaa3a6304b572b63729e8370/screenshots/Operations.jpeg -------------------------------------------------------------------------------- /screenshots/createTable.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mahmoodfathy/Bash-Shell-DBMS/7d324c2d8d0119c8aaa3a6304b572b63729e8370/screenshots/createTable.jpeg -------------------------------------------------------------------------------- /screenshots/fieldName.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mahmoodfathy/Bash-Shell-DBMS/7d324c2d8d0119c8aaa3a6304b572b63729e8370/screenshots/fieldName.jpeg -------------------------------------------------------------------------------- /screenshots/fieldType.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mahmoodfathy/Bash-Shell-DBMS/7d324c2d8d0119c8aaa3a6304b572b63729e8370/screenshots/fieldType.jpeg -------------------------------------------------------------------------------- /screenshots/insertFields.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mahmoodfathy/Bash-Shell-DBMS/7d324c2d8d0119c8aaa3a6304b572b63729e8370/screenshots/insertFields.jpeg -------------------------------------------------------------------------------- /screenshots/insertFields2.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mahmoodfathy/Bash-Shell-DBMS/7d324c2d8d0119c8aaa3a6304b572b63729e8370/screenshots/insertFields2.jpeg -------------------------------------------------------------------------------- /screenshots/insertTableName.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mahmoodfathy/Bash-Shell-DBMS/7d324c2d8d0119c8aaa3a6304b572b63729e8370/screenshots/insertTableName.jpeg -------------------------------------------------------------------------------- /screenshots/noFields.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mahmoodfathy/Bash-Shell-DBMS/7d324c2d8d0119c8aaa3a6304b572b63729e8370/screenshots/noFields.jpeg -------------------------------------------------------------------------------- /screenshots/primaryKey.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mahmoodfathy/Bash-Shell-DBMS/7d324c2d8d0119c8aaa3a6304b572b63729e8370/screenshots/primaryKey.jpeg -------------------------------------------------------------------------------- /selectFrom.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/bash 2 | 3 | getTableName 4 | 5 | echo "Please Enter row's primary key that you want to select" 6 | echo -n "Primary Key: " 7 | read pk 8 | 9 | fieldNum=`awk -F',' -v pk="$pk" '{if($1==pk) print NR}' dbs/$connectDbName/$tableName` 10 | exitflag=0 11 | 12 | while [ $exitflag -eq 0 ];do 13 | if [ -z $fieldNum ];then 14 | echo "No such primary key found" 15 | echo -n "Please enter a valid primay key: " 16 | read pk 17 | fieldNum=`awk -F',' -v pk="$pk" '{if($1==pk) print NF}' dbs/$connectDbName/$tableName` 18 | else 19 | exitflag=1 20 | fi 21 | done 22 | 23 | echo "##########################" 24 | awk -F',' -v pk="$pk" '{if($1==pk) print}' dbs/$connectDbName/$tableName 25 | echo "##########################" -------------------------------------------------------------------------------- /updateTable.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/bash 2 | typeset -i toNum 3 | getTableName #helper function to get table name 4 | 5 | listTableContents #helper function to list table rows 6 | 7 | echo "Please Enter row's primary key that you want to update" 8 | echo -n "Primary Key: " 9 | read pk 10 | 11 | fieldNum=`awk -F',' -v pk="$pk" '{if($1==pk) print NR}' dbs/$connectDbName/$tableName` 12 | exitflag=0 13 | 14 | while [ $exitflag -eq 0 ];do #loop until he enters a valid primary key 15 | if [ -z $fieldNum ];then #if fields number = 0 then this is invalid primary key 16 | echo -n "Please enter a valid primary key: " 17 | read pk 18 | #if primary ket exists the following line will get the number of fields of that row 19 | fieldNum=`awk -F',' -v pk="$pk" '{if($1==pk) print NF}' dbs/$connectDbName/$tableName` 20 | else 21 | exitflag=1 22 | fi 23 | done 24 | 25 | #the next line gets the row number of that primary key 26 | rowNum=`awk -F',' -v fn="$pk" '{if($1==fn) print NR}' dbs/$connectDbName/$tableName` 27 | 28 | if [ $rowNum -gt 0 ];then #only excute when the row number exists 29 | 30 | exitflag=0 31 | while [ $exitflag -eq 0 ];do #loop to make sure he entered a valid field number 32 | echo -n "Please Enter a valid field number: " 33 | read fieldNumber 34 | #the following lines gets the number of fields of the specified row 35 | fieldCount=`awk -F',' -v rn="$rowNum" '{if(NR==rn) print NF}' dbs/$connectDbName/$tableName` 36 | num=0 37 | num=$(($num+$fieldNumber)) 38 | 39 | #num is used to convert the fieldNumber value to integer if he entered a string instead of number 40 | #the following if check if the fieldNumber input is less than the actual number of fields 41 | #[ $num -gt 0 ] this means he didn't input a string or input a 0 42 | if [ $num -le $fieldCount ] && [ $num -gt 0 ];then 43 | break 44 | fi 45 | done 46 | 47 | typeField=$[ $fieldNumber+1 ] #this variable holds the line number of the field data type 48 | 49 | #type variable will hold the data type 50 | type=`awk -F',' -v ty="$typeField" '{if(NR==ty) print $2}' dbs/$connectDbName/$tableName.types` 51 | echo -n "Please Enter the new value: " 52 | read new #read the new value to update with 53 | 54 | exitflag=0 55 | if [ $fieldNumber -eq 1 ];then #if he wants to update the primary key 56 | 57 | idExist=`awk -F',' -v id="$new" '{if($1==id) print}' dbs/$connectDbName/$tableName` 58 | 59 | correct=`checkDataType $type $new` #check new value is right data type 60 | 61 | while [ $correct = "false" ];do #loop to make data type is correct 62 | echo "Date type for field $fieldNumber is $type" 63 | echo -n "Please enter a valid data type: " 64 | read new 65 | correct=`checkDataType $type $new` 66 | done 67 | 68 | while [ $exitflag -eq 0 ];do #loop to make sure the id is unique 69 | if [ -z $idExist ];then #this if will happen if new value is unique 70 | exitflag=1 71 | 72 | else 73 | echo -n "Please Enter a unique primary key: " 74 | read new 75 | #idExist holds the value to check if primary key is unique 76 | idExist=`awk -F',' -v id="$new" '{if($1==id) print}' dbs/$connectDbName/$tableName` 77 | fi 78 | done 79 | 80 | #update the table and save it 81 | awk -F',' -v col="${fieldNumber}" -v id="${pk}" -v val=$new '{if($1==id){$col=val}{print $0}}' FS=, OFS=, dbs/$connectDbName/$tableName > dbs/$connectDbName/transitionFile 82 | cat dbs/$connectDbName/transitionFile > dbs/$connectDbName/$tableName 83 | 84 | else 85 | correct=`checkDataType $type $new` #check new value is right data type 86 | 87 | while [ $correct = "false" ];do #loop to make data type is correct 88 | echo "Date type for field $fieldNumber is $type" 89 | echo -n "Please enter a valid data type: " 90 | read new 91 | correct=`checkDataType $type $new` 92 | done 93 | fi 94 | 95 | #update the table and save it 96 | awk -F',' -v col="${fieldNumber}" -v id="${pk}" -v val=$new '{if($1==id){$col=val}{print $0}}' FS=, OFS=, dbs/$connectDbName/$tableName > dbs/$connectDbName/transitionFile 97 | cat dbs/$connectDbName/transitionFile > dbs/$connectDbName/$tableName 98 | fi 99 | 100 | #remove the transitionFile 101 | rm dbs/$connectDbName/transitionFile 102 | echo "Row updated successfully" --------------------------------------------------------------------------------