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

2. Write a bash script called minor3B. sh that will print information about the

ID: 3584974 • Letter: 2

Question

2. Write a bash script called minor3B. sh that will print information about the files in the current directory based on the command line arguments (or lack thereof) The syntax is as follows: minor3B. sh [file1] [file2] . [fileN] If no arguments are given, your program will default to all non-hidden files in your The number of ordinary, readable, and executable files in your current . The number of non-existent (if files specified in command line arguments current directory. Your program will report the following statistics: do not exist) or “other" types of files in your current directory The number of directories in your current directory The number of ordinary and readable files in your current directory The total number of bytes (size) contained in ordinary and readable files in your current directory. Please note that no duplication of file counts is allowed (e.g., directories are counted as directories only, but not executable files even though they are current directory and not sub-directories (i.e., files inside directories in the current directories

Explanation / Answer

orecnt=0
neoth=0
dircnt=0
orcnt=0
totbytes=0

if [ $# -eq 0 ] ; then
list_of_files=*
else
list_of_files=$*
fi

for fname in $list_of_files
   do
     if [ -r $fname -a -x $fname -a -w $fname -a -f $fname ] ; then
      orecnt=`expr $orecnt + 1`
     elif [ -d $fname ] ; then
       dircnt=`expr $dircnt + 1`
     elif [ -r $fname -a -w $fname ] ; then
       orcnt=`expr $orcnt + 1`
       fsize=`ls -l $fname | cut -d" " -f5`
       totbytes=`expr $totbytes + $fsize`
     else
       if [ $# -ne 0 ] ; then
          neoth=`expr $neoth + 1`
       fi
     fi
   done
   echo "ordinary, readable, executable files : $orecnt"
   echo "non-existent or other types of files : $neoth "
   echo "directory files                      : $dircnt "
   echo "ordinary and readable files          : $orcnt"
   echo "total bytes in ordinary files        : $totbytes"

<<'COMMENT'
lenovo@lenovo-Vbox:~/chegg/test$ ls -l
total 52
-rwxrwxr-x 1 lenovo lenovo 1541 Oct 2 23:44 calc.sh
-rw-rw-r-- 1 lenovo lenovo 721 Oct 2 23:44 call_data.txt
-rw-rw-r-- 1 lenovo lenovo 239 Oct 2 23:44 err.txt
-rw-rw-r-- 1 lenovo lenovo 2918 Oct 2 23:33 main.c
-rw-rw-r-- 1 lenovo lenovo   63 Oct 2 23:33 makrs
-rw-rw-r-- 1 lenovo lenovo 5781 Oct 2 23:33 menu.c
-rwxrwxr-x 1 lenovo lenovo 886 Oct 2 23:45 minor3B.sh
-rwxrwxr-x 1 lenovo lenovo 905 Oct 2 23:44 minor3B.sh~
-rw-rw-r-- 1 lenovo lenovo   49 Oct 2 23:44 numbers.txt
drwxrwxr-x 2 lenovo lenovo 4096 Oct 2 23:32 temp
drwxrwxr-x 2 lenovo lenovo 4096 Oct 2 23:32 tmp
-rw-rw-r-- 1 lenovo lenovo 1566 Oct 2 23:44 weekly_call_info.txt
lenovo@lenovo-Vbox:~/chegg/test$ ./minor3B.sh tmp makrs minor3B.sh hello xyz
ordinary, readable, executable files : 1
non-existent or other types of files : 2
directory files                      : 1
ordinary and readable files          : 1
total bytes in ordinary files        : 63
lenovo@lenovo-Vbox:~/chegg/test$ ./minor3B.sh
ordinary, readable, executable files : 3
non-existent or other types of files : 0
directory files                      : 2
ordinary and readable files          : 7
total bytes in ordinary files        : 11337
lenovo@lenovo-Vbox:~/chegg/test$

COMMENT

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote