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

hi, Write a Unix system command called sub. The purpose of sub is to read from a

ID: 3663123 • Letter: H

Question

hi,

Write a Unix system command called sub. The purpose of sub is to read from a text file and either display the contents to the screen or copy them to another file -- replacing one character with another in the output or the target file. The source file remains unmodified. If the target file does not exist, it will be created. Any existing target file will be overwritten. If the source file does not exist, an appropriate error message is displayed. If the incorrect number of arguments is provided when calling the command, an appropriate usage message is displayed, such as:

sub char1 char2 source [target]

where char1 is the character to be changed from the source file source, and char2 is the character to change to. source is the name of the source text file. target is an optional destination file.

Sample Usages:

./sub a z file.txt displays the contents from file.txt to the screen, with all the a's changed to z's.

./sub a z file.txt output.txt copies the contents from file.txt to output.txt, replacing all a's with z's in output.txt.

./sub a z displays usage message

You must use Unix system calls wherever possible: open, close, read, write, creat, etc.

Hints:

Consider writing an algorithm for the program first, and then implement the algorithm one step at a time.

You may find it useful to write your own print function for processing this buffer.

You can test for the null terminator with an expression such as: ( x != '' )

The source text file remains unmodified no matter what.

Explanation / Answer

1) displays the contents from file.txt to the screen, with all the a's changed to z's.

$nano file.txt

$vi file.txt

echo "this is a sample file."

$tr 'a' 'z' <file.txt

this is z szmple file

2)

Copying and moving files

When copying files from one directory to another you need to know for certain which directory is the current working directory. If you are not sure, use the pwd command.

Make a copy in the current directory

$touch output.txt

$cp file.txt output.txt

3)

In computer programming, a usage message or help message refers to a brief message displayed by a program that utilizes a command-line interface for execution. This message usually consists of the correct command line usage for the program and includes a list of the correct command-line arguments or options acceptable to said program.

Usage messages are utilized as a quick way for a program to inform the user of proper command syntax, and should not be substituted for detailed documentation, such as aman page.

ex:

Explanation of the code: