diff --git a/perl-xCAT/xCAT/Utils.pm b/perl-xCAT/xCAT/Utils.pm index 35b9abea2..7da7d099c 100644 --- a/perl-xCAT/xCAT/Utils.pm +++ b/perl-xCAT/xCAT/Utils.pm @@ -48,7 +48,7 @@ require xCAT::Version; require DBI; our @ISA = qw(Exporter); -our @EXPORT_OK = qw(genpassword runcmd3); +our @EXPORT_OK = qw(genpassword runcmd3 natural_sort_cmp); # The functions that has been moved to TableUtils.pm @@ -4918,5 +4918,34 @@ sub acquire_lock_imageop { return (0,$lock); } +#-------------------------------------------------------------------------------- + +=head3 natural_sort_cmp + compare $left and $right by natrual. +=cut + +#-------------------------------------------------------------------------------- +sub natural_sort_cmp { + my $left = shift; + my $right = shift; + if( !($left =~ /\d+(\.\d+)?/) ) { + return $left cmp $right; + } + my $before = $`; + my $match = $&; + my $after = $'; + if( !($right =~ /\d+(\.\d+)?/) ) { + return $left cmp $right; + } + if( $before eq $` ) { + if( $match == $& ) { + return natural_sort_cmp( $after, $' ); + } else { + return $match <=> $&; + } + } else { + return $left cmp $right; + } +} 1; diff --git a/xCAT-server/lib/xcat/plugins/openbmc.pm b/xCAT-server/lib/xcat/plugins/openbmc.pm index c6dfa8cda..059fc1115 100644 --- a/xCAT-server/lib/xcat/plugins/openbmc.pm +++ b/xCAT-server/lib/xcat/plugins/openbmc.pm @@ -33,6 +33,7 @@ use xCAT::SvrUtils; use xCAT::GlobalDef; use xCAT_monitoring::monitorctrl; use POSIX qw(WNOHANG); +use xCAT::Utils qw/natural_sort_cmp/; $::VERBOSE = 0; # String constants for rbeacon states @@ -71,6 +72,10 @@ sub unsupported { } } +sub natural_sorter { + natural_sort_cmp( $a, $b ); +} + #------------------------------------------------------- =head3 handled_commands @@ -1887,12 +1892,10 @@ sub rinv_response { } } } - # If sorted array has any contents, sort it and print it + # If sorted array has any contents, sort it naturally and print it if (scalar @sorted_output > 0) { # sort alpha, then numeric - my @sorted_output = grep {s/(^|\D)0+(\d)/$1$2/g,1} sort - grep {s/(\d+)/sprintf"%06.6d",$1/ge,1} @sorted_output; - foreach (@sorted_output) { + foreach (sort natural_sorter @sorted_output) { # # The firmware output requires the ID to be part of the string to sort correctly. # Remove this ID from the output to the user @@ -2195,7 +2198,8 @@ sub rspconfig_response { push @output, "BMC IP Source: $ipsrc"; } elsif ($opt eq "netmask") { if ($address) { - my $decimal_mask = (2 ** $prefix - 1) << (32 - $prefix); + my $mask_shift = 32 - $prefix; + my $decimal_mask = (2 ** $prefix - 1) << $mask_shift; my $netmask = join('.', unpack("C4", pack("N", $decimal_mask))); push @output, "BMC Netmask: " . $netmask; } @@ -2390,9 +2394,7 @@ sub rvitals_response { # If sorted array has any contents, sort it and print it if (scalar @sorted_output > 0) { # Sort the output, alpha, then numeric - my @sorted_output = grep {s/(^|\D)0+(\d)/$1$2/g,1} sort - grep {s/(\d+)/sprintf"%06.6d",$1/ge,1} @sorted_output; - xCAT::SvrUtils::sendmsg("$_", $callback, $node) foreach (@sorted_output); + xCAT::SvrUtils::sendmsg("$_", $callback, $node) foreach (sort natural_sorter @sorted_output); } else { xCAT::SvrUtils::sendmsg("$::NO_ATTRIBUTES_RETURNED", $callback, $node); }