Unix Shell - Perl to shell script

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > March 2006 > Perl to shell script





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Perl to shell script
tester

2006-03-07, 8:48 pm

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";

Stachu 'Dozzie' K.

2006-03-08, 2: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
John W. Krahn

2006-03-08, 7:52 am

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
Stachu 'Dozzie' K.

2006-03-08, 5: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
tester

2006-03-08, 5: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-
>


DJ Stunks

2006-03-08, 5: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

Stachu 'Dozzie' K.

2006-03-08, 5: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
John W. Krahn

2006-03-09, 2: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
Mark Hobley

2006-03-11, 2: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/

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com