我的作業要求是:Shell腳本錯誤
Create a directory
~/UnixCourse/scriptAsst
. Turn the two-line version, above, of the substitution commands into a shell script,subst1
taking 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
foo
in the filemyFile.txt
bybar
, leaving the original file asmyFile.txt.bak
.Similarly,
`~/UnixCourse/scriptAsst/subst1 abc "" aardvark.dat`
should remove (replace by the empty string) all occurrences of
abc
in the fileaardvark.dat
with nothing, leaving the original file asaardvark.dat.bak
.
,我來到了我的代碼是:
#!/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
當我嘗試運行:
./subst1 foo bar myFile.txt
我不斷收到:
grep: : No such file or directory
請幫忙!!我究竟做錯了什麼??
腳本打印什麼'echo $ p1' ...? – DVK
其完全空白 – Sam
您是否複製並粘貼*完全*?這個錯誤看起來像是在你的'grep'命令後面有個''''。 – Kevin