Create a directory ~/UnixCourse/scriptAsst. Turn the two-line version, above, of
ID: 3712656 • Letter: C
Question
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,
should print the line:
having replaced all occurrences of “is” in the file myFile.txt by “are”, leaving the original file as myFile.txt.bak.
Similarly,
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:
Explanation / Answer
You can create the directory by using the following command:
cd ~ && mkdir UnixCourse && cd UnixCourse && mkdir scriptAsst
The bashrc which takes 3 argument to convert the data in the file is as follows:
# Code to take argument values and keep it
a=$1
b=$2
c=$3
# Code to read the data from the filename
filedata=$(cat $3)
# Code to replace the data from the string
filedata=${filedata//$a/$b}
# Code to write the modified data back in the file
echo $filedata > $c
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.