In: Computer Science
Please write shell scripts in Kali Linux to complete the following tasks.
Task A - Take your UIN
Write a script like below that reads and displays the MIDAS ID if the following requirements are satisfied:
Only lower case letter [a-z] and numeric character[0-9] are allowed
MIDAS ID must between 4 to 8 digits
Test your script with the following examples:
Your MIDAS ID in lower case
Your MIDAS ID w.one upper case
A string less than 4 digits
A string longer than 8 digits
Task B - Check files
Write a script like below that gets a list of files in a directory you entered, plus your name. Your script should pop out an error if an invalid directory is entered.
You need to test the following directories with your script:
/etc/systemd
/home
A directory that does not exist
The folowing ode is given below
Code:
#!/bin/bash
echo Enter your ODU MIDAS ID:
while true
do
read id
LEN=$(echo ${#id})
if ((LEN < 4 || LEN > 8)); then
echo "You must enter a valid midaS ID - four to eight digits only!"
echo "Please re-enter yout MIDAS ID:"
continue
fi
if [[ "${id}" =~ [^a-z0-9] ]]; then
echo "You must enter a valid midaS ID - four to eight digits only!"
continue
fi
echo "Thank you, your MIDAS ID is: "$id
break
done
Output Screenshot
TASK B:
#!/bin/bash
echo Enter the name of the directory you want to check:
read dir
echo Enter your name:
read name
out=$(ls $dir -1a 2> /dev/null)
if [ ${#out} == 0 ]; then
echo Sorry, $name, $dir is not a valid directory name!
else echo "Hello $name!" && echo "Below are the contents in the requested directory: $dir" && ls $dir -1a 2> /dev/null
fi