use strict;
use warnings;
use Getopt::Std;
#shift(@ARGV);
my %opt=();
getopts("f:t:s:u:p:", \%opt);
print "ZAXML-TXT\n";
my $Filepfad;
my $Volumetype;
my $Username;
my $Password;
my $Searchpattern;
my ($opt_file, $opt_type, $opt_user, $opt_passwd, $opt_search);
($opt_file, $opt_type, $opt_user, $opt_passwd, $opt_search) = ($opt{f}
+, $opt{t}, $opt{u}, $opt{p}, $opt{s});
print "$opt{f}\n";
if ($opt_file) {
$Filepfad = $opt_file;
}
else {
exit_with_msg( "A File-Path must be given", 1 );
}
if ($opt_type) {
if ( $opt_type ne "UNC" && $opt_type ne "local" && $opt_type ne "N
+FS" && $opt_type ne "SMB" ) {
exit_with_msg( "Filesystem-type must be UNC, local, NFS or SMB
+", 1 );
}
elsif ( $opt_type eq "SMB" ) {
$Volumetype = $opt_type;
if ( $opt_user && $opt_passwd ) {
$Username = $opt_user;
$Password = $opt_passwd;
}
else {
exit_with_msg( "Username and Password are required for acc
+ess to SMB-Shares", 1 );
}
}
else {
$Volumetype = $opt_type;
}
}
else {
exit_with_msg( "Volumetype must be given", 1 );
}
if ($opt_user) {
$Username = $opt_user;
}
if ($opt_passwd) {
$Password = $opt_passwd;
}
if ($opt_search) {
$Searchpattern = $opt_search;
}
else {
exit_with_msg( "A Searchpattern must be given", 1 );
}
my $os = $^O;
my $last_slash;
if ( $os eq "MSWin32" && $Volumetype eq "local" ) {
$last_slash = rindex( $Filepfad, "\\", );
}
else {
$last_slash = rindex( $Filepfad, "/", );
}
my $Test_Volume = substr( $Filepfad, 0, $last_slash );
print "Test_Volume: $Test_Volume\n";
if ( !-d $Test_Volume ) {
exit_with_msg( "Volume does not exist, isn't mounted or can't be r
+ead", 2 );
}
if ( -f $Filepfad ) {
if ( !unlink($Filepfad) ) {
exit_with_msg( "Can't delete existing File $Filepfad", 2 );
}
}
open my $Filehandle, '>', $Filepfad
or exit_with_msg( "Can't create new file ${Filepfad} for write acc
+ess", 2 );
print {$Filehandle} "Test\n"
or exit_with_msg( "Couldn't write into File", 2 );
close $Filehandle
or exit_with_msg( "Can't close file ${Filepfad} after writing", 2
+);
open $Filehandle, '<', $Filepfad
or exit_with_msg( "Can't create new file ${Filepfad} for read acce
+ss", 2 );
while ( my $Content = <$Filehandle> ) {
chomp $Content;
if ( $Content eq $Searchpattern ) {
exit_with_msg( "Files can be written and read", 3 );
}
else {
exit_with_msg( "File can be written but not read or file-conte
+nt does not match Searchpattern", 4 );
}
} #end while
1;
sub exit_with_msg {
my ( $message, $returnid ) = @_;
print "CURRVALUE_STRING: $message\n";
print "RETURNID: $returnid\n";
exit 0;
}
I also want to mention that I personally do not like Getopt::Std, I would prefer Getopt::Long. This is a core module, so you are able to use it without installing any package from CPAN.
Switching to Getopt::Long is left as an exercise for the OP.
Also I included the new sub exit_with_msg inspired by haukex. |