Solved

PERL (file exists y or n)

Posted on 2006-11-10
5
1,242 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…

749 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