我的作业要求是: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