In: Computer Science
Consider the following simple script incorporating a for loop:
#!/bin/bash
RED='\033[0;31m'
NC='\033[0m'
clear; cd /home
for DIR in $HOME; do
#for DIR in *;do
CHK=$(grep -c "/home/$DIR" /etc/passwd)
if [ $CHK -ge 1 ]
then
echo -e "${NC}$DIR is good"
else
echo -e "${RED}ALERT! $DIR is NOT good!"
fi
done
What kind of script is presented above? How did you make your determination? What is the purpose of the script?
In lines #5/6, what is the type of $DIR?
In line #7, what is the value of $CHK?
In line #8, what are the instructions contained with the brackets known as?
Why are there two lines containing the word "for"? Which line will execute? Will that line execute correctly?
What should you do before running the script?
How could the script be factored into your duties as a sysadmin?
The following script is a bash script. "#!bin/bash" represents bash script. "#!" is known as "SHEBANG " Operator and /bin/bash means the bash shell.
The main aim of this script is to gather a list of directories in the /home directory and check if that directory is listed in /etc/passwd
$DIR stands for List of Directory Contents. It has a datatype of Directory Stream.
In line 5 or 6, $CHK stores the number of instances of /home/$DIR in /etc/passwd
In line 8 the if statement checks if the $CHK value is greater than or equal to 1
There are two lines containing for, one is a comment and one is 'for' loop. The statement starting with # is a comment, it does not get executed. It is for the user explanation. And the other one is for loop. Yes, the for loop will execute correctly.
Before running the script, save the file with .sh extension. After that,make the script executable with command chmod +x <fileName>. and then run the script using ./<fileName>.