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

A Unix program: Shell Assignment 2.1 Stage 1 Create a directory ~/UnixCourse/scr

ID: 3571880 • Letter: A

Question

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,

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:

2.2 Stage 2

One problem with this approach is that the file’s 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

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

Explanation / Answer

For the 1st question


if [ "$#" -lt 3 ]
then
echo "usage: a b file1 file2"
exit
fi
#the above if condition makes sure that the parametrs passed are not less than 3

oldword="$1"
newword="$2"

#Assigning the parameters to the respective variable names

cp $3 $3.bak
#taking backup of the file

sed -i "s/$oldword/$newword/g" $3

#using linux utility sed for the replacement

For the 2nd Stage

if [ "$#" -lt 3 ]
then
echo "usage: a b file1 file2"
exit
fi

oldword="$1"
newword="$2"
grep $oldword $3
chk=`echo $?`

#echo $? gives the status of the previous executed command, 0 for successful and 1 for failure.
if [ $chk == "0" ]
then
cp $3 $3.bak
sed -i "s/$oldword/$newword/g" $3
fi

For the 3rd Question

if [ "$#" -lt 3 ]
then
echo "usage: a b file1 file2 ..."
exit
fi
A="$1"
shift

#shift - The positional parameters are shifted to the left by this number, N. The positional parameters from N+1 to $# are renamed to variable names from $1 to $# - N+1.


B="$1"

shift
while [ "$#" -gt 0 ]
do
cp $1 $1.bak
sed -i "s/$A/$B/g" $1
shift
done

Please let me know if you face any issue, will be glad to help. Have a good day.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote