Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Mac OS X > filesize in a shell script?

filesize in a shell script?
Thread Tools
Mac Elite
Join Date: Aug 2001
Location: Madison, WI
Status: Offline
Reply With Quote
Jul 24, 2003, 03:59 PM
 
I'm working on my first shell script adventure. My goal is to run chkrootkit every night on a linux server, direct the output to a file and diff that vs last night's output.

That part's working fine. The next part I can't quite phrase so the script does what I want: I need it to check the size of the file, and if it's not zero, email it to me, since a 0 k diff output means the files are the same.- if there's no change, I don't need to be told.

Unless there's a better way to alert me only when the file has changed, could someone advise how to phrase the "if filesize >0, then do mail thing, else forgeddaboudit" logic?
OS X: Where software installation doesn't require wizards with shields.
     
Dedicated MacNNer
Join Date: May 2002
Status: Offline
Reply With Quote
Jul 24, 2003, 06:21 PM
 
man test

test -n returns 0 ( true) if the file
is empty

if test -n filename ; then
non zero size file
fi
     
Mac Elite
Join Date: Aug 2001
Location: Madison, WI
Status: Offline
Reply With Quote
Jul 24, 2003, 09:06 PM
 
Perhaps I don't get something, but it seems to me this should work:

touch zzz
test -n zzz

Shouldn't a 0 show on screen? Or is that not the way it works?
OS X: Where software installation doesn't require wizards with shields.
     
Dedicated MacNNer
Join Date: Jul 2001
Location: NC
Status: Offline
Reply With Quote
Jul 31, 2003, 11:08 PM
 
Originally posted by C.J. Moof:
Perhaps I don't get something, but it seems to me this should work:

touch zzz
test -n zzz

Shouldn't a 0 show on screen? Or is that not the way it works?
No. To begin with function return values don't go STDOUT. However, the return value of the last command can found in the shell variable $?. (at least in a Bourne type shell) Next, -n is a string test operator. The file test operator that tests for non-zero file size is -s. Thus, a command that does what you want is:

test -s zzz; echo $?

I would guess that this works in tcsh but I don't script in C-type shells.
Gary
A computer scientist is someone who, when told to "Go to Hell", sees the
"go to", rather than the destination, as harmful.
     
Mac Enthusiast
Join Date: Nov 2001
Location: Adelaide, South Australia
Status: Offline
Reply With Quote
Aug 1, 2003, 07:00 AM
 
Maybe the zeros and ones are backwards in this discussion (and the option to "test"), or am I missing something? According to my "man test" the -n flag is for *string* tests; you need -s for file size tests.

test -s should return zero if the file exists and is *not* empty.

So the commands:

% touch anewfilename
% test -s anewfilename; echo $?

should produce the output

1

If you throw something into the file

% ls > anewfilename
% test -s anewfilename; echo $?

you should get a zero.

Cheers,
Paul
     
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
Aug 1, 2003, 10:11 AM
 
Another approach is to structure your test differently:

Code:
if [ -s filename ]; then echo "file is nonzero" fi
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
     
   
Thread Tools
Forum Links
Forum 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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 08:22 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2