目录
1.for求和
#!/bin/bash sum=0 for i in {1..100} do sum=$[i+sum] done echo "求和结果是$sum"
2.搭建本地yum源
[root@localhost ~]# vim yum.sh #!/bin/bash mkdir /111 mount /dev/sr0 /111 &>/dev/null#重定向 cd /etc/yum.repos.d mkdir bak mv *.repo bak echo "[abc] name=centos7 baseurl=file:///111 gpgcheck=0">abc.repo #将内容导入abc.repo yum clean all &>/dev/null #清理缓存,弹出信息导入空 yum install httpd -y #安装一个httpd测试
3.提取IP地址
#提取自己机器的IP地址 [root@localhost ~]# ifconfig ens33|awk /netmask/'{print$2}'
4.查看机器基本信息
#!/bin/bash name=`hostname` ip=`ifconfig ens33|awk /netmask/'{print $2}'` os=`cat /etc/redhat-release` kernel=`uname -r` cpu=`lscpu|grep 型号名称:|tr -s " "|cut -d":" -f2` mem=`free -hm |awk /Mem/'{print $2}'` space=`lsblk |awk /disk/'{print $4}'|awk -F G '{print $1}'` for i in $space do let sum=$[$sum+$i] done echo "主机名:$name" echo "IP地址:$ip" echo "系统版本:$os" echo "内核版本:$kernel " echo "CPU名称:$cpu" echo "内存总大小:$mem " echo "硬盘总大小:${sum}G "
5.判断当前centos系统版本
#!/bin/bash #判断用户centos版本 echo "$menu" if grep -i -q "centos linux 6" /etc/os-release then echo "此系统是centos 6" elif grep -i -q "centos linux 7" /etc/os-release then echo "此系统是centos 7" else echo "此系统是centos 8" fi
6.磁盘分区
#!/bin/bash fdisk /dev/sdb << EOF n p 1 +10G w EOF mkdir /data echo "/dev/sdb1 /data xfs defaults 0 0" >> /etc/fstab mkfs.xfs /dev/sdb1 mount -a
7.DNS正向解析
#!/bin/bash #检测YUM源仓库是否挂载 df | grep "sr0" if [ $? -eq 0 ] then yum -y install bind &> /dev/null #若已挂载则直接安装bind软件包 else mount /dev/cdrom /mnt &> /dev/null yum -y install bind &> /dev/null #若未挂载,则先挂载,再安装bind软件包" fi #关闭防火墙和强制访问控制安全系统 systemctl stop firewalld &> /dev/null systemctl disable firewalld &> /dev/null setenforce 0 &> /dev/null #修改主配置文件 sed -i 's/127.0.0.1/any/' /etc/named.conf sed -i 's/localhost/any/' /etc/named.conf #修改区域配置文件,添加正向区域配置 sed -i 's/localhost.localdomain/rui.com/' /etc/named.rfc1912.zones sed -i 's/named.localhost/rui.com.zone/' /etc/named.rfc1912.zones sed -i '18,100d' /etc/named.rfc1912.zones #配置正向区域数据文件 cd /var/named cp -p named.localhost rui.com.zone sed -i 's/@/rui.com./g' /var/named/rui.com.zone sed -i 's/rname.invalid./admin.rui.com./' /var/named/rui.com.zone sed -i 's/127.0.0.1/192.168.254.10/' /var/named/rui.com.zone sed -i '9a www IN A 192.168.254.10' /var/named/rui.com.zone sed -i '/AAAA/d' /var/named/rui.com.zone sed -i '/::1/d' /var/named/rui.com.zone #启动服务 systemctl start named systemctl restart named
8.石头剪刀布游戏
#使用shell脚本写出石头剪刀布游戏 #!/bin/bash echo "选择你出什么" echo "1.石头" echo "2.剪刀" echo "3.布" num=`echo $[RANDOM%3+1]` read -p "请选择1-3:" key case $key in 1) if [ $num -eq 1 ] then echo "平局" elif [ $num -eq 2 ] then echo "机器胜利" else echo "人类胜利" fi ;; 2) if [ $num -eq 2 ] then echo "平局" elif [ $num -eq 3 ] then echo "机器胜利" else echo "人类胜利" fi ;; 3) if [ $num -eq 3 ] then echo "平局" elif [ $num -eq 1 ] then echo "机器胜利" else echo "人类胜利" fi ;; *) echo "输入错误" esac
9.ping IP
#!/bin/bash ip=192.168.254. for i in {1..254} do { ping -c3 -W3 $ip$i &>/dev/null if [ $? -eq 0 ] then echo "$ip$i ok" fi }& done