Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple file test question 1

Status
Not open for further replies.

nfaber

Technical User
Oct 22, 2001
446
US
I have the following program:

#!/opt/perl/bin/perl

$file="/home/nfaber/test.txt";

if ( -e $file) {

print ( -s $file);

}

else {

print "$file does not exist";

}

The program returns the number of bytes on the command line. What I want to do is store the number of bytes in a variable that I can compare to another variable passed via the @ARGV.

Any help is appriciated.

Thanks
 
Sorry, I got it by replacing the print statement with a $var1 =


Please excuse me, I have not perl coded in a while and I am very rusty!!!

[blush]
 
Try this:
[tt]
#!/usr/bin/perl -w
use strict;

my $compare_size = $ARGV[0];
my $file_size;
my $file = "./test.pl";

if ( -e $file) {
$file_size = -s $file;
if ($file_size < $compare_size) {
print &quot;File $file is &quot; . ($compare_size - $file_size) . &quot; bytes smaller than the size given\n&quot;;
}
elsif ($file_size > $compare_size) {
print &quot;File $file is &quot; . ($file_size - $compare_size) . &quot; bytes bigger than the size given\n&quot;;
}
else {
print &quot;snap\n&quot;;
}
}
else {
print &quot;file does not exist\n&quot;;
}
[/tt]

usage:

./test.pl <size>

Will.

will@hellacool.co.uk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top