Solved

PERL (file exists y or n)

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

760 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

19 Experts available now in Live!

Get 1:1 Help Now