LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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, 01: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, 09:16 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
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, 09: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, 07:51 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,348

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
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 05:43 PM
How to redirect the output of exec command output in perl? sudhap85 Linux - Newbie 3 08-28-2009 03:35 PM
Perl: Running Command line apps in background and capturing output s0l1dsnak3123 Programming 8 03-28-2008 01:24 PM
Perl - how to capture output of a command with more than one args new_2_unix Linux - Newbie 2 11-29-2007 10:55 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:59 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
Open Source Consulting | Domain Registration