Shell to perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell to perl
# 1  
Old 05-25-2011
Shell to perl

Hi all experts,
I am new to Perl. Could you please help me to convert the following SHELL script to PERL ? I will appreciate.
Code:
 
#!/bin/bash
#set -x
fileday=$1
result_file=Total`date +%Y%m%d%H%M`
out_folder=/home/cmd/pm_analyze
tmp_folder=/tmp/pmtmp
datatype='split licenceCounter cdrsReceivedTotSum cdrsCombinedSum cdrsAggregatedSum'
ch_server() {
###### Choosing which server #################
echo "Please Choose Host IP ?"
echo " 1) CGX02 172.17.11.23"
echo " 2) CGX03 172.17.11.24"
echo " 3) CGW03 172.17.12.23"
echo " 4) CGW04 172.17.12.24"
echo " 5) CGT01 172.17.9.51"
read server
if [ "$server" = "1" ]
then
hostip="172.17.11.23"
elif [ "$server" = "2" ]
then
hostip="172.17.11.24"
elif [ "$server" = "3" ]
then
hostip="172.17.12.23"
elif [ "$server" = "4" ]
then
hostip="172.17.12.24"
else
hostip="172.17.9.51"
fi
}
proc_pm() {
for i in $datatype
do
total=0
number=0 
filename=$tmp_folder/$i.log
echo "Now processing $filename" 
while read line
do
number=` echo $line | cut -d '>' -f2 | cut -d '<' -f1 `
let total+=number
done < $filename
echo " the $i total sum = $total" >> $out_folder/$result_file 
done
}
#### Check Output folder exit or not #################
if [ ! -d $out_folder ]
then
echo "#######output folder not exit !! Creating #####"
mkdir -p $out_folder 
if [ ! -d $out_folder ]
then
echo "Creating Fail !! Pleae check"
exit
else
echo "Create Success"
fi
fi
if [ ! -d $tmp_folder ]
then
echo "#######TMP folder not exit !! Creating #####"
mkdir -p $tmp_folder 
if [ ! -d $tmp_folder ]
then
echo "Creating Fail !! Pleae check"
exit
ele
echo "Create Success"
fi
fi
##################Checking tmp folder data ##################
echo "************************ Warring **************************"
echo "*** All the files under temp folder will be deleted. ******"
echo "*** Temp folder is $tmp_folder ******"
echo "************************************************* **********"
read -p "Are you sure continue processing (yes/no):" answer
#read answer
if [ "$answer" == "n" ] || [ "$answer" == "no" ]
then
exit 0;
else
tmpfiles=`ls -al $tmp_folder/*.log | wc -l`
if [ "$tmpfiles" == "0" ]
then
echo "************************************************* **************"
echo " ********* OK, There are no any logs under $tmp_folder ********"
echo "************************************************* **************"
else
rm $tmp_folder/*
tmpfiles1=`ls -al $tmp_folder/*.log | wc -l`
echo $tmpfiles1
if [ "tmpfiles1" == "0" ]
then
echo " ********* OK, All files are deleted ********"
else
echo " ********* There are some logs cannot deleted. Please check it. ********"
exit 0
fi
fi

fi
##### choosing which file type #################
echo "Please Choose Solution ?"
echo " 1) Please Input An filename ?"
echo " 2) Summation Daily Total Counts"
read ftype

if [ "$ftype" == "1" ]
then
echo "Please input filename and file path"
read filepath 
if [ ! -e "$filepath" ]
then
echo "************************************"
echo "*****Shit!! File does not exist****"
echo "************************************"
exit 
fi
for i in $datatype
do
gzcat $filepath |grep $i >> $tmp_folder/$i.log
done
proc_pm;
elif [ $ftype == "2" ]
then
echo " Please input date for processing "
read filedate
ch_server;
echo "Please input path "
read datepath
datefile=`ls -al $datepath/PM.$hostip.$filedate.*.xml.gz | wc -l`
if [ "$datefile" == "0" ]
then
echo "Sorry !! There are no PM files under $datepath"
exit 1
fi
for i in $datatype
do
gzcat $datepath/PM.$hostip.$filedate.*.xml.gz |grep $i >> $tmp_folder/$i.log
done
proc_pm;
else
echo "Sorry!! You don't give me the right command"
fi


Last edited by Scott; 05-26-2011 at 01:45 AM.. Reason: Please use code tags, and indent code
# 2  
Old 05-25-2011
Hi jinanchen,

Welcome to the forum.If you have basic knowledge of perl, you can surely start.
Post your concerns where you stuck. We are here to help.
# 3  
Old 05-25-2011
reply

Quote:
Originally Posted by anchal_khare
Hi jinanchen,

Welcome to the forum.If you have basic knowledge of perl, you can surely start.
Post your concerns where you stuck. We are here to help.
Because i am very new in perl .
I have not learn about perl at all.
I just have learn a little shell script.
As for no sense to solve this critical issue.
I have try to edit the shell by myself.
But it doesn't work in environment.
And i try about two weeks.
And search google about perl teaching web sites.
But the conclusion is very poor.
I have ever to ask my University classmates.
But they talk to me ,they only learn C# & Java.
Never learn about in Perl.
So , i have no idea how to do right now ?

Below is my editted perl script
Code:
#!/usr/bin/perl
$fileday = $ARGV[0];
$result_file="Total" . `date +%Y%m%d%H%M`;
#### Check Output folder exit or not #################
$out_folder="/home/cmd/pm_analyze";
print output folder not exit !! Creating;
mkdir $out_folder,0755 or die;
print "Cannot create $out_folder: $!";
#### Check Temp folder exit or not #################
my$tmp_folder="/tmp/pmtmp";
print TMP folder not exit !! Creating;
mkdir $tmp_folder,0755 or die;
print "Cannot create $tmp_folder: $!";
###### Choosing which server #################
print "Please Choose Host IP  ?"
print " 1) CGX02 172.17.11.23"
print " 2) CGX03 172.17.11.24"
print " 3) CGW03 172.17.12.23"
print " 4) CGW04 172.17.12.24"
print " 5) CGT01 172.17.9.51"
$server = <STDIN>;  chomp $server; 
 if ( $server eq 1 ) {
 hostip="172.17.11.23"
elsif( $server eq 2 ){
 hostip="172.17.11.24"
elsif( $server eq 3 ){
 hostip="172.17.12.23"
elsif( $server eq 4 ){
 hostip="172.17.12.24"
else {
 hostip="172.17.9.51"
}
 
@datatype = ('split','licenceCounter','cdrsReceivedTotSum','cdrsCombinedSum','cdrsAggregatedSum');
    foreach (@datatype) {
         print $i;
         $total = 0;
         $number = 0;
         $filename=$tmp_folder/$i."log";
         print "Now processing $filename \n";
 }
        sum();
        sub sum
        {
                $number=` echo $line |  cut -d '>' -f2 | cut -d '<' -f1 ` ;
                $total += $number ;
        print $total < $filename ;
        print " the $i total sum = $total" >> $out_folder/$result_file ;
}
##################Checking tmp folder data ##################
print "************************ Warring **************************" ;
print "*** All the files under temp folder will be deleted. ******" ;
print "*** Temp folder is $tmp_folder                       ******" ;
print "***********************************************************" ;
print "Are you sure continue processing (yes/no):" ;
chomp ($line = <answer>);
if ($answer == "n" || $answer == "no" ) {
        exit 0;
}
else {
        $tmpfiles = `ls -al $tmp_folder/*.log | wc -l`;
}
        if ( "$tmpfiles" eq "0" ) {
                print "***************************************************************" ;
                print " ********* OK, There are no any logs under $tmp_folder ********" ;
                print "***************************************************************" ;
}
else {
                `rm $tmp_folder/*`;
               $tmpfiles = `ls -al $tmp_folder/*.log | wc -l`;
}
                if ( "tmpfiles" eq "0" )  {
                        print " ********* OK, All files are deleted ********" ;
}
                else {
                        print " ********* There are some logs cannot deleted. Please check it. ********" ;
                        exit 0  ;
}
##### choosing which file type #################
print "Please Choose Solution ? \n" ;
print " 1) Please Input An filename ? \n" ;
print " 2) Summation Daily Total Counts  \n" ;
chomp ($line = <ftype>);
if ("$ftype" == "1") {
        print "Please input filename and file path \n" ;
        chomp ($line = <filepath>);
}
        if ( ! -e "$filepath" ) {
                print "************************************" ;
                print "*****Shit!! File does not exist*****" ;
                print "************************************" ;
                exit 1;
}
@datatype = ('split','licenceCounter','cdrsReceivedTotSum','cdrsCombinedSum','cdrsAggregatedSum');
    foreach (@datatype) {
         print $i;
                `gzcat $filepath | grep $i  >> $tmp_folder/$i."log"`;
}
if ("$ftype" == "2")  {
        print "Please input date for processing. \n";
        chomp ($line = <filedate>);  # ch_server
        print "Please input path. \n";
        chomp ($line = <datepath>);
        $datefile =`ls -al $datepath/PM.$hostip.$filedate.*.xml.gz | wc -l`;
} 
        if ( "$datefile" == "0" ) {
                print "Sorry !! There are no PM files under $datepath" ;
                exit 1 ;
}
@datatype = ('split','licenceCounter','cdrsReceivedTotSum','cdrsCombinedSum','cdrsAggregatedSum');
    foreach (@datatype) {
         print $i;
               `gzcat $datepath/PM.$hostip."$filedate.\*"."xml.gz" |grep $i  >> $tmp_folder/$i."log"`;
}
if  ("$ftype" != "1" ||"$ftype" != "2" ) {
        print "Sorry!! You don't give me the right command" ;
 exit 0 ;
}


Last edited by Scott; 05-26-2011 at 01:46 AM.. Reason: Please use code tags
# 4  
Old 05-27-2011
Hi,

Where is the problem with your 'perl' script? Are there errors or what part of code doesn't work as you expect?

Regards,
Birei
# 5  
Old 05-29-2011
I don't know how to convert below shell session to perl ?

I don't know how to convert below shell session to perl ?
Because some symbol could support by shell , for example '>>'
could use in shell , but could not use in perl.

And number=` echo $line | cut -d '>' -f2 | cut -d '<' -f1 `
let total+=number
done < $filename
could use in shell ,but could not use in perl.


# Count Daily Total History:
Code:
for i in $datatype
do
gzcat $addToPath/PM.$b.$fileday.*.xml.gz |grep $i >> $addToPath/$i.log
done
 
# Summation Daily Total Counts:
for i in $datatype
do
total=0
number=0 
filename=$addToPath/$i.log
while read line
do
number=` echo $line | cut -d '>' -f2 | cut -d '<' -f1 `
let total+=number 
done < $filename
echo " the $i total sum = $total" >> $addToPath/$result_file
cat $addToPath/$result_file
done


Last edited by Franklin52; 05-30-2011 at 05:16 AM.. Reason: Please use code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Converting shell to Perl I run into shell built in function trap and need alternative in Perl

I am working on converting shell to Perl script. In shell we have built in function trap Do you know alternative in Perl or actually we don't need it? Thanks for contribution (3 Replies)
Discussion started by: digioleg54
3 Replies

2. Shell Programming and Scripting

Shell / Perl help

Hi, here is my problem. I have a command line tool to process files. Now I have few hundreds of files to process. So I have written a perl script to generate the command dynamically using variables for the arguments. when I use backtick to execute those it creates error. the shell doesn't get the... (4 Replies)
Discussion started by: jith
4 Replies

3. Shell Programming and Scripting

Shell to PERL

Hi, i must admit that am naive to scripting. I got one shell script created which worked fine on linux. Later i was told that this script is to run on MP-RAS(aka NCR Unix), which is quite an old version of Unix i understand.:wall: in the shell script that i got created, i have made... (6 Replies)
Discussion started by: utkarsh
6 Replies

4. Shell Programming and Scripting

HELP on Perl array / sorting - trying to convert Korn Shell Script to Perl

Hi all, Not sure if this should be in the programming forum, but I believe it will get more response under the Shell Programming and Scripting FORUM. Am trying to write a customized df script in Perl and need some help with regards to using arrays and file handlers. At the moment am... (3 Replies)
Discussion started by: newbie_01
3 Replies

5. Shell Programming and Scripting

Shell or Perl?

Hello, I am looking for some basic script help, I am currently tailing a log with a simple tail –f, combined with egrep –vi to remove some messages from the tail, i do not want to see. . I wanted to create something a little more complex were if it’s possible to script this into... (4 Replies)
Discussion started by: hamon
4 Replies

6. UNIX for Dummies Questions & Answers

perl through shell

Can we call a perl script/ commands through a shell script? (1 Reply)
Discussion started by: chn10db001
1 Replies

7. Shell Programming and Scripting

Perl Vs Shell Programming

Can someone please tell me what the big deal about perl is? i have been doing shell programming for quite a number of years and I have to say, there's very little if any thing that I can't do in shell programming. i just need to investigate how to do it. so, my question is, does deep... (1 Reply)
Discussion started by: SkySmart
1 Replies

8. Shell Programming and Scripting

Perl - pass shell-vars into perl for input loop

I need to process a file line-by-line using some value from a shell variable Something like:perl -p -e 's/$shell_srch/$shell_replace/g' input.txt I can't make the '-s' work in the '-p' or '-n' input loop (or couldn't find a syntaxis.) I have searched and found... (4 Replies)
Discussion started by: alex_5161
4 Replies

9. UNIX for Dummies Questions & Answers

shell to perl

hi all, Can we change a shell script into a perl script instead of writing a fresh one in perl? Is there any utility present that can be used for this purpose? please help me. Thanks in advance.. (1 Reply)
Discussion started by: arthi
1 Replies

10. Shell Programming and Scripting

How to Run a shell script from Perl script in Parent shell?

Hi Perl/UNIX experts, I have a problem in running a shell script from my perl script (auto.pl). I run the perl script using perl auto.pl from the shell prompt The shell script picks the files in "input" folder and procesess it. The shell script blue.sh has this code. export... (16 Replies)
Discussion started by: hifake
16 Replies
Login or Register to Ask a Question