views:

56

answers:

3

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!

A: 

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 off doesn't exist in bash and is useless.

  • The value of i variable is accessed with $i.

mouviciel
+1  A: 
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

ghostdog74
+1  A: 

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
ypnos
Simple awesome! Could you point me to the right source which will make me a a shell script hacker like you?! Thanks again!
ThinkCode
I found that the FLOSS manual at http://en.flossmanuals.net/gnulinux is quite a comprehensive source. while it starts from about zero, all important concepts of the UNIX command line are covered. Also available as PDF: http://en.flossmanuals.net/CommandLineIntro/FM_16Apr09.pdf
ypnos