diff --git a/tasks/module_4/task_2/data/2023-06-16-12-12-55-users.db.backup b/tasks/module_4/task_2/data/2023-06-16-12-12-55-users.db.backup new file mode 100644 index 0000000..4d84b97 --- /dev/null +++ b/tasks/module_4/task_2/data/2023-06-16-12-12-55-users.db.backup @@ -0,0 +1,2 @@ +Marat, Admin +Ivan, User diff --git a/tasks/module_4/task_2/data/2023-06-16-12-19-31-users.db.backup b/tasks/module_4/task_2/data/2023-06-16-12-19-31-users.db.backup new file mode 100644 index 0000000..cf54407 --- /dev/null +++ b/tasks/module_4/task_2/data/2023-06-16-12-19-31-users.db.backup @@ -0,0 +1,3 @@ +Marat, Admin +Ivan, User +Peter, User diff --git a/tasks/module_4/task_2/data/users.db b/tasks/module_4/task_2/data/users.db new file mode 100644 index 0000000..cf54407 --- /dev/null +++ b/tasks/module_4/task_2/data/users.db @@ -0,0 +1,3 @@ +Marat, Admin +Ivan, User +Peter, User diff --git a/tasks/module_4/task_2/scripts/db.sh b/tasks/module_4/task_2/scripts/db.sh new file mode 100755 index 0000000..76887fe --- /dev/null +++ b/tasks/module_4/task_2/scripts/db.sh @@ -0,0 +1,114 @@ +#!/usr/bin/env bash + +typeset fileName=users.db +typeset fileDir=../data +typeset filePath=$fileDir/$fileName + +# check/create db file +if [[ "$1" != "help" && "$1" != "" && ! -f $filePath ]]; then + read -p "users.db does not exist. Do you want to create it? [Y/n] " answer + answer=${answer,,} + if [[ "$answer" =~ ^(yes|y)$ ]]; then + touch $fileName + echo "File ${fileName} is created." + else + echo "File ${fileName} must be created to continue. Try again." >&2 + exit 1 + fi +fi + +function validateLatinLetters { + if [[ $1 =~ ^[A-Za-z_]+$ ]]; then return 0; else return 1; fi +} + +function add { + read -p "Enter user name: " username + validateLatinLetters "$username" + if [[ "$?" == 1 ]]; then + echo "Name must have only latin letters. Try again." + exit 1 + fi + + read -p "Enter user role: " role + validateLatinLetters "$role" + if [[ "$?" == 1 ]]; then + echo "Role must have only latin letters. Try again." + exit 1 + fi + + echo "${username}, ${role}" | tee -a $filePath +} + +function backup { + backupFileName=$(date +'%Y-%m-%d-%H-%M-%S')-${fileName}.backup + cp $filePath $fileDir/"$backupFileName" + + echo "Backup is created." +} + +function restore { + latestBackupFile=$(find $fileDir -name "*-${fileName}.backup" | sort | tail -n 1) + + echo "${latestBackupFile}" + + if [ ! -f "$latestBackupFile" ]; then + echo "No backup file found." + exit 1 + fi + + cat "$latestBackupFile" >$filePath + + echo "Backup is restored." +} + +function findUser { + read -p "Enter username to search: " username + + awk -F, -v x="$username" '$1 ~ x' ../data/users.db + + # don't work =( + if [[ "$?" == 1 ]]; then + echo "User not found." + exit 1 + fi +} + +inverseParam="$2" +function list { + if [[ $inverseParam == "inverse" ]]; then + cat --number $filePath | tac + else + cat --number $filePath + fi +} + +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 "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 contents of users.db in format: N. username, role + where N - a line number of an actual record + Accepts an additional optional parameter inverse which allows to get + result in an opposite order - from bottom to top" +} + +case $1 in +add) add ;; +backup) backup ;; +restore) restore ;; +find) findUser ;; +list) list ;; +help | '' | *) help ;; +esac