Perl to shell script
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Shell > Perl to shell script




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Perl to shell script  
tester


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-08-06 01:48 AM

Hi gurus,
Can anyone help me to convert the following PERL in to shell script
please.
Best Regards
tester



@ip_array = split /\./, $ip_address;

$hex_ip1 = sprintf ("%x", $ip_array[0]);
$hex_ip2 = sprintf ("%x", $ip_array[1]);
$hex_ip3 = sprintf ("%x", $ip_array[2]);
$hex_ip4 = sprintf ("%x", $ip_array[3]);

if (length($hex_ip1) == 1) { $hex_ip1 = "0".$hex_ip1; }
if (length($hex_ip2) == 1) { $hex_ip2 = "0".$hex_ip2; }
if (length($hex_ip3) == 1) { $hex_ip3 = "0".$hex_ip3; }
if (length($hex_ip4) == 1) { $hex_ip4 = "0".$hex_ip4; }

$hex_ip = join '', $hex_ip1,$hex_ip2,$hex_ip3,$hex_ip4;
$hex_ip = uc $hex_ip;

$tftpfile1 = $hex_ip;
$tftpfile2 = join '', "$hex_ip","\.SUN4U";

# Create the necessary files in HEX in the /tftpboot directory
symlink "inetboot.SUN4U.Solaris", "/tftpboot/$tftpfile1";
symlink "inetboot.SUN4U.Solaris", "/tftpboot/$tftpfile2";






[ Post a follow-up to this message ]



    Re: PERL to shell script  
Stachu 'Dozzie' K.


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-08-06 07:49 AM

On 08.03.2006, tester <bshah@citadon.com> wrote:
> Hi gurus,
> Can anyone help me to convert the following PERL in to shell script
> please.
> Best Regards
> tester
>
>
>
> @ip_array = split /\./, $ip_address;
>
> $hex_ip1 = sprintf ("%x", $ip_array[0]);
> $hex_ip2 = sprintf ("%x", $ip_array[1]);
> $hex_ip3 = sprintf ("%x", $ip_array[2]);
> $hex_ip4 = sprintf ("%x", $ip_array[3]);
>
> if (length($hex_ip1) == 1) { $hex_ip1 = "0".$hex_ip1; }
> if (length($hex_ip2) == 1) { $hex_ip2 = "0".$hex_ip2; }
> if (length($hex_ip3) == 1) { $hex_ip3 = "0".$hex_ip3; }
> if (length($hex_ip4) == 1) { $hex_ip4 = "0".$hex_ip4; }
>
> $hex_ip = join '', $hex_ip1,$hex_ip2,$hex_ip3,$hex_ip4;
> $hex_ip = uc $hex_ip;
>
> $tftpfile1 = $hex_ip;
> $tftpfile2 = join '', "$hex_ip","\.SUN4U";
>
> # Create the necessary files in HEX in the /tftpboot directory
> symlink "inetboot.SUN4U.Solaris", "/tftpboot/$tftpfile1";
> symlink "inetboot.SUN4U.Solaris", "/tftpboot/$tftpfile2";

Eeee... I haven't seen such long code doing such little for quite long
time!

#v+
print join '', map { sprintf '%02X', $_ } split /\./, $ip_address;
#v-

#v+
ip=192.168.0.1
file1=$(IFS=.; printf %02X $ip)
file2=$file1.SUN4U
#v-

It's a homework for user to create symlink having file1 and file2
variables.

--
Feel free to correct my English
Stanislaw Klekot





[ Post a follow-up to this message ]



    Re: PERL to shell script  
John W. Krahn


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-08-06 12:52 PM

Stachu 'Dozzie' K. wrote:
> On 08.03.2006, tester <bshah@citadon.com> wrote: 
>
> Eeee... I haven't seen such long code doing such little for quite long
> time!
>
> #v+
> print join '', map { sprintf '%02X', $_ } split /\./, $ip_address;

Why not just:

printf '%02X%02X%02X%02X', split /\./, $ip_address;

Or even:

printf '%02X' x 4, $ip_address =~ /\d+/g;


:-)

John
--
use Perl;
program
fulfillment





[ Post a follow-up to this message ]



    Re: PERL to shell script  
Stachu 'Dozzie' K.


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-08-06 10:56 PM

On 08.03.2006, John W. Krahn <someone@example.com> wrote:
> Stachu 'Dozzie' K. wrote: 
>
> Why not just:
>
> printf '%02X%02X%02X%02X', split /\./, $ip_address;

Can't be sure there are >= 4 number parts.

> Or even:
>
> printf '%02X' x 4, $ip_address =~ /\d+/g;
>
>
>:-)

Do you play perlgolf? 

--
Feel free to correct my English
Stanislaw Klekot





[ Post a follow-up to this message ]



    Re: PERL to shell script  
tester


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-08-06 10:57 PM


Stachu 'Dozzie' K. wrote:
> On 08.03.2006, tester <bshah@citadon.com> wrote: 
>
> Eeee... I haven't seen such long code doing such little for quite long
> time!

Thanks Stachu. Can you please explain

print join '', map { sprintf '%02X', $_ } split /\./, $ip_address

>
> #v+
> print join '', map { sprintf '%02X', $_ } split /\./, $ip_address;
> #v-
>
> #v+
> ip=192.168.0.1
> file1=$(IFS=.; printf %02X $ip)
> file2=$file1.SUN4U
> #v-
>






[ Post a follow-up to this message ]



    Re: PERL to shell script  
DJ Stunks


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-08-06 10:57 PM


tester wrote:
> Hi gurus,
> Can anyone help me to convert the following PERL in to shell script
> please.

why would you ever want to go in that direction??

-jp






[ Post a follow-up to this message ]



    Re: PERL to shell script  
Stachu 'Dozzie' K.


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-08-06 10:57 PM

On 08.03.2006, tester <bshah@citadon.com> wrote:
> Thanks Stachu. Can you please explain

Yes, I can, but I do it reluctantly. This is shell group, not Perl
group. I gave shorter PERL sample because I hate unnecessarily long
code.

> print join '', map { sprintf '%02X', $_ } split /\./, $ip_address

Read from the end: split on '.' character. For each field (field is
in $_ variable) execute some code ("{...}"). This code returns some
value for field; map returns list of these values. The rest you can find
in perldoc.

--
Feel free to correct my English
Stanislaw Klekot





[ Post a follow-up to this message ]



    Re: PERL to shell script  
John W. Krahn


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-09-06 07:49 AM

Stachu 'Dozzie' K. wrote:
> On 08.03.2006, John W. Krahn <someone@example.com> wrote: 
>
> Can't be sure there are >= 4 number parts.

If there are > 4 number parts then it wouldn't be an IP address.  If you
really need validation then you need more than just split /\./.  :-)


John
--
use Perl;
program
fulfillment





[ Post a follow-up to this message ]



    Re: PERL to shell script  
Mark Hobley


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-11-06 07:48 AM

DJ Stunks <DJStunks@gmail.com> wrote:

> why would you ever want to go in that direction??

If the script is being used in a system maintenance environment, it is
possible that /usr is not mounted, or the PERL compiler is not available.

--
Mark Hobley
393 Quinton Road West
QUINTON
Birmingham
B32 1QE

Telephone: (0121) 247 1596
International: 0044 121 247 1596

Email: markhobley at hotpop dot donottypethisbit com

http://markhobley.yi.org/






[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 02:23 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register