 |
 |
filesize in a shell script?
|
 |
|
 |
|
Mac Elite
Join Date: Aug 2001
Location: Madison, WI
Status:
Offline
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|