From 49dc6e805f76f07a0cc05c94cebb87a5bd4428c3 Mon Sep 17 00:00:00 2001 From: Llewellyn van der Merwe Date: Sat, 14 Nov 2020 01:11:31 +0200 Subject: [PATCH] Week 11: Assignment - Getting User Input & Conditional Statements --- week-11/checking.log | 23 +++++++++++++ week-11/checking.sh | 66 +++++++++++++++++++++++++++++++++++++ week-11/nosy.log | 26 +++++++++++++++ week-11/nosy.sh | 77 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 192 insertions(+) create mode 100644 week-11/checking.log create mode 100755 week-11/checking.sh create mode 100644 week-11/nosy.log create mode 100755 week-11/nosy.sh diff --git a/week-11/checking.log b/week-11/checking.log new file mode 100644 index 0000000..a4d71b4 --- /dev/null +++ b/week-11/checking.log @@ -0,0 +1,23 @@ +Script started on 2020-11-17 02:03:10+02:00 [TERM="xterm-256color" TTY="/dev/pts/1" COLUMNS="130" LINES="27"] +]2; /media/llewellyn/Hub/CHAMPLAIN/C/2_Week11_12_Week11_1 » cec-ckhecki./checking.sh +███████████████████████████████████████████████████████████ ERROR ███ +█ █ +█ Invalid argument, please your username █ +█ █ +█████████████████████████████████████████████████████████████████████ +]2; /media/llewellyn/Hub/CHAMPLAIN/C/2_Week11_12_Week11_1 » ./checking.sh llewelly + +█████████████████████████████████████████████████████████ CHECKING ███ +No such user on our system +██████████████████████████████████████████████████████████████ BYE ███ + +]2; /media/llewellyn/Hub/CHAMPLAIN/C/2_Week11_12_Week11_1 » ./checking.sh llewellyn + +█████████████████████████████████████████████████████████ CHECKING ███ +Found "llewellyn" in the /etc/passwd file. +██████████████████████████████████████████████████████████████ BYE ███ + +]2; /media/llewellyn/Hub/CHAMPLAIN/C/2_Week11_12_Week11_1 » exit +exit + +Script done on 2020-11-17 02:03:37+02:00 [COMMAND_EXIT_CODE="0"] diff --git a/week-11/checking.sh b/week-11/checking.sh new file mode 100755 index 0000000..159ec3c --- /dev/null +++ b/week-11/checking.sh @@ -0,0 +1,66 @@ +#!/bin/bash + + + +#█████████████████████████████████████████████████████████████ Color Codes ███ +normorange="\033[0;33m"; +normyellow="\033[1;33m"; +normnormcyan="\033[0;36m"; +lightRed="\033[1;31m"; +NC="\033[0m"; + +#████████████████████████████████████████████████████████ Function - PRINT ███ +function printo { + if [ -z "$2" ]; then + echo -e "$1" + elif [ "$2" == "S" ]; then + echo -e "${normyellow}$1${NC}" + elif [ "$2" == "F" ]; then + echo -e "${lightRed}$1${NC}" + elif [ "$2" == "H1" ]; then + echo -e "${normnormcyan}$1${NC}" + elif [ "$2" == "Q" ]; then + echo -e "${normorange}$1${NC}" + fi +} + +#███████████████████████████████████████████████████████████ Error notice ███ +function showerror() { + printo "███████████████████████████████████████████████████████████ ERROR ███" F + printo "█ █" F + printo "█ Invalid argument, please your username █" F + printo "█ █" F + printo "█████████████████████████████████████████████████████████████████████" F +} +# check that at least one argument is passed +if [ $# -eq 1 ] +then + # add the username + login_name="$1" +else + showerror + exit 1 +fi + +#██████████████████████████████████████████████████████████████ the script ███ +echo +# Ask the user login name +printo "█████████████████████████████████████████████████████████ CHECKING ███" H1 +# make sure we get an input value +while [[ -z $login_name ]] +do + printo "What is your login name: " Q + echo -n " [ENTER]: " + read login_name +done + +# check if this name is found +if grep -q "^$login_name:" /etc/passwd; then + + printo "Found \"$login_name\" in the /etc/passwd file." S +else + printo "No such user on our system" F +fi +printo "██████████████████████████████████████████████████████████████ BYE ███" H1 +echo + diff --git a/week-11/nosy.log b/week-11/nosy.log new file mode 100644 index 0000000..188d8f0 --- /dev/null +++ b/week-11/nosy.log @@ -0,0 +1,26 @@ +Script started on 2020-11-17 02:02:43+02:00 [TERM="xterm-256color" TTY="/dev/pts/1" COLUMNS="130" LINES="27"] +]2; /media/llewellyn/Hub/CHAMPLAIN/C/2_Week11_12_Week11_1 » ./nosy.sh clear./nosy.sh + +█████████████████████████████████████████████████████████████ NOSY ███ +Please enter your full name. +NAME FORMAT ==> [first last middle] + [ENTER]: Llewellyn van der MEr  erwe +Llewellyn, Welcome to Chamlplain Week 11 nosy bash script. +What is your login name:  + [ENTER]: llewellyn +Your user ID is: 1000 +Your home directory is: /home/llewellyn +██████████████████████████████████████████████████████████████████████ +█ Llewellyn currently have these running processes +██████████████████████████████████████████████████████████████████████ + PID TTY STAT TIME COMMAND + 2713 ? Rsl 16:08 /usr/bin/gnome-shell + 51756 pts/2 R+ 0:00 ps r -u llewellyn +██████████████████████████████████████████████████████████████████████ +The day of the week is Tuesday and the current time is 2:02:57 am CAT. +██████████████████████████████████████████████████████████████ BYE ███ + +]2; /media/llewellyn/Hub/CHAMPLAIN/C/2_Week11_12_Week11_1 » exit +exit + +Script done on 2020-11-17 02:03:03+02:00 [COMMAND_EXIT_CODE="0"] diff --git a/week-11/nosy.sh b/week-11/nosy.sh new file mode 100755 index 0000000..3f381d0 --- /dev/null +++ b/week-11/nosy.sh @@ -0,0 +1,77 @@ +#!/bin/bash + +#█████████████████████████████████████████████████████████████ Color Codes ███ +normorange="\033[0;33m"; +lightGreen="\033[1;32m"; +lightCyan="\033[1;36m"; +lightRed="\033[1;31m"; +NC="\033[0m"; + +#████████████████████████████████████████████████████████ Function - PRINT ███ +function printo { + if [ -z "$2" ]; then + echo -e "$1" + elif [ "$2" == "S" ]; then + echo -e "${lightGreen}$1${NC}" + elif [ "$2" == "F" ]; then + echo -e "${lightRed}$1${NC}" + elif [ "$2" == "H1" ]; then + echo -e "${lightCyan}$1${NC}" + elif [ "$2" == "Q" ]; then + echo -e "${normorange}$1${NC}" + fi +} + +#███████████████████████████████████████████████████████████████ First Try ███ +# # Ask the user their first name +# echo -n "Please enter your first name: " +# read -e first_name +# # Ask the user their middle name +# echo -n "Please enter your middle name: "; +# read -e middle_name +# # Ask the user their last name +# echo -n "Please enter your last name: "; +# read -e last_name + +# # greet user by their first name +# echo "$first_name, Welcome to Chamlplain Week 11 nosy bash script." + +#██████████████████████████████████████████████████████████████ Second Try ███ +echo +# Ask the user their first name +printo "█████████████████████████████████████████████████████████████ NOSY ███" H1 +printo "Please enter your full name." Q +printo "NAME FORMAT ==> [first last middle]" Q +echo -n " [ENTER]: " +read -a full_name + +# greet user by their first name +printo "${full_name[0]}, Welcome to Chamlplain Week 11 nosy bash script." S + +# Ask the user login name +printo "What is your login name: " Q +echo -n " [ENTER]: " +read login_name + +# check if this name is found +if grep -q "^$login_name:" /etc/passwd; then + # get this users details from the /etc/passwd file + IFS=':' read -r -a user_details <<< $(grep "^$login_name:" /etc/passwd) + # display the user ID + printo "Your user ID is: ${user_details[2]}" S + # display the user home directory path + printo "Your home directory is: ${user_details[5]}" S + # display user the processes running + printo "██████████████████████████████████████████████████████████████████████" H1 + printo "█ ${full_name[0]} currently have these running processes" H1 + printo "██████████████████████████████████████████████████████████████████████" H1 + ps r -u $login_name + printo "██████████████████████████████████████████████████████████████████████" H1 + # display the day of the week and the time + date +"The day of the week is %A and the current time is %r." +else + printo "This user name was not found in /etc/passwd" F +fi +printo "██████████████████████████████████████████████████████████████ BYE ███" H1 +echo +