Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Unix linux Write a shell script mycat which acts like an improved cat program. It has one option: -b, followed by any number of file

Unix linux

Write a shell script mycat which acts like an improved cat program. It has one option: -b, followed by any number of file paths. Here is what it does

it cats each file given to it on the commandline

if the file is not a text file (as identified by the file program), it refuses to cat it. Instead it outputs an error message

If the -b option was given:

the beginning of each file is announced with a banner that includes the file's name and

it outputs the number of files successfully output, after the last file has been cat-ed.

Here are examples of mycat running on two text files, a directory, and an executable. Note that it quietly skips the directory and the executable. (Maybe the design should specify a message?)

$ mycat -b addints tmp a.out getalpha ************************* addints *************************** addints() { # # this function adds a list of integers # given as arguments. It outputs the sum # to standard output # local sum=0 arg for arg; do if ! echo "$arg" | grep -qE '^-?[0-9]+$'; then echo "'$arg' is not an integer - skipped." >&2 continue fi ((sum = sum + arg)) done echo "$sum" } ************************* getalpha *************************** # getalpha getalpha() { # ask the user a question ($1) and get a response that # must contain only alphbetic characters and spaces # (an empty response is not allowed - this is your decision) # getalpha() repeats the question until a valid response is received local input # note that we do not check for an argument. This allows the user # to output their own question and just use our function (with an # empty question) to get the response. while : ; do read -p "$1:" input echo "$input" | grep -qE '^[[:alpha:] ]+$' && break echo \ "Response may consist of alphabetic characters and spaces only.">&2 done echo "$input" return 0 } ***************************************************************** 2 files cat-ed

Here are some design considerations:

the -b option must be first, so it's presence can be checked first. If it is present, set a flag and discard it using shift.

assuming there are still arguments, enter a loop that

checks the current argument. If not readable or it's not a file, just continue.

if the -b option was present, output a banner and count the file

cat the file

There are several problems that are open questions

during the loop you may want to use continue. Remember, you must shifteach time through the loop or your loop will be infinite. Check the solution to see an easy way to handle this.

what should you do if -b does not come first? This is an easy issue - we will do what all standard programs do - treat it as a file. (filenames that start with a dash are difficult to deal with, but the filename is legal)

what should you do if an option is used other than -b? Normally, you would check for this and give an error. Our version will just treat it as a file.

what should you do about non-readable arguments or arguments that are not files? Again, our version will silently skip them. We could alter the design to output a message, perhaps tied to the -b flag.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Programming Languages 12th International Symposium Dbpl 2009 Lyon France August 2009 Proceedings Lncs 5708

Authors: Philippa Gardner ,Floris Geerts

2009th Edition

3642037925, 978-3642037924

More Books

Students also viewed these Databases questions

Question

Introduce and define metals and nonmetals and explain with examples

Answered: 1 week ago

Question

What is IUPAC system? Name organic compounds using IUPAC system.

Answered: 1 week ago

Question

What happens when carbonate and hydrogen react with carbonate?

Answered: 1 week ago

Question

=+With whom does the firm have to negotiate?

Answered: 1 week ago

Question

=+Are there shop stewards?

Answered: 1 week ago