Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

A Unix program: Shell Assignment 2.1 Stage 1 Create a directory ~/UnixCourse/scriptAsst. Turn the two-line version, above, of the substitution commands into a shell script,

A Unix program: Shell Assignment

2.1 Stage 1

Create a directory ~/UnixCourse/scriptAsst.

Turn the two-line version, above, of the substitution commands into a shell script, subst1 taking three command-line parameters:

the string to be replaced

the string with which to replace it

the name of the file in which to make the substitution

For example,

echo "This is a test." > myFile.txt ~/UnixCourse/scriptAsst/subst1 is are myFile.txt cat myFile.txt 

should print the line:

Thare are a test. 

having replaced all occurrences of is in the file myFile.txt by are, leaving the original file as myFile.txt.bak.

Similarly,

echo "Hello world." > aardvark.dat echo "How are you?" >> aardvark.dat ~/UnixCourse/scriptAsst/subst1 o "" aardvark.dat cat aardvark.dat 

should remove (replace by the empty string) all occurrences of o in the file aardvark.dat, leaving the original file as aardvark.dat.bak. The output of the final statement should be:

Hell wrld. Hw are yu? 

2.2 Stage 2

One problem with this approach is that the files creation and modification dates are altered, even if no change was made to the file contents (because the string being relaced did not occur within the file).

Create a new script, subst2, taking the same three parameters, that performs the substitution and backup file creation when the target string occurs somewhere in the file, but leaves the file completely unchanged (and does not create the .bak file) if the string being replaced is nowhere in the file. (Hint: there are two ways to do this. One is to check first for the presence of the target string and, if it is found, to do the substitution as before. It may be easier to go ahead and do the substitution, and then to check and see if the resulting files are identical.)

2.3 Stage 3

Generalize your subst2 script, producing a new script subst that will apply a substitution to any number of files given on the command line. For example

~/UnixCourse/scriptAsst/subst foo bar myFile1.txt myFile2.txt myFile3.txt 

should apply the same substitution throughout each of the three files named there.

for stage 1. I tried,

#!/bin/sh cp $3 $3.bak sed -i "s/$1/$2/g" $3

that doesn't to work, when i run it says "backup file was not created properly and dates and other attributes have been changed."

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

More Books

Students also viewed these Databases questions

Question

Azure Analytics is a suite made up of which three tools?

Answered: 1 week ago