In: Computer Science
Write a bash script file that tests each file entry in the
current directory. It should determine if it is a file or
directory. If it is a file, it will determine if it is readable or
not. If it is readable, it will display only the first 4 lines to
the terminal in sorted order (just sort the first 4 lines).
If it is a directory, it should display the contents of that
directory (the files and subdirectories).
NOTE: Please don’t use the read command or arguments with your
script. Note that you don’t need to use either one of these methods
to get a list of files in the current directory.
=====================================
Example Output
Note that the output
from running the script starts at
the sixth line
=====================================
$ ls -l
total 32
drwxrwxr-x 3 cocofan cocofan 4096 Jan 20 17:58 adir
--w-rw-rw- 1 cocofan cocofan 128 Mar 15 22:43 lines.dat
-rw-rw-rw- 1 cocofan cocofan 48 Sep 5 2016 months.txt
$ bash assg5.sh
ENTRY IS adir
**This is a directory**
afileinadir.txt subdirofadir
ENTRY IS lines.dat
**This is a file**
...file is not readable.
ENTRY IS months.txt
**This is a file**
...and it's readable.
...it's first four lines in sorted order are:
Apr
Feb
Jan
Mar
$
++++++++++++++++++++++++++++++++++++
Question:
Write a bash script file that tests each file entry in the
current directory. It should determine if it is a file or
directory. If it is a file, it will determine if it is readable or
not. If it is readable, it will display only the first 4 lines to
the terminal in sorted order (just sort the first 4 lines).
If it is a directory, it should display the contents of that
directory (the files and subdirectories).
NOTE: Please don’t use the read command or arguments with your
script. Note that you don’t need to use either one of these methods
to get a list of files in the current directory.
=====================================
Example Output
Note that the output
from running the script starts at
the sixth line
=====================================
$ ls -l
total 32
drwxrwxr-x 3 cocofan cocofan 4096 Jan 20 17:58 adir
--w-rw-rw- 1 cocofan cocofan 128 Mar 15 22:43 lines.dat
-rw-rw-rw- 1 cocofan cocofan 48 Sep 5 2016 months.txt
Answer:
As per Problem Statement:
1. Script Need to Validate the Passed Name is File or Directory
2. If File Check the Readable Permissions :
**If Not Readable then just print Not Readable
**If Readable then print the contents of the file in console screen
As per the above logic below is the bash script code
===========================
$ cat find_info.sh
#!/bin/bash
NAME=$1
if [ -d "${NAME}" ] ; then
echo "${NAME} is Valid and it is a directory";
else
if [ -f "${NAME}" ]; then
echo "${NAME} is Valid and it is a file";
if [ -r "${NAME}" ]; then
echo "${NAME} is readable file and below are the file
contents";
cat $NAME
else
echo "${NAME} is not a readable file";
exit 1
fi
else
echo "${NAME} is EMPTY its not valid and it is not a Directory nor
a File";
exit 1
fi
fi
===================
Execute the 'find_info.sh' cant be executed at once.
Permissions to be changed to 'executable' to run the script.
$ chmod +x find_info.sh
Now below are the sample output
1. if no name is passed on then
$ ./find_info.sh
is EMPTY its not valid and it is not a Directory nor a File
2. if passed name is directory
$ ./find_info.sh adir
adir is Valid and it is a directory
3. if passed name is file and not readable THEN
$ ./find_info.sh lines.dat
lines.dat is Valid and it is a file
lines.dat is not a readable file
3. if passed name is file and readable THEN
$ ./find_info.sh months.txt
months.txt is Valid and it is a file
months.txt is readable file and below are the file contents
Apr
Feb
Jan
Mar
===================