Using two terminals (one as a text editor and one to test script) 1) Create an a
ID: 3589620 • Letter: U
Question
Using two terminals (one as a text editor and one to test script)
1) Create an array that contains five usernames
2) Create a for loop that prints each username to the console
3) Create another for loop that adds each user to the system
4) Demonstrate that the users have been added to the system. This script will be named Test_Script3_a.sh (any output will be saved as Test_Script3_a_Output.txt)
5) Copy the file that was just created, name it Test_Script3_b.sh (use the cp command)
6) Delete the users from your system
7a) Demonstrate that the users are no longer on the system
7b) Demonstrate their user directories are gone. Name this script Test_Script3_b.sh, and any output save as Test_Script3_b_Output.txt
Explanation / Answer
1.
declare -a arr=("user1" "user2" "user3" "user4" "user5")
2.
for i in "${arr[@]}"
do
echo "$i"
done
3.
for i in "${arr[@]}"
do
useradd "$i"
done
4.
#Test_Script3a.sh
for i in "${arr[@]}"
do
getent passwd "$i" > /dev/null 2&>1
if [ $? -eq 0 ]; then
echo "yes," $i " exists">>Test_Script3a_Output.txt
else
echo "No," $i "does not exist">>Test_Script3a_Output.txt
fi
done
5.
cp Test_Script3a.sh Test_Script3b.sh
6.
for i in "${arr[@]}"
do
userdel "$i"
done
7.a.
for i in "${arr[@]}"
do
getent passwd "$i" > /dev/null 2&>1
if [ $? -eq 0 ]; then
echo "yes," $i " exists"
else
echo "No," $i "does not exist"
fi
done
7.b.
#Test_Script3b.sh
for i in "${arr[@]}"
do
[ -d "/home/$i" ] && echo "Directory for user: $i exists.">>Test_Script3b_Outpt.txt || echo "Directory for user: $i does not exists.">>Test_Script3b_Outpt.txt
done
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.