Detect OS type perl
I want to use Perl to detect the OS type.
For example:
Lets say I have an installer, in order to know which commands the installer needs to run, I'll have to detect what operating system is installed, for example "linux". Lets say it's "linux", now which type of "linux"?
How would I do this?
Also, you can use the Config (it's a core module), this is an example:
use Config;
print "$Config{osname}n";
print "$Config{archname}n";
On my Mac OS X, it prints:
darwin
darwin-2level
According to perlvar, either $OSNAME
or $^O
will give you the operating system. This is also equivalent to using $Config{'osname'}
(see Config for more). A special note for Windows systems:
In Windows platforms, $^O
is not very helpful: since it is always MSWin32 , it doesn't tell the difference between 95/98/ME/NT/2000/XP/CE/.NET. Use Win32::GetOSName()
or Win32::GetOSVersion()
(see Win32 and perlport) to distinguish between the variants.
In order to get the exact platform for a Linux box, you'll need to use a module like xaxes mentioned in his answer.
The first step is to checking the output of $^O
variable.
If the output is by example
linux
you need more processing to detect wich distro is used.
See perldoc perlvar
So far, you can run lsb_release -a
to see which distro is used.
If this command is not present, you can test the presence of files like :
/etc/debian_version # debian or debian based
/etc/redhat-release # rpm like distro
Other files tests examples : http://www.novell.com/coolsolutions/feature/11251.html
Consider xaxes solution too using Linux::Distribution
module to check the distro.
If you want to detect the package manager, you can try this approach :
if ($^O eq "linux") {
my $pm;
do{
if (-x qx(type -p $_ | tr -d "n")) {
$pm = $_;
last;
}
} for qw/apt-get aptitude yum emerge pacman urpmi zypper/;
print $pm;
}
链接地址: http://www.djcxy.com/p/54586.html
上一篇: perl系统调用不会随机执行
下一篇: 检测OS类型的Perl