Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solution for Module 4 task 2 #29

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Marat, Admin
Ivan, User
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Marat, Admin
Ivan, User
Peter, User
3 changes: 3 additions & 0 deletions tasks/module_4/task_2/data/users.db
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Marat, Admin
Ivan, User
Peter, User
114 changes: 114 additions & 0 deletions tasks/module_4/task_2/scripts/db.sh
Original file line number Diff line number Diff line change
@@ -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