Hi expert,
We have an old Solaris box and there's no -h available in df command.
Could you show me how to convert -k to -h in script ?
Thanks.
Would you mind posting the solaris version ?
Here is a sample that I wrote, you could try this
#! /opt/third-party/bin/perl
use strict;
use warnings;
use Switch;
use constant CONV_FACTOR => '1024';
sub num_to_letter {
my $val = shift;
my $notation;
switch ($val) {
case 1 { $notation = "K"; }
case 2 { $notation = "M"; }
case 3 { $notation = "G"; }
else { $notation = "Error"; }
}
return $notation;
}
sub num_convert {
my $val = shift;
my $cnt = 1;
while ( ($val / +CONV_FACTOR) > 1 ) {
$val /= +CONV_FACTOR;
$cnt++;
}
return $val . num_to_letter($cnt);
}
my $pipeCommand = "/bin/df -k |";
open(PIPE, $pipeCommand) or die "Unable to open PIPE command $pipeCommand\n";
while(<PIPE>) {
if( $. == 1 ) {
print;
next;
}
my @arr = split(/ */);
for( my $i = 1; $i <= 3; $i++ ) {
$arr[$i] = num_convert($arr[$i]);
}
print "@arr";
}
close(PIPE);
exit(0);
5.5.1 meaning 2.5.1 right ?
I'll try and let you know. I need to install perl first.
Sorry, I didn't understand that.
Could you please rephrase or elaborate on that ?
SunOS 5.5.1 ( or ) Solaris 2.5
syntax error in file ./dfh.pl at line 2, next 2 tokens "use strict"
syntax error in file ./dfh.pl at line 11, next 2 tokens "my $val "
syntax error in file ./dfh.pl at line 16, next 2 tokens "case 2"
syntax error in file ./dfh.pl at line 34, next 2 tokens "num_to_letter("
syntax error in file ./dfh.pl at line 38, next 2 tokens ") or"
syntax error in file ./dfh.pl at line 45, next 2 tokens "my @arr"
syntax error in file ./dfh.pl at line 46, next 2 tokens "my $i "
syntax error in file ./dfh.pl at line 47, next 2 tokens "num_convert("
Execution of ./dfh.pl aborted due to compilation errors.
===================================================
below is the path and version of perl i use
/tivoli/bin/solaris2/contrib/bin/perl
/tivoli/bin/solaris2/contrib/lib/perl
/tivoli/bin/lcf_bundle/bin/aix4-r1/tools/bin/perl
/tivoli/bin/lcf_bundle/bin/aix4-r1/tools/lib/perl
/tivoli/bin/lcf_bundle/bin/hpux10/tools/bin/perl
/tivoli/bin/lcf_bundle/bin/hpux10/tools/lib/perl
/tivoli/bin/lcf_bundle/bin/os2-ix86/tools/lib/perl
/tivoli/bin/lcf_bundle/bin/solaris2/tools/bin/perl
/tivoli/bin/lcf_bundle/bin/solaris2/tools/lib/perl
/tivoli/bin/lcf_bundle/bin/sunos4/tools/bin/perl
/tivoli/bin/lcf_bundle/bin/sunos4/tools/lib/perl
/tivoli/bin/lcf_bundle/bin/w32-ix86/tools/lib/perl
-----------------------------------
#/tivoli/bin/lcf_bundle/bin/solaris2/tools/bin/perl -v
This is perl, version 4.0
$RCSfile: perl.c,v $$Revision: 1.1 $$Date: 1996/05/10 05:06:54 $
Patch level: 36
Copyright (c) 1989, 1990, 1991, Larry Wall
Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 4.0 source kit.
I tested that under 5.6.0 and it worked as expected
Did you copy the source as such ? ( Sorry for asking such silly questions )
First check with '-c' option and then run
well, you can convert it to nawk
df -k | nawk '
function num_to_letter(val) {
if (val=="1") notation = "K"
else if (val=="2") notation = "M"
else if (val=="3") notation = "G"
else notation = "Error"
return notation
}
function num_convert(v) {
cnt=0
CONV_FACTOR=1024
while ( (v / CONV_FACTOR) > 1 ) {
v /= CONV_FACTOR;
cnt++;
}
return v""num_to_letter(cnt);
}
{
print num_convert($2)
}
'
code courtesy of matrix. I leave you to do the rest.