Conversion of Perl Script to Shell Script..


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Conversion of Perl Script to Shell Script..
# 1  
Old 12-05-2014
Conversion of Perl Script to Shell Script..

Hi Guys
I am having a perl script that fetches exclude list from a unix client and I trying it to convert it to shell script but I am having issues please help me...


Code:
#!/usr/bin/perl

use strict; 
use warnings; 
use Getopt::Std; 


# To turn on debuging (i.e. more information) specify -d on the command line 
our $opt_d = 0; 

# To get ONLY the version information specify -v on the command line 
our $opt_v = 0; 

getopts('dv'); 

our $debug = $opt_d; 

my $uname = `uname -n`; 
chomp $uname; 

# Desiginate where to write the in/exclude files (must be fully qualified path) 
our $PWD = $ENV{PWD}; 
our $output_dir = "$PWD/EI_${uname}"; 
if ( ! -d $output_dir ) { mkdir $output_dir } 


# Location of bp.... commands 
our $nbadmin = "/usr/openv/netbackup/bin/admincmd"; 

# Generate a list of policies 
our @policy_list = `$nbadmin/bppllist`; 

# Used the get output of the bpgetconfig command. Only need for debut purposes 
our @status = (); 


foreach my $policy (@policy_list) { 
chomp $policy; 

# Get the individual policy information 
my @policy = `$nbadmin/bppllist $policy -l`; 

# Extract the info line 
my @info = grep /^INFO /, @policy; 

# If the policy type is not standard, ignore 
if ( (split /\s+/, $info[0])[1] != 0 ) { next } 

# If the policy is inactive, ignore 
if ( (split /\s+/, $info[0])[11] != 0 ) { next } 

# Pull out the clients for this policy and then keep only the client names 
my @clients = grep /^CLIENT /, @policy; 
@clients = map { (split /\s+/, $_)[1]} @clients; 

# Pull out the schedules for this policy and then keep only the schedule names 
my @schedules = grep /^SCHED /,@policy; 
@schedules = map { (split /\s+/, $_)[1]} @schedules; 

# Now for each client 
foreach my $client (@clients) { 

if ($debug != 0) { print STDERR "$client $policy\n"; } 

# Be sure the client is at leas pingable otherewise the bpgetconfig command will take a long time to fail 
system("ping -c 1 -W 5 $client > /dev/null 2>&1"); 
if ($? != 0 ) { print STDERR "$client not pingable\n"; next } 

open VERSION, ">$output_dir/version.$client" or die "Couldn't open $output_dir/version.$client for output: $!\n"; 
print VERSION "============= Version Check of $client ===================\n"; 
print VERSION `bpgetconfig -t -A -g $client 2>&1`; 
print VERSION "============= End Version Check of $client ===================\n"; 
close VERSION; 

if ( ! $opt_v ) { 
# get, if any, the basic include and/or exclude files. (i.e. /usr/openv/netbackup/exclude_list or include_list) 
@status = `$nbadmin/bpgetconfig -e \"/$output_dir/exclude.$client.basic\" \"$client\" 2>&1`; 
if ($? != 0 && $debug != 0 ) { print STDERR "$client bpgetconfig -exclude no policy failed with $?\n @status" } 
@status = `$nbadmin/bpgetconfig -i \"/$output_dir/include.$client.basic\" \"$client\" 2>&1`; 
if ($? != 0 && $debug != 0 ) { print STDERR "$client bpgetconfig -include no policy failed with $?\n @status" } 

# get, if any, the policy include and/or exclude files. (i.e. /usr/openv/netbackup/exclude_list.policy or include_list.policy) 
@status = `$nbadmin/bpgetconfig -e \"/$output_dir/exclude.$policy.$client\" \"$client\" \"$policy\" 2>&1`; 
if ($? != 0 && $debug != 0 ) { print STDERR "$client bpgetconfig -exclude policy only failed with $?\n @status" } 
@status = `$nbadmin/bpgetconfig -i \"/$output_dir/include.$policy.$client\" \"$client\" \"$policy\" 2>&1`; 
if ($? != 0 && $debug != 0 ) { print STDERR "$client bpgetconfig -include policy only failed with $?\n @status" } 

# Now for each schedule in the policy (i.e. /usr/openv/netbackup/exclude.policy.schedule= 
foreach my $schedule (@schedules) { 
if ($debug != 0) { print STDERR "$client $policy $schedule\n"; } 
@status = `$nbadmin/bpgetconfig -e \"/$output_dir/exclude.$policy.$client.$schedule\" \"$client\" \"$policy\" \"$schedule\" 2>&1`; 
if ($? != 0 && $debug != 0 ) { print STDERR "$client bpgetconfig -exclude with policy and schedule failed with $?\n @status" } 
@status = `$nbadmin/bpgetconfig -i \"/$output_dir/include.$policy.$client.$schedule\" \"$client\" \"$policy\" \"$schedule\" 2>&1`; 
if ($? != 0 && $debug != 0 ) { print STDERR "$client bpgetconfig -include with policy and schedule failed with $?\n @status" } 
} 
} 
} 

# If any of the bpgetconfigs work the ouput will be in the file name following the -e or -i 
} 

exit;

====================== End of code ========================

Last edited by Don Cragun; 12-05-2014 at 02:40 AM.. Reason: Add CODE tags.
# 2  
Old 12-05-2014
Are you asking someone to rewrite your entire Perl script in Bash? That is more than most people are willing to do. A better question would be to ask how to convert one specific function from Perl to Bash. No one is going to want to write a script from scratch for someone they don't know.
# 3  
Old 12-05-2014
It is a lot of work and time to do this right. Perl is a better language to use for serious apps. So is Python. They both produce more security than the shell apps. What you want in essence is a downgrade. Why??

I'd like to run this script to see what it does, but cant because it needs more data files.
# 4  
Old 12-05-2014
You can't just "convert" from one language to another. It's a program, not a jpeg. Show the input you have and the output you want and we'll show you how to do it.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 12-05-2014
Quote:
Originally Posted by ongoto
It is a lot of work and time to do this right. Perl is a better language to use for serious apps. So is Python. They both produce more security than the shell apps.
I disagree. This is too sweeping a generalization, and I have seen far too many perl programs which are nothing by line after line of things in system() and backticks -- i.e. shells.
This User Gave Thanks to Corona688 For This Post:
# 6  
Old 12-05-2014
Hello Corona688

I mentioned security, and what I had in mind at that moment was protection from buffer over-runs, sql-insertions, and the like. The script presented here has the markings of an 'out-facing' app used by...who knows? I just don't think bash would be the best choice out there in the wild wild west. Smilie

System calls are common...bash does it better sometimes (thinking of grep vs Perl grep for example).
Cheers.

Last edited by ongoto; 12-05-2014 at 04:21 PM..
# 7  
Old 12-05-2014
Quote:
Originally Posted by ongoto
I mentioned security, and what I had in mind at that moment was protection from buffer over-runs, sql-insertions, and the like.
Buffer over-runs aren't much of an issue in a scripting language, and this script accepts no input except -d and -v.
Quote:
The script presented here has the markings of an 'out-facing' app used by...who knows? I just don't think bash would be the best choice out there in the wild wild west. Smilie
Even though mostly what this script does, is call bash?

Every time you do system(), that's bash.

Every time you use a set of ``, that's bash.

If you really want to avoid bash, you are going to have a hard time using perl to control any external processes, because perl is really bad at it, so bad that the standard function defaults to handing it off to bash... In which case, if you can, why not just write it in bash?
Quote:
System calls are common...
This script makes no system calls.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script ouput conversion

Hi All, I am trying to print all the packages info in solaris 11 using below script. #!/usr/bin/env bash pkginfo -l | egrep '(BASEDIR|NAME|VERSION)' | awk '{print}' > /tmp/cp1 /usr/bin/nawk -F: ' {for (i=1; i<=NF; i++) {gsub (/^ *| *$/, "", $i) ... (5 Replies)
Discussion started by: sravani25
5 Replies

2. UNIX for Beginners Questions & Answers

powershell script to unix shell script conversion.

Here is a powershell script to use restful API to create ticket in our ticketing tool. Can anyone please convert it to a shell script sothat, I can run it in Unix servers, below is the code: $body = @{ Customer= ''test' Summary= 'test summary' Impact= '4-Minor/Localized' ... (2 Replies)
Discussion started by: pandeybhavesh18
2 Replies

3. Shell Programming and Scripting

Batch to shell script conversion

Hi All, I have a small tool which is currently configured in batch scripts only. But my need is to run it on Linux platform, so I have been trying to convert a batch script to shell script. below is the batch script: @echo off IF "%1"== "" GOTO ARGERR REM UPDATE THESE PROPERTIES TO... (2 Replies)
Discussion started by: sukhdip
2 Replies

4. Shell Programming and Scripting

Help required for Oracle database shutdown script conversion from shell to perl

Please tell me how to convert below program from shell script to perl. Same commands need to use in shutdown, just need program help for startup. export ORACLE_BASE=/home/oracle1 lsnrctl start lndb1 sqlplus '/ as sysdba' startup; (2 Replies)
Discussion started by: learnbash
2 Replies

5. Shell Programming and Scripting

Conversion batch shell script

while converting batch file to shell script ...dis command is ther i dunno how to change...can anyone knws how to change into shell script rm !(D:\temp\XX.txt) (3 Replies)
Discussion started by: monisha
3 Replies

6. Shell Programming and Scripting

shell or perl script needed for ldif file to text file conversion

This is the ldf file dn: sdcsmsisdn=1000000049,sdcsDatabase=subscriberCache,dc=example,dc=com objectClass: sdcsSubscriber objectClass: top postalCode: 29600 sdcsServiceLevel: 10 sdcsCustomerType: 14 givenName: Adelia sdcsBlackListAll: FALSE sdcsOwnerType: T-Mobile sn: Actionteam... (1 Reply)
Discussion started by: LinuxFriend
1 Replies

7. AIX

Need timestamp conversion shell script !!

Can anyone provide me with a ksh or bash script which will accept a timestamp (format is YYYY-MM-DD-HH24.Mi.Ss) and time offset (in hours). The output will be (timestamp passed - time offset passed deducted from it) in the same YYYY-MM-DD-HH24.Mi.Ss format. Basically I am trying to convert the... (1 Reply)
Discussion started by: shibajighosh
1 Replies

8. Shell Programming and Scripting

Encoding conversion in PERL script

I have oracle 9i database installed with UTF-8 Encoding. I want a perl script that converts unicode to utf8 before commiting in database and utf8 to unicode when retreiving from database For example : the word Ïntêrnatïônàlîzâtion has to be stored in database as Internationalization and when retreived... (6 Replies)
Discussion started by: vkca
6 Replies

9. Shell Programming and Scripting

Help me with file conversion [Shell Script]

Hello Group, I request your help with a shell script for filter an ascii file (this is small piece of the file): 09/24/2009,00:00,1.0268,1.0268,1.0249,1.0250,518 09/24/2009,01:00,1.0251,1.0261,1.0249,1.0259,424 09/24/2009,02:00,1.0258,1.0272,1.0258,1.0269,372... (3 Replies)
Discussion started by: csierra
3 Replies

10. Shell Programming and Scripting

Conversion of bash parsing script to perl?

I need help with a perl parsing script. I have some error logs on a windows machine that I need to parse from a text file, but I know nothing about perl. I usually run this bash script on my linux box and it does just what I need. How would I do the same thing with perl and port it to my windows... (2 Replies)
Discussion started by: cstovall
2 Replies
Login or Register to Ask a Question