Loop through a set of HTML files and append text at top and bottom of each file
For instance, I have an HTML file like this :
a.htm
<body>
Hello world!
</body>
I want :
a.htm
<html>
<LINK href='style.css' rel=stylesheet type='text/css'> 
<body>
Hello world!
</body>
</html>
The code I have so far is :
#!/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
Errors :
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
Please help. Thanks!
In Unix, there is the cat command for what you want to do. Create a file "header.txt" and a file "footer.txt". Then do:
for i in *.htm; do cat header.txt $i footer.txt > new-$i; done
After you checked for correctness, or inside the same for loop if you wish, you can replace the old files:
for i in *.htm; do mv new-$i $i; done
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
or just one line of sed
 sed -i.bak "1i $s" *.html *.htm
if you are doing it on Windows, you can download the windows version of sed from GNU
If by shell-scripting, you mean bash:
- The command is - echo, not- @echo. The @ form is specific to Makefiles for not displaying command itself. Moreover- @echo offdoesn't exist in bash and is useless.
- The value of - ivariable is accessed with- $i.
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论