Attempting to write 2 scripts:
One script called 'totalimg' extracts the img src from a html file and works out the sizes of the images:
Code:
#!/bin/bash
# USAGE: Add up total size of all embedded images in this HTML file.
# Test for command-line usage.
if test -z $1; then
echo "USAGE: totalimg file.html"
echo -e "\n"
exit
fi
# To add-up a cumulative total of (image) file sizes.
TOTALSIZE=0
# Extract path and filename of images, loop for each image link found
for FILENAME in `grep -i "<.*img" $1 | grep "src.*=" | sed 's|^.*src.*="||' | sed 's|">.*$||'`
do
# Test that the file exists
if [ -f $FILENAME ]; then
# If the files exists, then calculate the size and add to the total
FILESIZE=$(stat -c%s "$FILENAME")
#printf "%15d %s\n" $FILESIZE $FILENAME
TOTALSIZE=`expr $TOTALSIZE + $FILESIZE`
fi
done
#echo -e "\n"
echo $TOTALSIZE
#echo -e "\n"
Another called 'totalimgs' starts at the current directory and works thru all subdirectories, to calculate each html file's img filesizes:
Code:
#!/bin/bash
# USAGE: Add up total size of all embedded images in HTML files from this directory
# to all subdirectories.
# Test for command-line usage.
if test $1; then
echo "USAGE: totalimgs"
echo -e "\n"
exit
fi
# Recursively find every file from this directory through all subdirectories.
for FILENAME in `find . -type f -name '*html'`
do
# Test that the file exists
if [ -f $FILENAME ]; then
# If the files exists, call totalimg to calculate the size
printf "%15d %s\n" `totalimg $FILENAME` $FILENAME
fi
done
The problem I have is that totslimgs works for the current dir and all its html files, but the subdirs' html files all come up as zero.
Would like to know why?
So far I have written these and tested them myself... confused why $1 is not allowing totalimg to calculate the size....
Any help greatly appreciated!