是否有任何或命令可以使用 bash 命令/脚本检查 NGINX 是否已安装在 UBUNTU Linux 上?
我正在尝试这样的事情
echo "BEGINNING INSTALLATION OF NGINX WEB SERVER"
echo
echo
echo "CHECKING TO SEE IF NGINX IS ALREADY INSTALLED"
service nginx > temp.install 2> temperr.install
echo 111
grep -c unrecognized temperr.install > temp2.install
echo 222
status = `cat temp2.install`
echo "NGINX STATUS $status"
我是 Bash 脚本的新手,因此不确定这是否是解决此问题的最佳方法。我需要编写一个脚本来检查 NGINX 是否已安装。如果未安装,则只需安装 NGINX,否则先删除 NGINX,然后重新安装。
答案1
if ! which nginx > /dev/null 2>&1; then
echo "Nginx not installed"
fi
或者
if [ ! -x /usr/sbin/nginx ]; then
echo "Nginx not installed"
fi
或者如果你想要特定于 Debian/Ubuntu:
if ! dpkg -l nginx | egrep 'îi.*nginx' > /dev/null 2>&1; then
echo "Nginx not installed"
fi
如果你喜欢简洁的话:
! test -x /usr/sbin/nginx && echo "Nginx not installed"
答案2
尝试这个:
command -v nginx
如果尚未安装则安装:
command -v nginx || sudo apt install nginx
答案3
我构建了以下解决方案,它也在 cron 作业中运行
ISNGINX_INSTALLED=`/bin/ls /usr/sbin/nginx`
if [[ ! -z $ISNGINX_INSTALLED ]]; then
echo "NOT installed"
else
echo "installed"
fi
答案4
你可以像这样变得更清洁:
# create function
function ensure_nginx(){
if [ -x "$(command -v nginx)" ]; then
echo "Nginx already installed. Skipping installation..."
else
echo "Installing nginx..."
sudo apt update
sudo apt install nginx -y
sudo ufw app list
echo "Nginx installed!"
fi
}
# call it
ensure_nginx