Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

shellscript to Perl

by kluther (Novice)
on Mar 15, 2012 at 12:07 UTC ( [id://959775]=perlquestion: print w/replies, xml ) Need Help??

kluther has asked for the wisdom of the Perl Monks concerning the following question:

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
when I do this in perl like this:
if (`/usr/well561/bin/well.newlogfile 1` eq Y) { print "true"; } else { print "false"; }
I get an error about using Bareword "Y" in strict mode, when I take the strict mode out I don't get the right result. How should I rewrite the above in Perl. I've tried several things but they don't work, mostly an error on Bareword "Y". Advise please Thanx Kluther

Replies are listed 'Best First'.
Re: shellscript to Perl
by LanX (Saint) on Mar 15, 2012 at 12:24 UTC
    Enclose the Y into quotes 'Y', to transform a bareword into a string-literal.

    Cheers Rolf

    PS: please enclose your code in <code>...</code> tags.

Re: shellscript to Perl
by Anonymous Monk on Mar 15, 2012 at 12:27 UTC
Re: shellscript to Perl
by eyepopslikeamosquito (Archbishop) on Mar 15, 2012 at 21:47 UTC

    Also, note that shell backticks remove the trailing newline, while Perl backticks preserve it.

    Given you are just starting with Perl, it is a good idea to sprinkle print statements and error checks in your script to help you understand what is going on. To get you started, try this:

    use strict; use warnings; my $cmd = '/usr/well561/bin/well.newlogfile'; -f $cmd or die "error: '$cmd' not found"; -x $cmd or die "error: '$cmd' not executable"; my $out = `$cmd 1`; my $rc = $? >> 8; print "command exit code is $rc\n"; $rc == 0 or warn "warning: command returned $rc\n"; chomp $out; # remove trailing new line from command output print "command output:$out:\n"; if ($out eq 'Y') { print "command returned Y\n"; }
    Study the script above, referring to the Perl documentation, and you should be on your way to rewriting your old shell scripts in Perl. As for learning Perl, take a look at learn.perl.org and Perl Tutorial Hub. Good luck!

Re: shellscript to Perl
by osbosb (Monk) on Mar 15, 2012 at 12:26 UTC
    http://perldoc.perl.org/functions/system.html

    http://perldoc.perl.org/perlop.html#Quote-Like-Operators <-- read about qx.

Re: shellscript to Perl
by muppetjones (Novice) on Mar 16, 2012 at 19:11 UTC

    Perhaps I'm misinterpreting your post, but is "Y" a variable or a character?
    The compiler is complaining because it doesn't know how to handle 'Y' -- you need to either use it as a variable, i.e., $Y or surround it by quotes, i.e., 'Y'.

    As a side note, it is generally slightly faster to use single quotes rather than interpolating a variable or special character, e.g,
    'Hello world.' vs. "Hello world",
    'The number is '.$num vs "The number is $num", or
    'Hello world.'."\n" vs "Hello world\n".

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://959775]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-04-18 06:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found