Question

In: Computer Science

Write a bash script file that tests each file entry in the current directory. It should...

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
$
++++++++++++++++++++++++++++++++++++

Solutions

Expert Solution

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

===================


Related Solutions

Write a script that prompts the user for a pathname of a file or directory and...
Write a script that prompts the user for a pathname of a file or directory and then responds with a description of the item as a file along with what the user's permissions are with respect to it (read, write, execute).
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
Write a bash script to find all the files ending with .c recursively for every directory...
Write a bash script to find all the files ending with .c recursively for every directory in your current working directory, then copy each one to a folder called programs, need handle duplicates by appending the number to the end of the file (ex main.c main-1.c ) compile them and generate a report report should look like: main.c compiles prog2.c failed main-2.c compiles etc.
Write a bash shell script that takes exactly one argument, a file name. If the number...
Write a bash shell script that takes exactly one argument, a file name. If the number of arguments is more or less than one, print a usage message. If the argument is not a file name, print another message. For the given file, print on the screen its content.
Create a bash script file named assessment-script-a that: 1. Accepts any number of file names on...
Create a bash script file named assessment-script-a that: 1. Accepts any number of file names on the command line 2. For each file name provided, delete any line that contains the string: qwe.rty When the changes are completed, the script should display the total number of files scanned, and the total number of files changed. Example Command: assessment-script-a file.a file.b file.c file.d file.e    Example Output: 5 files scanned, 3 files changed 3. Your script should include a series of...
Parse the dirent struct to see if an entry is a directory or a file. If...
Parse the dirent struct to see if an entry is a directory or a file. If it is a directory, prepend "./" to the filename before printing it out. Include a main that does this below. Please do the above code using C language.
Use Vi text editor or ATOM to create a bash script file. Use the file name...
Use Vi text editor or ATOM to create a bash script file. Use the file name ayaan.sh. The script when ran it will execute the following commands all at once as script. Test your script file make sure it works. Here is the list of actions the script will do: It will create the following directory structure data2/new2/mondaynews off your home directory. inside the mondaynews, create 4 files sports.txt, baseball.txt, file1.txt and file2.txt. Make sure file1.txt and file2.txt are hidden...
Write a program that creates a file called "data.dat" in the current directory. Prompt the user...
Write a program that creates a file called "data.dat" in the current directory. Prompt the user for five numbers, and write them, one at a time, on both the screen and into the file. Close the file, then open it again for reading only, and display the contents on the screen. Handle error conditions that may occur. Please Need this to be done in PERL. Thanks
Fully Functional Script written in BASH In this section, you will write a fully functional script...
Fully Functional Script written in BASH In this section, you will write a fully functional script to help your manager with an important project that will build upon what you learned from your script work in Milestone Four. After you have written the script, you will execute it to generate a report for all three users (once for Bob, once for Henry, and once for Frank). You should have three unique generated reports in your ~/scripts directory (or more if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT