Register
It is currently Thu Oct 30, 2014 6:14 am

function to check file size?


All times are UTC - 6 hours


Post new topic Reply to topic  [ 9 posts ] 
Author Message
 PostPosted: Sun Jun 04, 2006 8:01 am   

Joined: Fri Oct 14, 2005 9:30 am
Posts: 61
I would like something like:
Code:
if [[ file != "100mb" ]]
then
       do something
else
       do something
fi

Thanks Alot,
Rob.


Top
 Profile WWW  
 PostPosted: Mon Jun 05, 2006 7:37 am   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 669
Location: Des Moines, Iowa
I have a file named field.jpg on my system..... it's 633K
Code:
#!/bin/bash
compare_file_sizes ()
{
# 2 variables below....
matchsize="633K"
FILENAME="field.jpg"

du -h0 ${FILENAME} > /tmp/comparefilesize.txt
filesize=`cut -f 1 /tmp/comparefilesize.txt`
rm -f /tmp/comparefilesize.txt

if [ "$filesize" == "$matchsize" ]
then
       #do something
       echo "file size is a match "
else
      # do something
       echo "file size is ${filesize} and does not match"
fi
}

compare_file_sizes

exit


Top
 Profile WWW  
 PostPosted: Mon Jun 05, 2006 11:15 am   

Joined: Fri Oct 14, 2005 9:30 am
Posts: 61
Cheers crouse.
Could you annotate each command for me so I know what it does (apart from the obvious ones) becuase I do not know half of them.
Cheers,
Rob.


Top
 Profile WWW  
 PostPosted: Wed Jun 07, 2006 10:45 am   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 669
Location: Des Moines, Iowa
#!/bin/bash You know what this is.... ;)

compare_file_sizes () Naming the function
{ Starting the function
# 2 variables below.... Just a comment
matchsize="633K" Setting a variable
FILENAME="field.jpg" Setting another variable

du -h0 ${FILENAME} > /tmp/comparefilesize.txt du is "disk usage" the -h is for "human readable" and the 0 is for ending the line with a null instead of a newline character
filesize=`cut -f 1 /tmp/comparefilesize.txt`setting filesize variable with a command. "cut" sorts through a text file... -f 1 means field one
rm -f /tmp/comparefilesize.txt "rm" removes -f "forces removal" of the temp file

if [ "$filesize" == "$matchsize" ]this is the start of the if/then statement the "==" means equal too
then
#do something
echo "file size is a match "
else
# do something
echo "file size is ${filesize} and does not match"
fi ending the if/then statement
}Ending the function

compare_file_sizes This starts the function

exitThis exits the program


Top
 Profile WWW  
 PostPosted: Mon Jun 12, 2006 12:06 pm   

Joined: Fri Oct 14, 2005 9:30 am
Posts: 61
Ok cheers :), I did say not to point out the obvious ones but hey whatever.


Top
 Profile WWW  
 PostPosted: Mon Jun 12, 2006 3:18 pm   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
not all readers would think they are obvious though :wink:


Top
 Profile  
 PostPosted: Thu Jun 15, 2006 11:56 am   

Joined: Fri Oct 14, 2005 9:30 am
Posts: 61
true :D


Top
 Profile WWW  
 PostPosted: Thu Jun 15, 2006 8:38 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 669
Location: Des Moines, Iowa
crouse smacks crudball....

and i was to know what was obvious to you how :P :lol: :lol:

Hope it helped..... looks like your moving right along the bash learning curve..... your gonna pass me up !!! :)


Top
 Profile WWW  
 PostPosted: Sat Jun 17, 2006 2:45 am   

Joined: Fri Oct 14, 2005 9:30 am
Posts: 61
hey chill out crouse :lol: . I dont think I'll overtake you for a while yet but I will keep at it...


Top
 Profile WWW  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP