Perl to shell script

Perl to shell script

Post by teste » Thu, 09 Mar 2006 09:53:02



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

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

 
 
 

Perl to shell script

Post by Stachu 'Dozzie' K » Thu, 09 Mar 2006 17:00:39



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


> $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

 
 
 

Perl to shell script

Post by John W. Krah » Thu, 09 Mar 2006 19:07:22




>>Can anyone help me to convert the following perl in to shell script
>>please.


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

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

 
 
 

Perl to shell script

Post by Stachu 'Dozzie' K » Fri, 10 Mar 2006 00:24:13





>>>Can anyone help me to convert the following perl in to shell script
>>>please.


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

> Why not just:

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

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

Quote:> Or even:

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

>:-)

Do you play perlgolf? :)

--
Feel free to correct my English
Stanislaw Klekot

 
 
 

Perl to shell script

Post by teste » Fri, 10 Mar 2006 07:16:44




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


> > $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!

Thanks Stachu. Can you please explain

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

- Show quoted text -

Quote:

> #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-

 
 
 

Perl to shell script

Post by DJ Stunk » Fri, 10 Mar 2006 07:25:47



> 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

 
 
 

Perl to shell script

Post by Stachu 'Dozzie' K » Fri, 10 Mar 2006 07:31:54



Quote:> 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.

Quote:> 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

 
 
 

Perl to shell script

Post by John W. Krah » Fri, 10 Mar 2006 15:56:17





>>>#v+
>>>print join '', map { sprintf '%02X', $_ } split /\./, $ip_address;
>>Why not just:

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

> 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

 
 
 

Perl to shell script

Post by Mark Hobl » Sun, 12 Mar 2006 12:07:55



> 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/

 
 
 

1. Perl or shell scripts

Hello I use Linux intensivley and find my self writing small scripts
which do the job for now.  However I like the look of Perl and I feel
that I must learn it now!.  I have perl and perl2 Orielly books so
someone convince me not to.  I am basically aimin to use it for
sysadmin stuff and to make life easier on my keyboard/fingers.

2. exec vs. pipe

3. Execute a sh script under perl and sh: sh script; perl script?

4. Restoring overwritten ufsdump

5. Incomplete output from crontab email: running shell scripts from a perl script

6. How to log all files used at boot??

7. Question: Use a Perl Script to Make a Call to a Bourne Shell Script?

8. Passing a variable between shell script and perl script

9. Passing a variable from a Perl script to a shell script

10. PERL script vs SHELL script

11. can you translate shell script into perl script?

12. shell-script executes a perl-script???