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

home / study / engineering / computer science / questions and answers / TOPIC: S

ID: 3574668 • Letter: H

Question

home / study / engineering / computer science / questions and answers / TOPIC: Shell Scripts I Am Working On A 3 Part Problem ...

Your question has been answered! Rate it below.

Let us know if you got a helpful answer.

Question: TOPIC: Shell Scripts I am working on a 3 part prob...

BookmarkBookmarked

TOPIC: Shell Scripts

I am working on a 3 part problem and I have successfully completed the first 2 parts but can't figure out the third.

Part 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 (replaces all occurences of the word "foo" with "bar" in the given files)

MY subst2 script:

#!/bin/sh

echo $1
echo $2
echo $3

if grep "$1" "$3" > /dev/null; then
mv "$3" "$3.bak"
sed "s/$1/$2/g" "$3.bak" > "$3"
fi

essentially, I need to modify the above subst2 script so that it can handle any number of files that are entered on the command line

Explanation / Answer

#!/bin/sh i=1 first=$1; second=$2; for file do $((i++)) if [ $i -ge 3 ]; then sed -i 's/'$first'/'$second'/g' $file" fi done