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

Hello Experts,

I am new to scripting and need a shell script changed to Perl. Below is the script, can someone please help me out in converting this to PERL.

#!/bin/sh sudo /usr/openv/netbackup/bin/admincmd/bpimagelist -A -media -hoursago + 24 |sed 's/\|/ /'|awk '{print $1}' > /home/h150371a/file.tmp cat /home/h150371a/file.tmp | while read Image do sudo /usr/openv/netbackup/bin/admincmd/nbemmcmd -listmedia -mediaid $I +mage | grep -iE 'Media ID|Data Expiration' >> file1.tmp done if [ -s file1.tmp ] then mailx -s "Daily" ishonbackup.nar@capgemini.com </home/h150371a/ +file1.tmp fi rm /home/h150371a/file1.tmp
-MP

Replies are listed 'Best First'.
Re: Shell script to Perl
by Mr. Muskrat (Canon) on Mar 01, 2016 at 19:42 UTC

    I am new to scripting and need a shell script changed to Perl.

    Why?

    You're running several scripts via sudo and manipulating the output. I would argue that it is fine as is.

      I agree. There's a time and place for Perl. This is not it.

      My personal philosophy is, try it first as a shell script. If I find that I have to do anything more than operate over a list of things (or find myself wanting an array), move over to Perl.

Re: Shell script to Perl
by FreeBeerReekingMonk (Deacon) on Mar 01, 2016 at 20:23 UTC
    Reconstructed SHELL program:

    #!/bin/sh TEMPFILE0=/home/h150371a/file.tmp TEMPFILE1=/home/h150371a/file1.tmp sudo /usr/openv/netbackup/bin/admincmd/bpimagelist -A -media -hoursago + 24 |sed ' s/\|/ /'|awk '{print $1}' > $TEMPFILE0 cat $TEMPFILE0 | while read Image do sudo /usr/openv/netbackup/bin/admincmd/nbemmcmd -listmedia -me +diaid $Image | grep -iE 'Media ID|Data Expiration' >> $TEMPFILE1 done if [ -s "$TEMPFILE1" ] then mailx -s "Daily" ishonbackup.nar@capgemini.com < $TEMPFILE1 fi rm $TEMPFILE1 -MP # rm $TEMPFILE0

    The only way I see this becoming faster in perl is if you do not write the files to disk, at expense of more RAM usage during execution.

    NetWallah already did a good job of presenting you sample code... except you would get multiple emails instead of a single one... might want to replace that.

    maybe this (also untested)

    my @MEDIA; for (qx<sudo /usr/openv/netbackup/bin/admincmd/bpimagelist -A -media - +hoursago 24>){ s/\|/ /; my ($Image) = split; for (qx<sudo /usr/openv/netbackup/bin/admincmd/nbemmcmd -listmedia - +mediaid $Image>){ next unless m/Media ID|Data Expiration/i; push @MEDIA, $_; } } if(@MEDIA){ open(FF, "|/usr/bin/mailx -s 'Daily' ishonbackup.nar\@capgemini.com" +) or die $!; for (@MEDIA){ print FF $_ . "\n"; } close FF; }
      If the mailx is not working, check if you are allowed to send email outside. Some mailservers only allow root to go outside the intranet domain (and normal users not).
      ls > ls.txt mailx -s "testemail" ishonbackup.nar@capgemini.com < ls.txt
Re: Shell script to Perl
by NetWallah (Canon) on Mar 01, 2016 at 19:21 UTC
    Something along these lines (Untested):
    for (qx<sudo /usr/openv/netbackup/bin/admincmd/bpimagelist -A -media - +hoursago 24>){ s/\|/ /; my ($Image) = split; for (qx<sudo /usr/openv/netbackup/bin/admincmd/nbemmcmd -listmedia - +mediaid $Image>){ next unless m/Media ID|Data Expiration/i; qx<echo \"$_\" | mailx -s "Daily" ishonbackup.nar@capgemini.com>; } }

            "Think of how stupid the average person is, and realize half of them are stupider than that." - George Carlin

Re: Shell script to Perl
by GotToBTru (Prior) on Mar 01, 2016 at 19:00 UTC

    This is hard to read. The reason you have a preview button is so you can fix that. Please use code tags!

    But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

      #!/usr/bin/perl # #my @MEDIA; #for (qx<sudo /usr/openv/netbackup/bin/admincmd/bpimagelist -A -media +-hoursago 24>){ # s/\|/ /; # my ($Image) = split; # for (qx<sudo /usr/openv/netbackup/bin/admincmd/nbemmcmd -listme +dia -mediaid $Image>){ # next unless m/Media ID|Data Expiration/i; # push @MEDIA, $_; # } # } # # if(@MEDIA){ # open(FF, "|/usr/bin/mailx -s 'Daily' ishonbackup.na +r\@capgemini.com") or die $!; # for (@MEDIA){ # print FF $_ . "\n"; # } # close FF; # }
      I am using the above script but its not giving me any output. Can someone please help me out.
        #!/usr/bin/perl use strict; use warnings; my @MEDIA; for (qx<sudo /usr/openv/netbackup/bin/admincmd/bpimagelist -A -media - +hoursago 24>) { s/\|/ /; my ($Image) = split; for (qx<sudo /usr/openv/netbackup/bin/admincmd/nbemmcmd -listmedia - +mediaid $Image>){ next unless m/Media ID|Data Expiration/i; push @MEDIA, $_; } } if (@MEDIA){ open(FF, "|/usr/bin/mailx -s 'Daily' ishonbackup.nar\@capgemini.com" +) or die $!; for (@MEDIA){ print FF $_ . "\n"; } close FF; }

        What output do you expect from bpimagelist? You might want to run this in the debugger or add print statements to verify it.

        s/\|/ /; ($image) = split; could be more easily written ($image) = split /\|/; although in either case I don't think it returns the value you want. split returns a list, so if you want one of the values you need to use a subscript.

        But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

Re: Shell script to Perl
by kroach (Pilgrim) on Mar 01, 2016 at 19:00 UTC
    Please use code tags for readabilty and post your code.
Re: Shell script to Perl
by FreeBeerReekingMonk (Deacon) on Mar 02, 2016 at 20:08 UTC