Detect OS type using Perl
operating-system, perl
Solution
The first step is to check the output of the `$^O` variable.
If the output is for example
linux
you need more processing to detect which distribution is used.
See `perldoc perlvar`.
So far, you can run `lsb_release -a` to see which distribution 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: Detecting Underlying Linux Distro
Consider xaxes' solution too using the `Linux::Distribution` module to check the distribution.
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;
}
Problem
I want to use Perl to detect the OS type. For example: Let's 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. Let's say it's Linux. Now which type of Linux? - Fedora - Ubuntu - CentOS - Debian - etc. How would I do this?