Hi all experts,
I am new to Perl. Could you please help me to convert the following SHELL script to PERL ? I will appreciate.
#!/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
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
#!/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 ;
}
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:
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