LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-15-2012, 02:55 AM   #1
kluther
LQ Newbie
 
Registered: Mar 2012
Posts: 4

Rep: Reputation: Disabled
check output of command in perl


Hello all,

I've an existing shell-script, but I would like to rewrite it in Perl. I'm new to Perl and now I stumble on something. In my shell-script I have the following line:
if [ a`/usr/well561/bin/newlogfile 1` = aY ]; then
echo "true"
else
echo "false"
fi

How should I rewrite the above in Perl. I've tried several things but the don't work, mostly an error on Bareword "Y".

Advise please

Thanx Kluther
 
Old 03-15-2012, 10:16 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
I haven't seen the syntax that you have in the 'if' command before (namely the 'a' before the command, and the 'Y'). What does it do?

Is the code snippet meant to check the output of the newlogfile command, and see if it returns 'Y'?

If so, the following code should work:

Code:
if( `/usr/well561/bin/newlogfile 1` eq "Y\n"){
    print "True\n";
} else {
    print "False\n";
}
(or if newlogfile doesn't output a newline as well, then change "Y\n" to "Y")

Hope this helps,
 
Old 03-15-2012, 10:28 AM   #3
kluther
LQ Newbie
 
Registered: Mar 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thank you Snark1994,

this is it. I also saw what went wrong. The program /usr/well561/bin/newlogfile returns a newline.

Thank you very much for your help!

Kluther
 
Old 03-15-2012, 08:51 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Another way would be
Code:
$result = `/usr/well561/bin/newlogfile 1`;
chomp($result);
if( $result eq 'Y' )
{
   code....
}
In general, get returned 'stuff', then tidy it up, then check what's left.
It's more robust.
Good Perl docs etc
http://perldoc.perl.org/
http://www.perlmonks.org/?node=Tutorials
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Assigning Output of a Command to an Array in Perl devUnix Programming 10 11-27-2010 06:43 PM
How to redirect the output of exec command output in perl? sudhap85 Linux - Newbie 3 08-28-2009 04:35 PM
Perl: Running Command line apps in background and capturing output s0l1dsnak3123 Programming 8 03-28-2008 02:24 PM
Perl - how to capture output of a command with more than one args new_2_unix Linux - Newbie 2 11-29-2007 11:55 PM


All times are GMT -5. The time now is 10:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration