Solved

PERL (file exists y or n)

Posted on 2006-11-10
5
1,217 Views
Last Modified: 2012-06-22
I am new to PERL and I think this should be a simple one... I want to convert this script to PERL:

grep -s $x /etc/security/user
if [[ $? = 1 ]]
then
SANS="No"
else
SANS="Yes"
fi

So can I start with something like this:

if (-e "/etc/security/user"){
    print "Yes \n";
    } else {
    print "No \n";
    }
0
Comment
Question by:bebo_c
  • 2
5 Comments
 
LVL 43

Expert Comment

by:ravenpl
ID: 17916297
$file = "etc/security/user";
( -f $file ) && print "Yes\n" || print "No\n";
0
 
LVL 43

Accepted Solution

by:
ravenpl earned 125 total points
ID: 17916317
But in fact Your code does little more

$ok = 0;
$x = "pattern to match";
$file = "etc/security/user";
if (open(F, $f))
{
 while(<F>)
 {
  if (/$x/)
  {
    $ok = 1;
    last;
  }
 }
 close(F);
}
print ($ok ? "Yes\n" : "No\n");
0
 
LVL 8

Expert Comment

by:koppcha
ID: 17916888
$x="searchPattern";
open F, '</etc/security/user' or die "Can't open the file:$!";
if ( grep/$x/, <F> ) {
print "Yes";
}
else {
print "No";
}
close(F);
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Perl count the hash for print 4 161
Perl for loop for 2000 ms 7 95
PERL variable conundrum 9 89
Perl string filter 5 72
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now