1.break函数
#!/bin/bash
#
#********************************************************************
#Author: hanweize
#QQ: 822020480
#Date: 2022-05-09
#FileName: break.sh
#URL: www.hanweize.cn
#Description: The test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
for i in {1..10};do
if [ $i -eq 5 ];then break ;fi
echo $i
done
2.颜色字体脚本
#!/bin/bash
#
#********************************************************************
#Author: hanweize
#QQ: 822020480
#Date: 2022-05-09
#FileName: colure.sh
#URL: www.hanweize.cn
#Description: The test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
color () {
RES_COL=60
MOVE_TO_COL="echo -en \\033[${RES_COL}G"
SETCOLOR_SUCCESS="echo -en \\033[1;32m"
SETCOLOR_FAILURE="echo -en \\033[1;31m"
SETCOLOR_WARNING="echo -en \\033[1;33m"
SETCOLOR_NORMAL="echo -en \E[0m"
echo -n "$1" && $MOVE_TO_COL
echo -n "["
if [ $2 = "success" -o $2 = "0" ] ;then
${SETCOLOR_SUCCESS}
echo -n $" OK "
elif [ $2 = "failure" -o $2 = "1" ] ;then
${SETCOLOR_FAILURE}
echo -n $"FAILED"
else
${SETCOLOR_WARNING}
echo -n $"WARNING"
fi
${SETCOLOR_NORMAL}
echo -n "]"
echo
}
#[ $# -eq 0 ] && echo "Usage: `basename $0` {success|failure|warning}"
color hello 333
3.continue函数
#!/bin/bash
#
#********************************************************************
#Author: hanweize
#QQ: 822020480
#Date: 2021-12-29
#FileName: continue.sh
#URL: https://www.hanweize.cn
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
for i in {1..10};do
if [ $i -eq 5 ];then continue;fi
echo $i
done
4.case函数
#!/bin/bash
#
#********************************************************************
#Author: hanweize
#QQ: 822020480
#Date: 2022-05-09
#FileName: case.sh
#URL: www.hanweize.cn
#Description: The test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
sum=0
PS3="请点菜(1-6): "
select MENU in 北京烤鸭 佛跳墙 小龙虾 羊蝎子 火锅 点菜结束;do
case $REPLY in
1)
echo $MENU 价格是 ¥100
let sum+=100
;;
2)
echo $MENU 价格是 ¥88
let sum+=88
;;
3)
echo $MENU价格是 ¥66
let sum+=66
;;
4)
echo $MENU 价格是 ¥166
let sum+=166
;;
5)
echo $MENU 价格是 ¥200
let sum+=200
;;
6)
echo "点菜结束,退出"
break
;;
*)
echo "点菜错误,重新选择"
;;
esac
done
echo "总价格是: $sum"
5.系统初始化脚本
#!/bin/bash
#********************************************************************
#Author: hanweize
#QQ: 822020480
#Date: 2022-05-09
#FileName: reset.sh
#URL: www.hanweize.cn
#Description: The test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
color () {
RES_COL=60
MOVE_TO_COL="echo -en \\033[${RES_COL}G"
SETCOLOR_SUCCESS="echo -en \\033[1;32m"
SETCOLOR_FAILURE="echo -en \\033[1;31m"
SETCOLOR_WARNING="echo -en \\033[1;33m"
SETCOLOR_NORMAL="echo -en \E[0m"
echo -n "$1" && $MOVE_TO_COL
echo -n "["
if [ $2 = "success" -o $2 = "0" ] ;then
${SETCOLOR_SUCCESS}
echo -n $" OK "
elif [ $2 = "failure" -o $2 = "1" ] ;then
${SETCOLOR_FAILURE}
echo -n $"FAILED"
else
${SETCOLOR_WARNING}
echo -n $"WARNING"
fi
${SETCOLOR_NORMAL}
echo -n "]"
echo
}
#关闭SELinux
disable_selinux () {
sed -i '/^SELINUX=/c SELINUX=disabled' /etc/selinux/config
}
#关闭防火墙
disable_firewall (){
systemctl disable --now firewalld
color "防火墙已经关闭" 0
}
#支持光盘,/misc/cd对应就是光盘内容
set_cdrom () {
yum -y install autofs
systemctl enable --now autofs
}
#配置yum 仓库
yum_config (){
mkdir /etc/yum.repos.d/backup
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup/
cat > /etc/yum.repos.d/base.repo <<EOF
[BaseOS]
name=BaseOS
baseurl=file:///misc/cd/BaseOS
https://mirror.tuna.tsinghua.edu.cn/centos/8/BaseOS/x86_64/os/
https://mirrors.huaweicloud.com/centos/8/BaseOS/x86_64/os/
https://mirrors.cloud.tencent.com/centos/8/BaseOS/x86_64/os/
https://mirrors.aliyun.com/centos/8/BaseOS/x86_64/os/
gpgcheck=0
[AppStream]
name=AppStream
baseurl=file:///misc/cd/AppStream
https://mirror.tuna.tsinghua.edu.cn/centos/8/AppStream/x86_64/os/
https://mirrors.huaweicloud.com/centos/8/AppStream/x86_64/os/
https://mirrors.cloud.tencent.com/centos/8/AppStream/x86_64/os/
https://mirrors.aliyun.com/centos/8/AppStream/x86_64/os/
gpgcheck=0
[epel]
name=EPEL
baseurl=https://mirror.tuna.tsinghua.edu.cn/epel/\$releasever/Everything/\$basearch
https://mirrors.cloud.tencent.com/epel/\$releasever/Everything/\$basearch
https://mirrors.huaweicloud.com/epel/\$releasever/Everything/\$basearch
https://mirrors.aliyun.com/epel/\$releasever/Everything/\$basearch
gpgcheck=0
enabled=1
[extras]
name=extras
baseurl=https://mirror.tuna.tsinghua.edu.cn/centos/\$releasever/extras/\$basearch/os
https://mirrors.cloud.tencent.com/centos/\$releasever/extras/\$basearch/os
https://mirrors.huaweicloud.com/centos/\$releasever/extras/\$basearch/os
https://mirrors.aliyun.com/centos/\$releasever/extras/\$basearch/os
gpgcheck=0
enabled=1
[PowerTools]
name=CentOS- - PowerTools - mirrors.aliyun.com
baseurl=https://mirror.tuna.tsinghua.edu.cn/centos/\$releasever/PowerTools/\$basearch/os/
https://mirrors.cloud.tencent.com/centos/\$releasever/PowerTools/\$basearch/os/
https://mirrors.huaweicloud.com/centos/\$releasever/PowerTools/\$basearch/os/
https://mirrors.aliyun.com/centos/\$releasever/PowerTools/\$basearch/os/
gpgcheck=0
enabled=1
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
EOF
color "yum仓库配置成功" 0
}
#修改网卡名称
config_network () {
sed -ri '/GRUB_CMDLINE_LINUX=/s#(.*)"$#\1 net.ifnames=0"#' /etc/default/grub
grub2-mkconfig -o /boot/grub2/grub.cfg
}
#最小化安装系统后,建议安装常用软件
install_packages () {
yum -y install autofs vim-enhanced tcpdump autofs chrony lrzsz tree telnet ftp lftp redhat-lsb-core bash-completion net-tools postfix wget bzip2 zip unzip xz lsof mlocate man-pages rsync
}
set_env () {
cat > /etc/profile.d/env.sh <<EOF
PS1='\[\e[1;36m\][\u@\h \W]\\$\[\e[0m\]'
export EDITOR=vim
export HISTTIMEFORMAT="%F %T "
EOF
}
disable_selinux
disable_firewall
set_cdrom
yum_config
config_network
install_packages
set_env
color "系统初始化已完成!" 0
sleep 2
reboot
#!/bin/bash
#
#********************************************************************
#Author: hanweize
#QQ: 822020480
#Date: 2022-05-09
#FileName: reset.sh
#URL: www.hanweize.cn
#Description: The test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
disable_selinux(){
sed -i.bak 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
echo "SElinux已禁用,重新启动后才可生效"
}
disable_firewall(){
systemctl disable --now firewalld &> /dev/null
echo "防火墙已禁用"
}
set_ps1() {
echo "PS1='\[\e[1;35m\][\u@\h \W]\\$\[\e[0m\]'" > /etc/profile.d/reset.sh
echo "提示符已修改成功,请重新登录生效"
}
set_eth(){
sed -i.bak '/GRUB_CMDLINE_LINUX=/s#"$# net.ifnames=0"#' /etc/default/grub
grub2-mkconfig -o /boot/grub2/grub.cfg &> /dev/null
echo "网络名称已修改成功,请重新启动才能生效"
}
PS3="请选择相应的编号(1-6): "
MENU='
禁用SELinux
关防火墙
修改提示符
修改网卡名
以上全实现
退出
'
select M in $MENU ;do
case $REPLY in
1)
disable_selinux
;;
2)
disable_firewall
;;
3)
set_ps1
;;
4)
set_eth
;;
5)
disable_selinux
disable_firewall
set_ps1
set_eth
;;
6)
break
;;
*)
echo "请输入正确的数字"
esac
done
6.扫描同网段扫描可ping通主机
#!/bin/bash
#********************************************************************
#Author: hanweize
#QQ: 822020480
#Date: 2022-05-09
#FileName: reset.sh
#URL: www.hanweize.cn
#Description: The test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
NET=10.0.0
cat /dev/null > hosts.txt
for i in {1..254};do
if ping -c1 -W1 $NET.$i &> /dev/null ;then
echo $NET.$i is up | tee -a hosts.txt
fi
done
7.扫描主机端口
#!/bin/bash
#********************************************************************
#Author: hanweize
#QQ: 822020480
#Date: 2022-05-09
#FileName: reset.sh
#URL: www.hanweize.cn
#Description: The test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
i=1
host=10.0.0.7
while [ $i -le 65535 ];do
if nc -z $host $i &> /dev/null ;then
echo $i | tee -a port.txt
fi
let i++
done
#!/bin/bash
#********************************************************************
#Author: hanweize
#QQ: 822020480
#Date: 2022-05-09
#FileName: reset.sh
#URL: www.hanweize.cn
#Description: The test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
NET=10.0.0
cat /dev/null > hosts2.txt
for((i=1;i<=254;i++));do
if ping -c1 -W1 $NET.$i &> /dev/null ;then
echo $NET.$i is up | tee -a hosts2.txt
fi
done
8.sum函数
#!/bin/bash
#********************************************************************
#Author: hanweize
#QQ: 822020480
#Date: 2022-05-09
#FileName: reset.sh
#URL: www.hanweize.cn
#Description: The test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
sum=0
for i in {1..100};do
if [ $[i%2] -eq 0 ];then continue;fi
let sum+=i
done
echo $sum
#!/bin/bash
#
#********************************************************************
#Author: hanweize
#QQ: 822020480
#Date: 2022-05-09
#FileName: reset.sh
#URL: www.hanweize.cn
#Description: The test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
sum=0
while read student ;do
age=`echo $student|cut -d" " -f2`
let sum+=age
done < students.txt
echo $sum
9.sleep函数
#!/bin/bash
#********************************************************************
#Author: hanweize
#QQ: 822020480
#Date: 2022-05-09
#FileName: reset.sh
#URL: www.hanweize.cn
#Description: The test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
hello() {
echo hello
sleep 1
hello
}
hello
10.创建用户脚本
#!/bin/bash
#
#********************************************************************
#Author: hanweize
#QQ: 822020480
#Date: 2022-05-09
#FileName: reset.sh
#URL: www.hanweize.cn
#Description: The test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
while [ "$1" ] ;do
useradd $1 && echo $1 is created
shift
done