My assignment asks that:
Create a directory
~/UnixCourse/scriptAsst. Turn the two-line version,
above, of the substitution commands into a shell script,subst1taking
three 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,
`~/UnixCourse/scriptAsst/subst1 foo bar myFile.txt`should replace all occurrences of
fooin the filemyFile.txtbybar, leaving the
original file asmyFile.txt.bak.Similarly,
`~/UnixCourse/scriptAsst/subst1 abc "" aardvark.dat`should remove (replace by the empty string) all occurrences of
abcin the fileaardvark.datwith nothing, leaving the original file asaardvark.dat.bak.
My code that I came up with is:
#!/bin/bash
set p1 = "$1"
shift
set p2 = "$1"
shift
set p3 = "$*"
echo $p1
echo $p2
echo $p3
if grep "$p1" "$p3" > /dev/null; then
mv "$p3" "$p3.bak"
sed "s/$p1/$p2/g" "$p3.bak" > "$p3"
fi
When I try to run:
./subst1 foo bar myFile.txt
I keep getting:
grep: : No such file or directory
Please help!! What am I doing wrong??
or use the parameters directly…