vlambda博客
学习文章列表

shell脚本3__配置yum安装nginx

写个shell脚本,通过yum自动安装最新的stable版本,脚本如下:

#!/bin/bash#Life is short!#Show me the answer!#人生短暂,给我答案!
#获取操作系统的型号(centos/redhat/ubuntu等)和版本号(6/7等,只需要大版本号)OS=$(cat /etc/redhat-release |awk '{print $1}' |tr '[A-Z]' '[a-z]')OS_version=$(cat /etc/redhat-release |awk '{print $4}')OSRELEASE=$(echo ${OS_version:0:1})
#配置nginx的yum源echo -e "[nginx]name=nginx repo baseurl=http://nginx.org/packages/$OS/$OSRELEASE/\$basearch/gpgcheck=0enabled=1" >/etc/yum.repos.d/nginx.repo
#yum安装nginxyum -y install nginx
#判断nginx是否成功安装if [ $? -eq 0 ];thenecho "nginx installed successfully!"fi

看看脚本执行结果,下面只截取了最后一部分显示消息:

Installed:nginx.x86_64 1:1.16.1-1.el7.ngx
Dependency Updated:openssl.x86_64 1:1.0.2k-19.el7   openssl-libs.x86_64 1:1.0.2k-19.el7
Complete!nginx installed successfully!

再看看nginx的yum源/etc/yum.repos.d/nginx.repo

[root@tian shell]# more /etc/yum.repos.d/nginx.repo [nginx]name=nginx repobaseurl=http://nginx.org/packages/centos/7/$basearch/gpgcheck=0enabled=1


接下来说一说比较容易踩坑的地方,先看nginx的官方文档:


To set up the yum repository for RHEL/CentOS, create the file named /etc/yum.repos.d/nginx.repo with the following contents:

[nginx] 

name=nginx repo 

baseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/ gpgcheck=0 

enabled=1

Replace “OS” with “rhel” or “centos”, depending on the distribution used, and “OSRELEASE” with “6” or “7”, for 6.x or 7.x versions, respectively



也就是说在配置yum源文件的时候,替换“OS”字段的linux系统版本,必须用小写如“centos”或者“rhel”,要不然yum安装会报错!

所以脚本中的这一句,是要获取centos操作系统版本后全部转换成小写

OS=$(cat /etc/redhat-release |awk '{print $1}' |tr '[A-Z]' '[a-z]')

本文完。