【问题标题】:Install nginx web server using shell script使用 shell 脚本安装 nginx 网络服务器
【发布时间】:2022-02-22 02:55:36
【问题描述】:

我想自动安装 nginx Web 服务,如果 nginx 已经存在,它将安装在其他位置,例如:/usr/local/nginx。

#!/bin/bash
echo "Beginning installation of nginx web server"
if ! which nginx > /dev/null 2>&1; then
    echo "Nginx is not installed"
else
    echo "Installing nginx"
    yum install nginx -y
    
    #check if nginx services are up
    service nginx start
fi

我是 shell 脚本的新手并尝试了上述方法,但是我无法在 else 块中执行命令。 非常感谢您的帮助。

【问题讨论】:

    标签: bash shell nginx


    【解决方案1】:

    我想我理解你想要做什么,但你代码中的逻辑不是最好的,所以这里是我第一个 bash 脚本中的一个片段,我做了类似的事情:

      read INP
      if [ $INP -eq 1 ]
      then
        if command -v nginx
        then
          systemctl status nginx
          back
        else
          error
          read INP
          if [ $INP == y ]
          then
            sudo apt install nginx
          else
            back
          fi
        fi
      fi
    

    顺便说一句,这是基于 Debian 的发行版,因此您可以将 apt 更改为 yum、pacman 等软件包管理器。为了清楚起见,返回和错误是我创建的函数。

    【讨论】:

      猜你喜欢
      • 2014-01-01
      • 1970-01-01
      • 2015-05-17
      • 1970-01-01
      • 2015-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-21
      相关资源
      最近更新 更多