脚本1:
#!/bin/bash
#统计根分区使用率
rate =$(df -h | grep "/dev/sda5" |awk '{print $5}' | cut -d "%" -f 1)
#显示根分区,再筛选出包含/dev/sda5的行,再筛选出第5列,最后以%为分隔符,筛选出第1列
if [ $rate -ge 80 ]
then
echo "waring"
fi
脚本2:
#!/bin/bash
#使用for 打印in后面列举的内容
for time in morning noon afternoon evening
do
echo "this time is $time"
done
结果:
this time is morning
this time is noon
this time is afternoon
this time is evening
脚本3
#!/bin/bash
cd /home/liy/shell #切换到/home/liy/shell目录下
ls *.sh >ls.log #将该目录下的.sh文件放入sh.log中
y=1
#统计ls.log中.sh文件个数
for i in $(cat ls.log)
do
echo $y
y=$(($y+1))
done
结果:
1
2
3
4
5
6
7
8
9
10
脚本4:
#!/bin/bash
#从1加到100
s=0
for ((i=1;i<=100;i=i+1))
do
s=$(($s+$i))
done
echo "the sum of 1+2+..+100 is: $s"
结果:
the sum of 1+2+..+100 is: 5050
脚本5:
#/bin/bash
#从1加到100
i=1
s=0
while [ $i -le 100 ]
#当i小于等于100时,执行以下语句
do
s=$(($s+$i))
i=$(($i+1))
done
echo "the sun is: $s"
结果:
the sun is: 5050
脚本6:
#/bin/bash
#从1加到100
i=1
s=0
until [ $i -gt 100 ]
#当i大于等于100时,不执行以下语句
do
s=$(($s+$i))
i=$(($i+1))
done
echo "the sun is: $s"
结果:
the sun is: 5050
脚本7:
#!/bin/bash
#判断用户输入
read -p "please choose yes/no" -t 30 cho #等待30s将输入值赋给变量cho
case $cho in
"yes")
echo "yes!"
;;
"no")
echo "no!"
;;
*)
echo "error!"
;;
esac
结果:
please choose yes/noyes
yes!