Perl Scripting from a shell script with if and for

Hi,

Im trying to convert a script I have in shell to a similar one using Perl (newbie with Perl..).

Version I have: This is perl, v5.8.8 built for x86_64-linux-thread-multi

First the script has to get 3 pieces of information:

1- Get number o SMSs connections from this command line: (rtl is a aliais for a custom command available for the user that will run the script)
>rtl | grep SMS_GATEWAY/NumberOfConnections

And the output is:
"Transp" -inst="ts1a" "SMS_GATEWAY/NumberOfConnections" "3" -type=FWI32 -description="swdwedwe" -visibility=INSTANCE

In this case 3 connections.

2- Get if the connections is TCIP or S7 per the ChannelType for each connection, so it will be 3 commands like this:

>rtl | grep CONNECTION_<connection_number> | grep ChannelType
"Transp" -inst="ts1a" "SMS_CONNECTION_1/ChannelType" "TCIP" -type=STRING -description="Sfjdroforjfoerjie" -visibility=INSTANCE

<connection_number> is the 1,2,3 for the example with 3 connections (as above), so it has to be a loop (not sure if possible using Perl...
In this case it was '1'.

The channel type on this example is TCIP. (but could be S7)


3- Get the SMPP in which the connection relates to.

> rtl | grep CONNECTION_<connection_number>/Protocol_ID
"Transp" -inst="ts1a" "SMS_CONNECTION_1/Protocol_ID" "SMPP_A" -type=STRING -description="sjoidjeiojdieo" -visibility=INSTANCE

The value for this one is SMPP_A.

This has to be done for each connection... in this case 3 times (connec 1,2,3)

===

After this information gathering, the script will get the information its really looking for using the variables saved before. Using this command: (will need an if)

-  If the connection is a TCIP then do: (for each connection)

rtool | grep -E "<Value got on #3>/OriginatingAddressTon|<Value got on #3>/OriginatingAddressNpi|<Value got on #3>/DestinationAddressTon|SMS_CONNECTION_<connection_number>/SEND/TCPIP/StartConnection|SMS_CONNECTION_<connection_number>/ProtocolType" | cut -d " " -f 1,2,3,4

Output sample for each connection:
"TransportServer" -inst="ts1a" "SMSC_CONNECTION_1/SEND/TCPIP/StartConnection" "connect"
"TransportServer" -inst="ts1a" "SMSC_CONNECTION_1/Protocol_ID" "SMPP_A"
"TransportServer" -inst="ts1a" "SMSC_CONNECTION_1/ProtocolType" "SMPP"
"TransportServer" -inst="ts1a" "SMPP_A/DestinationAddressTon" "0"
"TransportServer" -inst="ts1a" "SMPP_A/OriginatingAddressNpi" "0"
"TransportServer" -inst="ts1a" "SMPP_A/OriginatingAddressTon" "0"

<Value got on #3>  value that will be individual for each connection - for connection 1 can be SMPP_A and for connection 2 can be SMPP_C...

<connection_number> is the 1,2,3 for the example with 3 connections (as above), so it has to be a loop (not sure if possible using Perl...



-  If the connection is a S7 then do:
rtool | grep -E "SMS_CONNECTION_<connection_number>/SS7MSC/MAPVersion|TransportServer/ts1a/SMS_CONNECTION_<connection_number>/SS7/Trace/User|TransportServer/ts1a/SMS_CONNECTION_<connection_number>/SS7/TranslationType/TT_for_MSC_GT" | cut -d " " -f 1,2,3,4

<connection_number> is the 1,2,3 for the example with 3 connections (as above), so it has to be a loop (not sure if possible using Perl...


====

Sorry for the long explanation... Wanted to cover the details...

Not sure if its possible (easy to be done) using Perl.

Tks,
Joao
joaotellesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jmcgOwnerCommented:
This seems like a bigger project than a newbie at Perl is likely to succeed at. I have no doubt that a Perl script can accomplish the task, but the approach one would take in Perl is rather different from the approach needed with shell scripting.

First, given the diverse outputs from the rtl and rtool commands, there would be a tendency or incentive to try to parse that output within the Perl script rather than calling out to grep or cut. This could be a big project if you wanted to be general or it could simply pull out (with regular expressions) the particular values of interest based on their context.

Second, yes, Perl has loops and arrays, so iterating over multiple items is definitely feasible. Usually you would prefer to use the foreach approach rather than set up a C-style (or should I call it FORTRAN-style?) for(i=0;i<3;i++) loop.

What I suggest is that you break the task down into smaller steps, make an attempt at the first step and post here about where you're getting stuck.
0
joaotellesAuthor Commented:
Tks for the reply.

I agree... I will take you advice and start from the beginning without over thinking it...  each step at a time...

I did not quite understood what you meant on the first observation...

Just for the sake of having an example to guide from... how would you do this the item #1

>rtl | grep SMS_GATEWAY/NumberOfConnections

And the output is:
"Transp" -inst="ts1a" "SMS_GATEWAY/NumberOfConnections" "3" -type=FWI32 -description="swdwedwe" -visibility=INSTANCE

To store the '3' in a variable (I might still have the shell way of thinking.. if Perl differs that much from it)

Tks,
Joao
0
jmcgOwnerCommented:
So, as an example, you could do:
my ($var1) = `rtl` =~  m{"SMS_GATEWAY/NumberOfConnections" "(\d+)"}m;

Open in new window

which would assign $var1 the value 3.

[Well, I've tested that it works in Perl 5.14 - I'd have to dig around quite a bit to find a 5.8 environment to test in.]

Without being able to see the unfiltered output of this rtl program, I suspect, but can't be sure, that the first step, to get the number of connections, can be skipped entirely. The individual connections appear to all be included in the output and the parameters corresponding to each one can probably be pulled out by another regular expression match, using just one execution of the rtl program.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
joaotellesAuthor Commented:
Tks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.