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

So I've been trying to do this with the O'Reilly Learning Perl, but it is seemingly too basic. I've tried hacking it together with my own limited knowledge and am hitting the wall. I'm trying to take this bash script and convert it to perl:
#!/bin/bash service=`echo $1 | sed -n "s/.*Service:\(.*\)State.*/\1/p"` state=`echo $1 | sed -n "s/.*State:\(.*\)/\1/p"` company=`echo $1 | sed -n "s/.*Service:\(.*\)/\1/p" | cut -c 1-6` echo $company echo $state echo $service if [ "$state" = "down" ]; then echo $service $company | telalertc -g LoadBalancer -m "web server is n +ot responding at $service" -check "$2 status" -host 172.16.3.104 -tag +s $company else echo $state $service $company | telalertc -g LoadBalancer -can +celnc "$2 status" -host 172.16.3.104 -tags $company fi

Edit: davorg - added code tags

Replies are listed 'Best First'.
Re: Converting bash script to perl
by gellyfish (Monsignor) on Jun 28, 2006 at 16:12 UTC
Re: Converting bash script to perl
by marto (Cardinal) on Jun 28, 2006 at 16:12 UTC
Re: Converting bash script to perl
by ptum (Priest) on Jun 28, 2006 at 16:06 UTC

    Hi, mlsrar. What have you tried, and which wall are you hitting (hopefully not Larry)? We'll be much more eager to help you when we see that you've put some effort into it. :)


    No good deed goes unpunished. -- (attributed to) Oscar Wilde
Re: Converting bash script to perl
by Moron (Curate) on Jun 28, 2006 at 16:22 UTC
    Some hints:

    Perl variable names ('scalars') actually begin with the dollar sign whereas in bash the dollar is used an operator to dereference the variable.

    The print command should replace the bash 'echo'.

    sed-like operators can be used directly perl without spawning a sed process.

    Instead of direct use of a pipe to a program, use the open command (the documentation also refers to multichannel piping) to spawn the process and generate filehandles to read or write to the pipe.

    -M

    Free your mind

Re: Converting bash script to perl
by shmem (Chancellor) on Jun 28, 2006 at 16:08 UTC
    Please correct your post and set your script inside <code></code> tags. Thanks ;-)

    update - done by davorg; now waiting for OPs update

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Converting bash script to perl
by mikasue (Friar) on Jun 28, 2006 at 16:51 UTC
    mlsrar, Here's an attempt to translate the first part.
    #!/usr/bin/perl use strict; use warnings; my $input = $ARGV[0]; my ($service, $state) = $input =~ /Service:(.*)State:(.*)/s; my $company = substr $service, 0, 6; print <<EOF; Service: $service State: $state CompanY: $company EOF

    You can look into using system for the last part.
    You should still post your attempt to translate in perl.

Re: Converting bash script to perl
by jesuashok (Curate) on Jun 28, 2006 at 16:20 UTC
    Hi

    Since you have specified larry's name here I will give Larry's answer for this Question.

    Larry says in Perl Faq

    Larry's standard answer for this is to send your script to me (Tom Christiansen) with appropriate supplications and offerings. :-( That's because there's no automatic machine translator. Even if you were, you wouldn't gain a lot, as most of the external programs would still get called. It's the same problem as blind translation into C: you're still apt to be bogged down by exec()s. You have to analyze the dataflow and algorithm and rethink it for optimal speedup. It's not uncommon to see one, two, or even three orders of magnitude of speed difference between the brute-force and the recoded approaches.

    "Keep pouring your ideas"
A reply falls below the community's threshold of quality. You may see it by logging in.
A reply falls below the community's threshold of quality. You may see it by logging in.