LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-20-2009, 12:58 AM   #1
stephenhogan
LQ Newbie
 
Registered: Apr 2009
Posts: 2

Rep: Reputation: 0
Post 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!
 
Old 04-20-2009, 01:25 AM   #2
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
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
 
Old 04-20-2009, 07:16 AM   #3
stephenhogan
LQ Newbie
 
Registered: Apr 2009
Posts: 2

Original Poster
Rep: Reputation: 0
Post 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
 
Old 04-20-2009, 10:20 AM   #4
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
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"
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
I am new to scripting PKrishna Linux - Newbie 7 03-17-2008 09:39 PM
Scripting Help chefsride Linux - Newbie 3 11-06-2006 01:52 PM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM
Scripting CICA Programming 5 10-04-2005 04:54 AM
Looking for scripting Help steve_f60 Linux - General 3 03-22-2005 12:45 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration