Write a bash script called min r4B.sh that will print information about the file
ID: 3754573 • Letter: W
Question
Write a bash script called min r4B.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 minor4B.sh [filel] [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 directory do not exist) or "other" types of files in your current directory The number of directories in vour 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 executable, since they are not ordinary files). Also, only count data from the current directory and not sub-directories (i.e., files inside directories in the current directories SAMPLE OUTPUT (user input shown in bold): $ ls -1 total 76 -rwx1 mat0299 mat0299 7285 Aug 22 01:21 a.out -rw---1 mat0299 mat0299 83 Aug 25 08:40 file1 0 Aug 21 19:35 mat2 file -rw---- 1 mat0299 mat0299 84 Aug 21 19:31 mat file -rw--1 mat0299 mat0299 382 Aug 22 01:21 minorl.c -rwx---1 mat0299 mat0299 1464 Sep 8 11:27 minor3.sh -rwx---1 mat0299 mat0299 996 Sep 13 23:19 minor3B.sh -rw---1 mat0299 mat0299 373 Aug 21 19:23 myfile -rw--1 mat0299 mat0299 55 Aug 25 08:48 myfilel -rw--1 mat0299 mat0299 55 Aug 25 08:49 myfile2Explanation / 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$
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.