例如,我有一个像这样的HTML文件:
a.htm
<body>
Hello world!
</body>
我想:
a.htm
<html>
<LINK href='style.css' rel=stylesheet type='text/css'>
<body>
Hello world!
</body>
</html>
我到目前为止的代码是:
#!/bin/sh
for i in `ls *.htm`
do
@echo off
echo ***New top line*** > temp.txt
type $i >> temp.txt
echo ***New bottom line*** >> temp.txt
mv temp.txt $i
done
错误:
abc@bunny:~/fileAppendText$ ./loopAllFilesTest.sh
./loopAllFilesTest.sh: line 5: @echo: command not found
./loopAllFilesTest.sh: line 7: type: i: not found
./loopAllFilesTest.sh: line 9: move: command not found
./loopAllFilesTest.sh: line 5: @echo: command not found
./loopAllFilesTest.sh: line 7: type: i: not found
./loopAllFilesTest.sh: line 9: move: command not found
./loopAllFilesTest.sh: line 5: @echo: command not found
./loopAllFilesTest.sh: line 7: type: i: not found
./loopAllFilesTest.sh: line 9: move: command not found
请帮忙。谢谢!
答案 0 :(得分:1)
s="<html>\n<LINK href='style.css' rel=stylesheet type='text/css'>"
for file in *.htm *.html
do
sed -i.bak "1i $s" "$file"
done
或只是一行sed
sed -i.bak "1i $s" *.html *.htm
如果您在Windows上执行此操作,则可以下载windows version of sed from GNU
答案 1 :(得分:1)
在Unix中,有cat
命令可以执行您想要的操作。创建文件“header.txt”和文件“footer.txt”。然后做:
for i in *.htm; do cat header.txt $i footer.txt > new-$i; done
如果您需要检查正确性,或者在同一个for循环内,您可以替换旧文件:
for i in *.htm; do mv new-$i $i; done
答案 2 :(得分:0)
如果通过shell脚本编写,则表示bash:
命令为echo
,而不是@echo
。 @ form特定于Makefile,不显示命令本身。此外,bash中不存在@echo off
并且无用。
使用i
访问$i
变量的值。