操作系统:
RHEL7.x 或CentOS 7.x
- 最小化安装
- 配置好固定的IP,能访问互联网
- 配置好yum源(yum repolist 可以查看yum源)
- 本地光盘
- 挂载光盘,开机自动挂载
- vim + /etc/fstable
- /dev/sr0 /mnt iso9660 defaults 0 0
- 创建挂载点目录:
- mkdir /media/cdrom
- 挂载:mount -a
- 配置yum源:
- yum-config-manger --add-repo=file:/// media/cdrom
- echo "gpgcheck = 0" >> /etc/yum.repos.d/media_cdrom.repo
- 挂载光盘,开机自动挂载
- EPEL
- aliyun sohu 中科大 清华 网易
- 本地光盘
开发环境:vim
查看系统shell类型:
[root@template ~]# cat /etc/shells
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash
查看当前默认shell:
[root@template ~]# echo $SHELL
/bin/bash
快速如何快速生成脚本开头的版本版权注释信息
[root@template ~]# cat ~/.vimrc
autocmd BufNewFile *.go,*.py,*.cc,*.sh,*.java exec ":call SetTitle()"
func SetTitle()
if expand("%:e") == 'sh'
call setline(1,"#!/bin/bash")
call setline(2,"#########################")
call setline(3,"#File name:".expand("%"))
call setline(4,"#Version:v1.0")
call setline(5,"#Email:[email protected]")
call setline(6,"#Created time:".strftime("%F %T"))
call setline(7,"#Description:")
call setline(8,"#########################")
call setline(9,"")
endif
endfunc
修改Tab缩进
在/etc/vim/下有两个文件,分别为vimrc 和vimrc.tiny
在vimrc文件中加入:set tabstop=4
流程控制与判断练习:
1、ping主机测试,查看主机是否存活
[root@template chap04]# cat ping.sh
#!/bin/bash
#########################
#File name:ping.sh
#Version:v1.0
#Email:[email protected]
#Created time:2023-03-23 17:32:18
#Description:
#########################
read -p "please enter your host:" host
if ping -c2 $host &> /dev/null
then
echo "$host is running"
else
echo "$host is down"
fi
2、判断一个用户是否存在
[root@template chap04]# cat user.sh
#!/bin/bash
#########################
#File name:user.sh
#Version:v1.0
#Email:[email protected]
#Created time:2023-03-23 17:45:03
#Description:
#########################
read -p "please enter a username:" username
if id $username &> /dev/null
then
echo "$username is exist"
else
echo " $username is not exist "
fi
3、判断当前内核主版本是否为3,且次版本是否大于10
[root@template chap04]# cat sys.sh
#!/bin/bash
#########################
#File name:sys.sh
#Version:v1.0
#Email:[email protected]
#Created time:2023-03-23 17:52:56
#Description:
main_version=`uname -r | awk -F . '{print $1 }'`
minor_version=`uname -r | awk -F . '{print $2}'`
if [ "$main_version" -eq 3 ] && [ "$minor_version" -ge 10 ]
then
echo "主版本是:$main_version 次版本是:$minor_version"
else
echo "不满足条件,此系统的主版本是:$main_version 次版本是:$minor_version"
fi
4、判断vsftpd软件包是否安装,如果没有则自动安装
#!/bin/bash
#########################
#File name:isvsftp.sh
#Version:v1.0
#Email:[email protected]
#Created time:2023-03-23 18:05:56
#Description:
#########################
if rpm -qa | grep vsftpd &> /dev/null
then
echo "vsftp is exist"
else
echo "vsftp is not exist"
read -p "please enter your choice:" choice
if [ $choice -eq 1 ]
then
yum install vsftpd -y &> /dev/null
else
echo " break and uninstall"
fi
fi
# 测试结果:最初环境没有安装,选择时随机输入,最后选择安装,最后一次测试检查是否安装成
[root@template chap04]# ./isvsftp.sh
vsftp is not exist
please enter your choice:2
break and uninstall
[root@template chap04]# ./isvsftp.sh
vsftp is not exist
please enter your choice:1
[root@template chap04]# ./isvsftp.sh
vsftp is exist
5、判断httpd是否运行
# 也不算完整:先检测是否安装htppd.service此软件,注意centos7版本中自带一个httpd-xxx的工具,所以在写服务时最好加上httpd.service,程序流程:如果包存在,直接输出active,如果不存在就选择是否安装
[root@template chap04]# cat httpd.sh
#!/bin/bash
#########################
#File name:httpd.sh
#Version:v1.0
#Email:[email protected]
#Created time:2023-03-23 18:20:59
#Description:
#########################
if rpm -qa | grep httpd.service &> /dev/null
then
echo "system `systemctl is-active httpd`"
else
echo "httpd is uninstall"
read -p " please enter your choice : " choice
if [ $choice -eq 1 ]
then
yum install httpd -y &> /dev/null
systemctl restart httpd
else
echo " break and uninstall"
fi
fi
6、判断指定的主机是否能ping通,必须使用$1变量
[root@template chap04]# cat ping2.sh
#!/bin/bash
#########################
#File name:ping2.sh
#Version:v1.0
#Email:[email protected]
#Created time:2023-03-23 18:55:21
#Description:
#########################
if ping -c1 $1 &> /dev/null
then
echo "$1 is runing"
else
echo "$1 is dead"
fi
[root@template chap04]# ./ping2.sh 192.168.11.10
192.168.11.10 is runing
[root@template chap04]# ./ping2.sh baidu.com
baidu.com is runing
7、报警脚本,要求如下
根分区剩余空间小于20%
内存已用空间大于80%
向用户alice发送告警邮件
配合crond每5分钟检查一次
yum install mailx -y &> /dev/null
#!/bin/bash
total_mem=$(free -m | tr -s " " | cut -d " " -f 2 | head -2 | tail -1)
used_mem=$(free -m | tr -s " " | cut -d " " -f 3 | head -2 | tail -1)
used_memper=$(echo "scale=2;$used_mem/$total_mem*100" | bc)
total_root=$(df | grep "/"$ |tr -s " " | cut -d " " -f 2)
used_root=$(df | grep "/"$ |tr -s " " | cut -d " " -f 4)
free_rootper=$(echo "scale=2;$used_root/$total_root*100" | bc)
v1=$(echo "used_memper > 80" | bc)
v2=$(echo "free_rootper < 20" | bc)
if [ $v1 -eq 1 ];then
echo "内存已用空间大于80%" | mail -s "警告信息" alice
elif [ $v2 -eq 1 ];then
echo "根分区剩余空间小于20%" | mail -s "警告信息" alice
else
echo "正常使用"
fi
8、判断用户输入的是否是数字,如果是数字判断该数字是否大于10
[root@template chap04]# cat num.sh
#!/bin/bash
read -p "please input a num:" num
if echo " $num" | grep " [ 0-9 ]" &> /dev/null
then
if [ $num -gt 10 ]
then
echo "$num is more than 10"
else
echo "$num is less than 10"
fi
else
echo "input a num!!!"
fi
9、计算用户输入的任意两个整数的和、差、乘积、商、余数
a=$1
b=$2
if [ $# -eq 2 ]
then
if [[ "$a" =~ ^[0-9]*$ && "$b" =~ ^[0-9]*$ ]]
then
echo "a、b Is an integer"
echo a+b=$((a+b))
echo a-b=$((a-b))
echo a*b=$((a*b))
echo a/b=$((a/b))
echo a%b=$((a%b))
else
echo "a,b Is not an integer"
exit 0
fi
else
echo "The number of parameters is 2"
exit 0
fi
标签:bin,Shell,流程,echo,---,#########################,sh,template,root
From: https://www.cnblogs.com/sre-chan/p/17247478.html