Week 11: Assignment - Getting User Input & Conditional Statements
This commit is contained in:
parent
e62fc1f4bd
commit
49dc6e805f
|
@ -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_1[32;1m[0m2_Week11_1[0m » cec-[K[Kck[K[K[Khecki[K[K[K[K[K[K./checking.sh
|
||||||
|
[1;31m███████████████████████████████████████████████████████████ ERROR ███[0m
|
||||||
|
[1;31m█ █[0m
|
||||||
|
[1;31m█ Invalid argument, please your username █[0m
|
||||||
|
[1;31m█ █[0m
|
||||||
|
[1;31m█████████████████████████████████████████████████████████████████████[0m
|
||||||
|
]2; /media/llewellyn/Hub/CHAMPLAIN/C/2_Week11_1[32;1m[0m2_Week11_1[0m » ./checking.sh llewelly
|
||||||
|
|
||||||
|
[0;36m█████████████████████████████████████████████████████████ CHECKING ███[0m
|
||||||
|
[1;31mNo such user on our system[0m
|
||||||
|
[0;36m██████████████████████████████████████████████████████████████ BYE ███[0m
|
||||||
|
|
||||||
|
]2; /media/llewellyn/Hub/CHAMPLAIN/C/2_Week11_1[32;1m[0m2_Week11_1[0m » ./checking.sh llewellyn
|
||||||
|
|
||||||
|
[0;36m█████████████████████████████████████████████████████████ CHECKING ███[0m
|
||||||
|
[1;33mFound "llewellyn" in the /etc/passwd file.[0m
|
||||||
|
[0;36m██████████████████████████████████████████████████████████████ BYE ███[0m
|
||||||
|
|
||||||
|
]2; /media/llewellyn/Hub/CHAMPLAIN/C/2_Week11_1[32;1m[0m2_Week11_1[0m » exit
|
||||||
|
exit
|
||||||
|
|
||||||
|
Script done on 2020-11-17 02:03:37+02:00 [COMMAND_EXIT_CODE="0"]
|
|
@ -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
|
||||||
|
|
|
@ -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_1[32;1m[0m2_Week11_1[0m » ./nosy.sh [5Pclear./nosy.sh
|
||||||
|
|
||||||
|
[1;36m█████████████████████████████████████████████████████████████ NOSY ███[0m
|
||||||
|
[0;33mPlease enter your full name.[0m
|
||||||
|
[0;33mNAME FORMAT ==> [first last middle][0m
|
||||||
|
[ENTER]: Llewellyn van der MEr erwe
|
||||||
|
[1;32mLlewellyn, Welcome to Chamlplain Week 11 nosy bash script.[0m
|
||||||
|
[0;33mWhat is your login name: [0m
|
||||||
|
[ENTER]: llewellyn
|
||||||
|
[1;32mYour user ID is: 1000[0m
|
||||||
|
[1;32mYour home directory is: /home/llewellyn[0m
|
||||||
|
[1;36m██████████████████████████████████████████████████████████████████████[0m
|
||||||
|
[1;36m█ Llewellyn currently have these running processes[0m
|
||||||
|
[1;36m██████████████████████████████████████████████████████████████████████[0m
|
||||||
|
PID TTY STAT TIME COMMAND
|
||||||
|
2713 ? Rsl 16:08 /usr/bin/gnome-shell
|
||||||
|
51756 pts/2 R+ 0:00 ps r -u llewellyn
|
||||||
|
[1;36m██████████████████████████████████████████████████████████████████████[0m
|
||||||
|
The day of the week is Tuesday and the current time is 2:02:57 am CAT.
|
||||||
|
[1;36m██████████████████████████████████████████████████████████████ BYE ███[0m
|
||||||
|
|
||||||
|
]2; /media/llewellyn/Hub/CHAMPLAIN/C/2_Week11_1[32;1m[0m2_Week11_1[0m » exit
|
||||||
|
exit
|
||||||
|
|
||||||
|
Script done on 2020-11-17 02:03:03+02:00 [COMMAND_EXIT_CODE="0"]
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue