Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 不同发行版的脚本_Linux_Perl_Bash - Fatal编程技术网

Linux 不同发行版的脚本

Linux 不同发行版的脚本,linux,perl,bash,Linux,Perl,Bash,第一次来这里,我是一个新手,我为我糟糕的英语和我的错误感到抱歉:) 我需要使用perl(或bash)编写一个安装脚本,但我一直在寻找一种方法使它在不同的linux发行版中运行。特别是,如果尚未安装httpd和mysql,则该脚本应该安装它们。我可以使用很多if-else,但我想知道是否有任何自动化系统可以为我做到这一点,即使我找不到任何东西。例如: 伪代码 //find httpd if is installed, if not it will try to install try(servic

第一次来这里,我是一个新手,我为我糟糕的英语和我的错误感到抱歉:)

我需要使用perl(或bash)编写一个安装脚本,但我一直在寻找一种方法使它在不同的linux发行版中运行。特别是,如果尚未安装httpd和mysql,则该脚本应该安装它们。我可以使用很多if-else,但我想知道是否有任何自动化系统可以为我做到这一点,即使我找不到任何东西。例如:

伪代码

//find httpd if is installed, if not it will try to install
try(service httpd status) ..
else try (service apache2 status)
if (not_installed) { 
   try yum install httpd
   else try apt-get install apache2
   else try pacman ....
}
我在考虑类似自动工具的东西。。它存在吗

谢谢大家,再见:)

您可以使用chkconfig--list查找已安装的服务

可以这样做:

#!/bin/bash
chkmysql=`chkconfig --list | grep mysql | wc -l`
chkhttp=`chkconfig --list | grep http | wc -l`
if [[ chkmysql > 0 ]]
then
        echo "mysql is installed"
else
        echo "mysql is not installed"
        #write code to install mysql
fi
if [[ chkhttp > 0 ]]
then
        echo "http is installed"
else
        echo "http is not installed"
        #write code to install httpd service

fi

`$^O``变量将报告您所使用的操作系统,请参阅文档perlports platform部分了解其工作原理

perl还有一个配置模块,它包含perl编译时计算出的所有信息——使用类似于autotools的方法

配置手册页中的示例程序显示了可用的信息

        use Config;
       if ($Config{usethreads}) {
           print "has thread support\n"
       }

       use Config qw(myconfig config_sh config_vars config_re);

       print myconfig();

       print config_sh();


       config_vars(qw(osname archname));

顺便说一句,puppet擅长在不同的平台和不同的配置路径上安装软件包。虽然它使用ruby,因此可能不适合您的工具链的其他部分,因为您没有运行一次性安装,但我假设您担任某种系统管理员角色。你看过linux kickstarts吗?kickstart允许您完全按照您想要的方式获取系统,然后将其镜像以供以后远程安装。这样,如果一个系统发生故障,它可以在最小的外部依赖性下快速恢复。建立一个系统的自愈方法的问题是,当它不能正常工作时,它会导致更多的问题,然后再解决$0.02

您可以使用bash内置“type”来确定是否安装了给定的程序;或具有相同名称的函数/别名

if ! type PROGRAM >& /dev/null; then...
您可以解析/etc/issue的内容以获取操作系统的名称

if grep 'Arch Linux' /etc/issue >& /dev/null; then...

chkconfig并非在所有平台上都可用,因此作为“帮助我在不同版本的Linux上安装”的解决方案,它可能并不太好。ISTR它可以在Redhat变体上使用,因此可能会有一些有用的东西:它用于远程操作,但您可以在本地仅使用与服务相关的位。