shell脚本练习题:
1、从1加到100的和:
#!/bin/bash
sum=0
for i in {1..100}
do
sum=$[i+sum]
done
echo "求和结果为:$sum"
[root@localhost data]
[root@localhost data]
总用量 16
-rw-r--r-- 1 root root 59 10月 19 09:39 abc.repo
-rwxr-xr-x 1 root root 87 10月 19 09:23 hh
-rw-r--r-- 1 root root 5 10月 19 10:57 passwd.txt
-rw-r--r-- 1 root root 201 10月 19 09:53 yumd
[root@localhost data]
求和结果为:5050
[root@localhost data]
求和结果为:5050
[root@localhost data]
求和结果为:5050
[root@localhost data]
求和结果为:5050
[root@localhost data]
求和结果为:5050
sum=0
for i in {1..10..2}
do
sum=$[sum+i]
done
echo "10以内的奇数相加:$sum"
10以内的奇数相加:25
sum=0
for ((i=0; i<=100; i++))
do
sum=$[sum+i]
done
echo "100以内的数求和:$sum"
100以内的数求和:5050
i=1
sum=0
while(( $i<=100 ))
do
sum=$[sum+i]
let "i++"
done
echo "1-100求和:$sum"
1-100求和:5050
2、shell脚本自动搭建本地yum仓库:
#!/bin/bash
cd /etc/yum.repos.d
mkdir bak
mv *.repo bak/
echo "[abc]
name=abc
baseurl=file:///mnt
enable=1
gpgcheck=0" > abc.repo
mount /dev/sr0 /mnt
yum clean all
rpm -q dhcp
yum install dhcp -y
3、shell脚本自动化分区,格式化和挂载:
#!/bin/bash
lsblk
fdisk /dev/sdb <<EOF
m
n
p
1
2048
+10G
p
w
EOF
lsblk
mkfs.xfs /dev/sdb1
mount /dev/sdb1 /opt/ky15
df -hT
4、shell脚本查看系统信息:
#!/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" " -f1 --complement`
mem=`free -hm | awk /Mem/'{print $2}'`
space=`lsblk | awk /disk/'{print $4}'|awk -F G '{print $1}'`
G="\e[1;32m"
B="\e[1;34m"
E="\e[0m"
for i in $space
do let sum=$[$sum+$i]
done
echo -e "$B主机名:$E$G\t$name\t$E"
echo -e "${B}IP地址:$E$G\t$ip\t$E"
echo -e "$B系统版本:$E$G\t$os\t$E"
echo -e "$B内核版本:$E$G\t$kernel\t$E"
echo -e "${B}cpu名称:$E$G\t$cpu\t$E"
echo -e "$B内存总大小$E:$G\t$mem\t$E"
echo -e "$B硬盘总大小$E:$G\t${sum}G\t$E"
5、猜拳游戏(剪刀,石头,布)
#!/bin/bash
echo "游戏规则:
玩家出石头:输入: 1
玩家出剪刀:输入: 2
玩家出布 :输入: 3
"
echo "-----------------猜拳游戏开始---------------"
num=$[RANDOM%3]
read -p "请选择 1-3:" i
case $i in
1)
echo "你出的是石头"
if [ $num -eq 0 ]
then
echo "计算机出石头"
echo "平 局"
elif [ $num -eq 1 ]
then
echo "计算机出剪刀"
echo "你赢了"
else
echo "计算机出布"
echo "计算机赢了"
fi;;
2)
echo "你出的是剪刀"
if [ $num -eq 0 ]
then
echo "计算机出剪刀"
echo "平 局"
elif [ $num -eq 1 ]
then
echo "计算机出布"
echo "你赢了"
else
echo "计算机石头"
echo "计算机赢了"
fi;;
3)
echo "你出的是布"
if [ $num -eq 0 ]
then
echo "计算机出布"
echo "平 局"
elif [ $num -eq 1 ]
then
echo "计算机出石头"
echo "你赢了"
else
echo "计算机出剪刀"
echo "计算机赢了"
fi;;
*)
echo "请输入正确的数字:(提示:输入1、2、3)"
esac
6、判断成绩:
read -p "请输入你的成绩(0-100) : " grade
if [ $grade -ge 90 ] && [ $grade -le 100 ]
then
echo "你真是个小天才"
elif [ $grade -ge 80 ] && [ $grade -le 89 ]
then
echo "考的还行,,良好"
elif [ $grade -ge 60 ] && [ $grade -le 79 ]
then
echo "你及格了"
else
echo "准住备罚抄吧,你不及格"
fi
7、猜数字游戏:
#!/bin/bash
num=$[RANDOM%100+1]
i=1
count=10
while(( $i<=10 ))
do
read -p "----计算机生成了一个随机数(你有10次机会猜出这个数)----:" n
let "count--"
echo -e "你还有 "$count" 次机会哦!\n"
if [ $n -eq $num ]
then
echo "恭喜你,猜对啦"
exit
elif [ $n -gt $num ]
then
echo -e "sorry,你猜大了\n"
else
echo -e "sorry,你猜小了\n"
fi
let "i++"
done
8、打印九九乘法表:(正序和倒序打印)
#!/bin/bash
for j in {1..9}
do
for i in `seq $j`
do
echo -e "$i*$j=$[i*j]\t\c"
done
echo
done
for j in {9..1..-1}
do
for i in `seq $j`
do
echo -e "$i*$j=$[i*j]\t\c"
done
echo
done
echo
echo "用 for 的另一种格式循环输出99乘法表:"
for(( j=1;j<=9;j++ ))
do
for(( i=1;i<=j;i++ ))
do
echo -e "$i*$j=$[i*j]\t\c"
done
echo
done
9、shell脚本批量改文件名:
#!/bin/bash
cd /root/aa
touch {1..10}.txt
for i in *
do
name=`echo $i | cut -d "." -f1`
mv $i ${name}.bak
done
10、查看dd 命令复制磁盘要多久:
#!/bin/bash
date
dd if=/dev/zero of=/root/sh/1.txt bs=10M count=5
date
11、shell脚本自动创建多个用户:
#!/bin/bash
i=1
while(( $i<=10 ))
do
useradd stu$i
echo "123456" | passwd --stdin stu$i &>/dev/null
let "i++"
done