for file in $(ls -ltr *.html | awk '{ print $9 }')
do
for filedata in $(cat $file)
do
if [ $filedata == "<html>" ]; then
echo "<script>" >> $file
echo "Some .js code here" >> $file
echo "</script>" >> $file
fi
done
done
for file in $(ls -ltr *.html | awk '{ print $9 }')
instead of:
for file in *.html
The second form will not fail due to ARG_MAX limits nor filenames containing whitespace characters; doesn't need to start up a subshell, doesn't need to invoke ls or awk ; and will run faster.
All expansions of $file and $filedata also need to be quoted.
It also seems strange that this code replaces everything in every input file with one copy of the text that was supposed to be added after every occurrence of the word <html> in the corresponding input file instead of adding that text to the existing file in the specified spot(s).
Hi Ferocci,
Is the text to be added contained in a file, or is it just a string in your script?
I don't have time to give a complete proposal tonight, but if I was doing this I'd probably use ed -s "$file" in a loop instead of reading the files line by line in the shell (and I certainly wouldn't use echo to copy the results back to the files being processed.