Question

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...

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?

Solutions

Expert Solution

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>.


Related Solutions

Examine the following shell script and describe its function line-by-line: #! /bin/bash #This script backs up...
Examine the following shell script and describe its function line-by-line: #! /bin/bash #This script backs up the Oracle DB rm -f /SAN/backup-oracle* if tar -zcvf /SAN/backup-oracle- 'date +%F'.tar.gz/oracledb/* then echo "Oracle backup completed on 'date'" >>/var/log/oraclelog else echo "Oracle backup failed on 'date'" >>/var/log/oraclelog mail -s ALERT jason.eckert@trios.com </var/log/oraclelog fi
Examine the following shell script and describe its function line-by-line: #!/bin/bash echo -e "Which file would...
Examine the following shell script and describe its function line-by-line: #!/bin/bash echo -e "Which file would you like to copy> --> \c" echo -e "Which file would you like to copy? --> \c?" read FILENAME mkdir /stuff || echo "The /stuff directory could not be created." && echo "The /stuff directory could not be created." cp -f $FILENAME /stuff && echo "$FILENAME was successfully copied to /stuff"
SQL ONLY. WRITE CLEAR AND SIMPLE ANSWERS. Consider the following relations (PRIMARY KEYS ARE WRITTEN IN...
SQL ONLY. WRITE CLEAR AND SIMPLE ANSWERS. Consider the following relations (PRIMARY KEYS ARE WRITTEN IN BOLD) departments (dept_no, dept_name) dept_emp (emp_no, dept_no, from_date, to_date) dept_manager (dept_no, emp_no, from_date, to_date) employees (emp_no, birth_date, first_name, last_name, gender, hire_date) salaries (emp_no, salary, from_date, to_date) titles(emp_no, title, from_date, to_date) Write the following queries in SQL. No duplicates should be printed in any of the answers. List all the titles for which there is at least one employee having the title. Find the current...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT