Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

UNIX. Please help, very cnfused on what to do for this. Objective • Learn to wri

ID: 3765918 • Letter: U

Question

UNIX. Please help, very cnfused on what to do for this.

Objective • Learn to write a more advanced bash shell script that analyzes information about the contents of directories Requirements • For this assignment you must use your knowledge of shell scripting to try to find bad poetry files that are hidden in the current directory • All of the usual requirement for bash scripts apply (the magic line). Write your script in a readable, understandable way so that the grader can give you points. The grader must be able to test your script by running it in a directory • In this assignment, you must write a shell script that counts the number of files that are possibly bad poetry in the current directory. One problem is that whoever is writing this terrible poetry is now hiding the poetry in files with harmless names, so we cannot check filenames to try to find these files. What we know about bad poetry is (So you will need to check each file to see if all three of these things are true about it) • Bad poetry files are readable (and the file must exist of course) • Bad poetry files are writeable • Bad poetry files are not executable (they aren’t scripts) You must print a message showing the name of each suspected bad poetry file • Determine the total number of suspected bad poetry files in the current directory and print a message which displays the number • Determine the total number of items in the directory and print a message which displays the number • So we can see which of these undesirable files is the newest, list all the files in the current directory in long listing format, and sorted by modification time (newest first) Other Requirements • Your script need not check the contents of sub-directories. Only check the contents of the directories which are provided as command line arguments to your script. • Do not analyze hidden files or hidden directories, such as .. (the parent directory) or files beginning with the dot character ( . ). • Your printed output must match the examples below (some exact names will be different depending on the files in the directory of course) • The script you write should be named a4_lastname_firstname.sh using your last and first name, where for example the file of Bill Smith would be named a4_smith_bill.sh. • The script should print all of its results to standard output (do not not redirect to any file)

Sample Output from the script: use this to see how your output should look >

./a4_smith_bill.sh

possible bad poetry: bp.txt possible bad poetry: c

possible bad poetry: d

possible bad poetry: harmless.txt

possible bad poetry: hiddenbp

possible bad poetry: somebp.txt

Number of possible bad poetry files in current directory: 6

Number of files in current directory : 9

list of files in this directory by modification time (newest first): total 36

-rwxr--r-- 1 me users 1326 Nov 16 18:42 abadscript.sh

-rwxr--r-- 1 me users 1326 Nov 16 18:42 simplescript.sh

-rwxr--r-- 1 me users 1326 Nov 16 18:40 a4_smith_bill.sh

-rw-r--r-- 1 me users 28 Nov 16 18:39 hiddenbp

-rw-r--r-- 1 me users 14 Nov 14 12:04 bp.txt

-rw-r--r-- 1 me users 14 Nov 14 11:46 somebp.txt

-rw-r--r-- 1 me users 14 Nov 14 11:45 harmless.txt

-rw-r--r-- 1 me users 16 Nov 13 17:43 d

-rw-r--r-- 1 me users 21 Nov 13 17:43 c

Explanation / Answer

Program to implement the shell script that counts the number of files that are possibly in the current directory.

step1 : Command ls -l displays the number of files and directories that are there in the current directory.We can identify from the sticky bit which one belongs to file and which one belong to directory by the first bit as - or d in the group of 10 bits of sticky bits

Example to identify list of files and directory in the current directory : total 21

-rwxr--r-- 1 root root 9438 Oct 26 18:42 count.sh

drwxr--r-- 1 root root 1433 Oct 26 18:45 rit

from the above example we can say count as file and rit as directory

step2 : Command ls -l | wc -l displays the total number of files and directories that are there in the current directory.

step3 : Command ls -l | tail +2 | wc -l displays the total number of files and directories that are there in the current directory

step4 : Command NUMFILE= $(ls -l | tail +2 | wc -l) displays the total number of files and directories that are there in the current directory

step5 : Command echo $NUMFILE displays the same number that are displayed in step 4

step6 : Command cd bin is used to create a directory with a name bin.In this directory let us create a shell script by using vi or gedit editors with a name count.sh

Program :

echo -e "which directory to count "

read DIR

NUMFILES= $(ls -l $DIR | tail +2 | wc -l)

echo -e "You have $NUMFILE which in $DIR "

NUMORD= $(ls -l $DIR | grep -c ^-)

NUMDIR= $(ls -l $DIR | grep -c ^d)

echo -e "You have $NUMORD files which in directory $DIR "

echo -e "You have $NUMDIR directories within $DIR "