Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions tasks/module_4/task-2/data/users.db
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
baer,baer
temp,temp
asd,qweq
qweqweqwe,qweqweqwe
20 changes: 20 additions & 0 deletions tasks/module_4/task-2/scripts/db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

source "./helpers/help_db.sh"
source "./helpers/add_user_db.sh"
source "./helpers/backup_db.sh"
source "./helpers/restore_db.sh"
source "./helpers/list_users_db.sh"
source "./helpers/find_user_db.sh"

DB_FILE_PATH="../data/users.db"
USERNAME_OR_INVERSE_FLAG=$2;

case $1 in
add) add "$DB_FILE_PATH" ;;
backup) backup "$DB_FILE_PATH" ;;
restore) restore "$DB_FILE_PATH" ;;
find) find "$DB_FILE_PATH" "$USERNAME_OR_INVERSE_FLAG" ;;
list) list "$DB_FILE_PATH" "$USERNAME_OR_INVERSE_FLAG" ;;
help | '' | *) help ;;
esac
25 changes: 25 additions & 0 deletions tasks/module_4/task-2/scripts/helpers/add_user_db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# add_user_db.sh

add() {
local _DB_FILE_PATH="$1"
while true; do
read -p "Type the username (latin letters only) of a new entity: " _username
if [[ "$_username" =~ ^[a-zA-Z]+$ ]]; then
break
else
echo "Incorrect input, latin letters only"
exit 1
fi
done
while true; do
read -p "Type a role (latin letters only): " _role
if [[ "$_role" =~ ^[a-zA-Z]+$ ]]; then
break
else
echo "Incorrect iput, litin letters only"
exit 1
fi
done
echo "$_username,$_role" >> "$_DB_FILE_PATH"
echo "Data saved to $_DB_FILE_PATH"
}
9 changes: 9 additions & 0 deletions tasks/module_4/task-2/scripts/helpers/backup_db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# backup_db.sh

backup() {
local DB_FILE_PATH="$1"
local current_date=$(date +'%Y-%m-%d')
local backup_file="../data/$current_date-users.db.backup"
cp "$DB_FILE_PATH" "$backup_file"
echo "Backup created as $backup_file"
}
22 changes: 22 additions & 0 deletions tasks/module_4/task-2/scripts/helpers/find_user_db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#find_user_db.sh

function find {
local DB_FILE_PATH="$1"
if [ -n "$2" ]; then
local USERNAME_TO_FIND="$2"
if [ -f "$DB_FILE_PATH" ]; then
found_entries=$(awk -v username="$USERNAME_TO_FIND" -F',' '$1 == username' "$DB_FILE_PATH")
if [ -n "$found_entries" ]; then
echo "$found_entries"
else
echo "User not found."
fi
else
echo "Database file not found."
exit 1
fi
else
echo "Usage: db.sh find <username>"
exit 1
fi
}
32 changes: 32 additions & 0 deletions tasks/module_4/task-2/scripts/helpers/help_db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
function help {
echo "Manages users in db. It accepts a single parameter with a command name."
echo
echo "Syntax: db.sh [command]"
echo
echo "List of available commands:"
echo
echo "add Adds a new line to the users.db. Script must prompt user to type a
username of new entity. After entering username, user must be prompted to
type a role."
echo "backup Creates a new file, named" $filePath".backup which is a copy of
current" $fileName
echo "restore Restores the database from a previously created backup. If a backup file
with the current date exists, it will be used to overwrite the current
database. If no backup file is found, the script will inform you that no
backup file is available for restoration."
echo "find Prompts user to type a username, then prints username and role if such
exists in users.db. If there is no user with selected username, script must print:
“User not found”. If there is more than one user with such username, print all
found entries."
echo "list Prints the contents of users.db in the format: N. username, role
where N is the line number of an actual record. By default, it lists the records
in ascending order. You can use the additional optional parameter --inverse to
reverse the order and list the records from bottom to top. For example, running
the command "\$ db.sh list --inverse" will return the result as follows:

\`$ db.sh list --inverse\`

10. John, admin
9. Valerie, user
8. Ghost, guest"
}
11 changes: 11 additions & 0 deletions tasks/module_4/task-2/scripts/helpers/list_users_db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#list_users_db.sh

function list () {
local _DB_FILE_PATH="$1"
local _INVERSE_FLAG="$2"
if [ "$_INVERSE_FLAG" == "--inverse" ]; then
awk -F',' '{data[NR] = $1 ", " $2} END {for (i = NR; i >= 1; --i) print i ". " data[i]}' "$_DB_FILE_PATH"
else
awk '{print NR ". " $1 ", " $2}' FS=',' "$_DB_FILE_PATH"
fi
}
13 changes: 13 additions & 0 deletions tasks/module_4/task-2/scripts/helpers/restore_db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# restore_db.sh

restore() {
local _DB_FILE_PATH="$1"
current_date=$(date +'%Y-%m-%d')
backup_file="../data/$current_date-users.db.backup"
if [ -f "$backup_file" ]; then
cp "$backup_file" "$_DB_FILE_PATH"
echo "Restored from $backup_file"
else
echo "No backup file found for restoration."
fi
}