LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Scripting (https://www.linuxquestions.org/questions/linux-newbie-8/scripting-720328/)

stephenhogan 04-20-2009 12:58 AM

Scripting
 
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!

Disillusionist 04-20-2009 01:25 AM

I have not tested, but I suspect that the working directory is not being passed and therefore it isn't finding the .img files

You may want to try using dirname and basename to cut the html filename into it's respective components so that you know what directory to start looking for the .img files.

PS: Welcome to LQ ;)

stephenhogan 04-20-2009 07:16 AM

Scripting
 
Thank you Disillusionist!

I have tried what you said with acquiring the full pathname:

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 FILEPATH in `find . -type f -name '*html' -print`

do

FILENAME=`echo $(cd "$(dirname "$FILEPATH")"; /bin/pwd)/$(basename $FILEPATH)`

#echo $FILENAME

  # 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

However, this still does not want to work for me.

I think it may be a problem with the grep -i ... $1 in totalimg... but I am not sure.

And thank you for welcoming me :)

Disillusionist 04-20-2009 10:20 AM

Try editing totalimg:
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

###
### Directory Name
###
l_directory=$(dirname $1)


# 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

  ###
  ### Filename with path_to_file
  ###
  l_file=${l_directory}/${FILENAME}

  # Test that the file exists

  if [ -f $l_file ]; then
 
    # If the files exists, then calculate the size and add to the total

    FILESIZE=$(stat -c%s "$l_file")
    #printf "%15d  %s\n" $FILESIZE $l_file
    TOTALSIZE=`expr $TOTALSIZE + $FILESIZE`
 
  fi

done

#echo -e "\n"
echo $TOTALSIZE
#echo -e "\n"



All times are GMT -5. The time now is 01:26 AM.