Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PERL (file exists y or n)

Posted on 2006-11-10
5
Medium Priority
?
1,257 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Six Sigma Control Plans

721 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