Solved

PERL (file exists y or n)

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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Removing file extension within a file. 4 94
How to get all the API from website? 11 92
Perl File::Find alternative 1 70
Export Variables in Perl 3 69
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

831 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