簡體   English   中英

使用shell腳本安裝nginx web服務器

[英]Install nginx web server using shell script

我想自動安裝 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 腳本中的一個片段,我做了類似的事情:

  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 等 package 管理器。 需要說明的是,back 和 error 是我創建的函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM