06 软件管理和shell编程基础

06 软件管理和shell编程基础

1 软件管理

1.1 常规软件管理命令【以yum|dnf为例】-二进制包安装

#以nginx为例
#建立元数据(更新软件数据源)
yum makecache
#安装相应的软件
yum install nginx
#卸载相应的软件(包含配置文件等信息)
yum autoremove nginx
#查看待安装的软件
yum list |grep	关键字
yum search		软件名

#软件源信息
/etc/yum.conf					#主配置文件
/etc/yum.conf.d/xxx.repo		#定制的软件源配置文件

#定制本地源
#挂载文件到mnt中
file://为协议,本地数据源的时候需要用到file://协议,网络源则需要https://

[baseos]
name=Rocky Linux $releasever - BaseOS
mirrorlist=https://mirrors.rockylinux.org/mirrorlist?arch=$basearch&repo=BaseOS-$relea$
#baseurl=http://dl.rockylinux.org/$contentdir/$releasever/BaseOS/$basearch/os/
gpgcheck=1
enabled=1
countme=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-rockyofficial



#apt类似,但删除命令建议使用purge
apt purge ningx*					#可删除配置文件一类的东西


#卸载软件必须完整,若不完整,可能会产生bug,因此卸载的时候最好删除干净
#若出现问题,可使用下列命令
find / -name 软件名| xargs rm -rf	#彻底删除软件相关的文件

自定义软件源可参考各类镜像站的说明文档,建议清华开源镜像站的说明【以Ubuntu为例子】

1.2 源码安装

  1. 下载源码包
  2. 解压源码包
  3. 配置软件
  4. 编辑软件
  5. 安装软件
  6. 测试效果

下载位置:

  • 第三方仓库
  • 官方仓库
  • GitHub【企业用Gitlab】

2 服务管理

2.1 检查服务是否启动【Ubuntu默认启动,Rocky默认不启动】

2.1.1 查端口

ss -tunlp

2.1.2 查进程

ps aux | grep 软件名

2.1.3 查服务

systemctl status 软件名
命令 子命令 中文释义 服务名
systemctl
start 启动
stop 关闭
restart 重启
reload 重载配置文件
status 状态
enable 自动启动
disable 禁用自启
daemon-reload 重载服务
cat 查看
edit 编辑

2.2 服务内容管理

root@test-VirtualBox:~# cat /etc/systemd/system/bind9.service 
[Unit]											#描述信息
Description=BIND Domain Name Server
Documentation=man:named(8)
After=network.target
Wants=nss-lookup.target
Before=nss-lookup.target

[Service]										#服务启动的配置信息
Type=forking
EnvironmentFile=-/etc/default/named
ExecStart=/usr/sbin/named $OPTIONS
ExecReload=/usr/sbin/rndc reload
ExecStop=/usr/sbin/rndc stop
Restart=on-failure

[Install]										#希望被谁管理
WantedBy=multi-user.target
Alias=bind9.service
init 3		#这里的数字是systemd里面的运行级别

#运行级别一共分为7个级别
0
1
2
3						#文字级别=>multi-users
4
5						#图形级别=>文字级别+图形服务=>Graphic
6

3 shell基础

shell是命令解释器,将命令解释给操作系统,因此,shell是逐行执行的

shell一般用于自动化运维【手工做出的命令动作,以文件文本的形式自动进行】

  • 能做简单的事情

  • 使用方式

    • 命令行

      • 逐行输入、逐行确认、逐行执行
    • 文件-->脚本

      • 执行成功的命令,在文件里面合理的组合,从上到下的执行的顺序

3.1 脚本规范

  1. 脚本命名-见名知意-后缀:.sh

  2. 编写脚本工具

    1. vi
    2. vim
    3. echo
  3. 脚本内容的首行-指定脚本运行环境

    1. !/bin/bash-->优先级最高

  4. 脚本的内容

    1. 正常执行的命令
  5. 脚本的执行

/bin/bash file.sh					#file.sh可以是绝对路径,也可以是相对路径,推荐使用该方法
/bin/bash -x file.sh				#利用-x参数检查bug

file.sh								#文件必须具有x权限

source file.sh						#工作场景:加载脚本内部的环境到当前的终端环境
  1. 优秀的代码书写习惯:成对出现

3.2 变量相关

  1. 变量的作用--提高脚本变动的效率

  2. 变量如何定义

  3. 变量如何删除

  4. 变量如何查看

    1. 普通环境变量:echo $变量名
    2. 全局的环境变量:echo $变量名 | evn | declare
#本地环境变量
变量名=变量值
变量名='变量值'					#直接将内部的字符直接输出
变量名="变量值"					#内部字符若有变量,会先进行变量转换,然后输出相应的内容

变量名=`命令`
变量名=$(命令)					#命令的结果传递给变量名,同时只适用于当前终端

#全局变量-范围稍大
变量名=变量值
export 变量名

#删除变量名
unset 变量名

3.2.1 变量的作用范围

img

3.2.2 内置环境变量

序号 变量名 解析
1 $0 获取当前执行的shell脚本文件名
2 $n 获取当前执行的shell脚本的第n个参数值,当n为0时表示脚本的文件名,如果n大于9就要用大括号括起来$
3 $# 获取当前shell命令行中参数的总个数
4 $? 获取执行上一个指令的返回值(0为成功,非0为失败)

3.2.3 和字符串相关

	--截取

${ver_name:起始位置:截取长度}

			0代表第一个字符

			1代表第二个字符
root@test-VirtualBox:~# string="sdifdahsdfujhasdfkjasdhbfjsdaf"
root@test-VirtualBox:~# echo ${string:3:6}								#正数的第4个字符开始
fdahsd
root@test-VirtualBox:~# echo ${string:0-3:6}							#倒数的第3个字符开始
daf
root@test-VirtualBox:~# echo ${string:0-8:6}							#倒数的第8个字符开始,往后6位
hbfjsd

3.2.4 脚本交互

--将 脚本外部的参数,传递到脚本内部,进行使用
1 脚本执行的时候,输入参数
/bin/bash file.sh arg1 arg2
	-- $1 $2
2 脚本执行过程中,输入数据
	read
场景:输入用户名和登录密码

root@test-VirtualBox:~# vim simple_login.sh
root@test-VirtualBox:~# /bin/bash simple_login.sh test 12345678
当前主机的登陆用户名为:test
当前主机的登陆密码:12345678
root@test-VirtualBox:~# cat simple_login.sh 
#!/bin/bash
#简单的脚本参数传递
#
#定制环境变量
username="$1"
passwd="$2"

echo "当前主机的登陆用户名为:$username"
echo "当前主机的登陆密码:$passwd"

#以read来执行相应的脚本
root@test-VirtualBox:~# cp simple_login.sh read_login.sh
root@test-VirtualBox:~# vim read_login.sh
root@test-VirtualBox:~# /bin/bash read_login.sh
请输入登陆用户名: pppp
请输入登陆密码: 1234567
当前主机的登陆用户名为:pppp
当前主机的登陆密码:1234567
root@test-VirtualBox:~# cat read_login.sh
#!/bin/bash
#简单的read脚本参数传递
#
#定制环境变量
read -p "请输入登陆用户名: " username
read -p "请输入登陆密码: " passwd

echo "当前主机的登陆用户名为:$username"
echo "当前主机的登陆密码:$passwd"

3.3 表达式-【相对于3.4,本节内容为单条件判断】

3.3.1 计算表达式

//$[计算表达式]
#例如
//$[2+5]
root@test-VirtualBox:~# echo $[ 4 % 5 ]
4
root@test-VirtualBox:~# echo $[ 4 / 5 ]
0
root@test-VirtualBox:~# echo $[2+3]
5

//let
root@test-VirtualBox:~# let a=5+6
root@test-VirtualBox:~# echo $a
11
root@test-VirtualBox:~# let a=5 * 6
-bash: let: get_args.sh: 语法错误:无效的算术运算符(错误记号是 ".sh")
#let和a之间需要空格来进行分割
root@test-VirtualBox:~# leta=5+6
root@test-VirtualBox:~# echo $leta
5+6

//expr
root@test-VirtualBox:~# expr 2+3
2+3
root@test-VirtualBox:~# expr 2 + 3
5
root@test-VirtualBox:~# expr 2 * 3
expr: 语法错误:未预期的参数 "get_args.sh"
root@test-VirtualBox:~# expr 2 \* 3
6
root@test-VirtualBox:~# expr 2 \\ 3
expr: 语法错误:未预期的参数 "\\"
root@test-VirtualBox:~# expr 2 \/ 3
0

//echo "2+3" | bc
root@test-VirtualBox:~# echo "scale=2;5 * 8" | bc
40
root@test-VirtualBox:~# echo "5 * 8" | bc
40
root@test-VirtualBox:~# echo "scale=2;5 / 8" | bc
.62
root@test-VirtualBox:~# echo "scale=6;5 / 8" | bc
.625000

//带计算功能的赋值运算
#+=(使用频率较高)
let a=a+1	=> let a+=1
let a=a+2	=> let a+=2
#-=(使用频率较高)
let a=a-1	=> let a-=1
let a=a-2	=> let a-=2
*=
let a=a*1	=> let a*=1
let a=a*2	=> let a*=2
/=
%=

3.3.2 测试功能|判断功能

test 表达式 或者  [ 表达式 ]
	判断表达式是否成立
		成立的话 	$?  0			
		不成立的话 	$?  1		
[]也可用于表达判断

root@test-VirtualBox:~# [ q == q ]
root@test-VirtualBox:~# echo $?
0
root@test-VirtualBox:~# [ q == qqqqqq ]
root@test-VirtualBox:~# echo $?
1

3.3.3 逻辑表达式

&&				#且|多条件场景

||				#或|命令执行场景

!				#取反

#示例
root@test-VirtualBox:~# [ 1 == 1 ] && echo "条件成立"
条件成立
root@test-VirtualBox:~# [ 1 == 2 ] && echo "条件成立"
root@test-VirtualBox:~# [ 1 == 2 ] || echo "条件不成立"
条件不成立
root@test-VirtualBox:~# [ 1 == 1 ] || echo "条件不成立"
root@test-VirtualBox:~# [ 1 == 1 ] && echo "条件成立" || echo "条件不成立"
条件成立
root@test-VirtualBox:~# [ 1 == 2 ] && echo "条件成立" || echo "条件不成立"
条件不成立
root@test-VirtualBox:~# [ ! 1 == 1 ] && echo "条件成立" || echo "条件不成立"
条件不成立

3.3.4 字符串表达式

#内容比较判断
	str1 == str2			#str1和str2字符串内容一致
	str1 != str2			#str1和str2字符串内容不一致,!表示相反的意思
#内容控制判断
	-z	str					#空值判断,获取字符串长度,长度为0,返回True
	-n	"str"				#非空值判断,获取字符串长度,长度不为0,返回True
								#注意:str外侧必须携带"",否则无法判断

#示例
root@test-VirtualBox:~# [ aaa == aaa ]
root@test-VirtualBox:~# echo $?
0
root@test-VirtualBox:~# [ aaa != aaa ]
root@test-VirtualBox:~# echo $?
1
root@test-VirtualBox:~# [ aaa != aaaaaaa ]
root@test-VirtualBox:~# echo $?
0
root@test-VirtualBox:~# string=nihao
root@test-VirtualBox:~# echo $string
nihao
root@test-VirtualBox:~# [ -z $string ]
root@test-VirtualBox:~# echo $?
1
root@test-VirtualBox:~# [ -n $string ]
root@test-VirtualBox:~# echo $?
0
root@test-VirtualBox:~# [ -n $string1 ]			#未定义string1的情况下本应输出为1,出现错误,因此需要加上双引号(英文状态下)
root@test-VirtualBox:~# echo $?
0
root@test-VirtualBox:~# [ -n "$string1" ]
root@test-VirtualBox:~# echo $?
1

3.3.5 数字表达式

#语法解读
n1 -eq n2		#相等
n1 -ne n2		#不等于
n1 -ge n2		#大于等于
n1 -gt n2		#大于
n1 -lt n2		#小于
n1 -le n2		#小于等于

#示例,并以此类推
root@test-VirtualBox:~# [ 1 -eq 1 ]
root@test-VirtualBox:~# echo $?
0
root@test-VirtualBox:~# [ 1 -eq 11 ]
root@test-VirtualBox:~# echo $?
1

#默认情况下以字符串进行判断,因此'=='和'-eq'在数字上都可以使用
root@test-VirtualBox:~# [ 1 == 1 ]
root@test-VirtualBox:~# echo $?
0

#综合运行一下示例
root@test-VirtualBox:~# ls
bak  get_args.sh  get_file.sh  get_num.sh  read_login.sh  simple_login.sh  snap
root@test-VirtualBox:~# vim simple_login.sh 
root@test-VirtualBox:~# cat simple_login.sh 
#!/bin/bash
#简单的脚本参数传递


#要求脚本必须接受两个参数

[ $# -eq 2 ] || exit 
#定制环境变量
username="$1"
passwd="$2"

echo "当前主机的登陆用户名为:$username"
echo "当前主机的登陆密码:$passwd"
root@test-VirtualBox:~# /bin/bash simple_login.sh 
root@test-VirtualBox:~# /bin/bash -x simple_login.sh 
+ '[' 0 -eq 2 ']'
+ exit
root@test-VirtualBox:~# /bin/bash -x simple_login.sh test 123456
+ '[' 2 -eq 2 ']'
+ username=test
+ passwd=123456
+ echo 当前主机的登陆用户名为:test
当前主机的登陆用户名为:test
+ echo 当前主机的登陆密码:123456
当前主机的登陆密码:123456

3.3.6 文件表达式

#文件属性判断
	-d		#检查文件是否存在且为目录文件
	-f		#检查文件是否存在且为普通文件

#文件权限判断
	-x		#检查文件是否存在且可执行

#示例
root@test-VirtualBox:~# ls
get_args.sh  get_file.sh  get_num.sh  read_login.sh  simple_login.sh  snap
root@test-VirtualBox:~# [ -d bak ] || mkdir bak
root@test-VirtualBox:~# ls
bak  get_args.sh  get_file.sh  get_num.sh  read_login.sh  simple_login.sh  snap
root@test-VirtualBox:~# [ -x get_num.sh ] && ./get_num.sh || chmod +x get_num.sh		#检测get_num.sh是否具有执行权限,若没有,添加执行权限
root@test-VirtualBox:~# ls
bak  get_args.sh  get_file.sh  get_num.sh  read_login.sh  simple_login.sh  snap
root@test-VirtualBox:~# [ -x get_num.sh ] && ./get_num.sh || chmod +x get_num.sh
收到的总参数量:0

img

3.4 脚本编程进阶

[ 条件判断1 -a 条件判断2 ]			#a == and
[ 条件判断1 -o 条件判断2 ]			#o == or

[[ 条件判断1 && 条件判断2 ]]
[[ 条件判断1 || 条件判断2 ]]

[[单条件判断]]					#支持正则表达式
[单条件判断]

#示例
root@test-VirtualBox:~# string=value
root@test-VirtualBox:~# [ $string = value ]
root@test-VirtualBox:~# echo $?
0
root@test-VirtualBox:~# [ $string = v* ]
root@test-VirtualBox:~# echo $?
1
root@test-VirtualBox:~# [[ $string = v* ]]
root@test-VirtualBox:~# echo $?
0

3.4.1 逻辑控制

单分支:只有条件成立的时候执行,否则,无任何显示

双分支:条件成立的时候执行,否则,执行另外一个输出结果

多分支:条件成立的时候执行,否则,执行另外一个条件,一直执行到输出结果

#单if语句
if 条件判断
then
	执行语句
fi


#双分支语句
if 条件判断
then
	执行语句
else 条件判断
then
	执行语句
fi

#多分支语句
if 条件判断
then
	执行语句
elif 条件判断
then 
	执行语句
else
	执行语句
fi


#示例,多分支语句
root@test-VirtualBox:~# cat duo_if.sh 
#!/bin/bash
#多分支if测试
#

#接受参数
gender="$1"

if [ "${gender}" == "nan" ]
then
        echo "您的性别为男性"
elif [ "${gender}" == "nv" ]
then
        echo "您的性别为女性"
else
        echo "Are you from American or from 泰国?"
fi

root@test-VirtualBox:~# /bin/bash duo_if.sh nan
您的性别为男性
root@test-VirtualBox:~# /bin/bash duo_if.sh na
Are you from American or from 泰国?
root@test-VirtualBox:~# /bin/bash duo_if.sh nv
您的性别为女性
#多分支if语句集成练习
root@test-VirtualBox:~# cat systemctl.sh 
#!/bin/bash
# 服务启动脚本
#

# 接受参数
server_action="$1"
#
# 判断逻辑
if  [ "${server_action}" == "start" ]
then
        echo "服务启动中..."
elif [ "${server_action}" == "stop" ]
then
        echo "服务停止中..."
elif [ "${server_action}" == "restart" ]
then
        echo "服务重启中..."
else
        echo "脚本 $0 使用方式:/bin/bash $0 [ start|stop|restart ]"
fi

root@test-VirtualBox:~# /bin/bash systemctl.sh 
脚本 systemctl.sh 使用方式:/bin/bash systemctl.sh [ start|stop|restart ]
root@test-VirtualBox:~# /bin/bash systemctl.sh start
服务启动中...
root@test-VirtualBox:~# /bin/bash systemctl.sh stop
服务停止中...
root@test-VirtualBox:~# /bin/bash systemctl.sh restart
服务重启中...

#优化上述代码,采用case来撰写【较为简单】
case "变量名" in
	"值1")					#类似于[ "变量名" == 值1 ]
		执行语句1;;
	"值2")
		执行语句2;;
	...
	*)
		执行语句X;;
esac

#优化后如下
root@test-VirtualBox:~# cat systemctl1.sh
#!/bin/bash
# 服务启动脚本
#

# 接受参数
server_action="$1"
#
# 判断逻辑
case  "${server_action}" in
        "start")
                echo "服务开启中...";;
        "stop")
                echo "服务关闭中...";;
        "restart")
                echo "服务重启中...";;
        *)
                echo "脚本 $0 使用方式:/bin/bash $0 [ start|stop|restart ]"
esac
# 简单的多行注释
:<<!
if  [ "${server_action}" == "start" ]
then
        echo "服务启动中..."
elif [ "${server_action}" == "stop" ]
then
        echo "服务停止中..."
elif [ "${server_action}" == "restart" ]
then
        echo "服务重启中..."
else
        echo "脚本 $0 使用方式:/bin/bash $0 [ start|stop|restart ]"
fi
!
root@test-VirtualBox:~# /bin/bash systemctl1.sh
脚本 systemctl1.sh 使用方式:/bin/bash systemctl1.sh [ start|stop|restart ]
root@test-VirtualBox:~# /bin/bash systemctl1.sh start
服务开启中...
root@test-VirtualBox:~# /bin/bash systemctl1.sh stop
服务关闭中...
root@test-VirtualBox:~# /bin/bash systemctl1.sh restart
服务重启中...

posted @ 2026-01-24 10:43  小茗同学study  阅读(2)  评论(0)    收藏  举报