shell脚本实例2

11 监控linux磁盘根分区,如果根分区空间大于等于40%,发送邮件给Linux SA

[root@localhost script]# cat disk
#!/bin/bash
while sleep 1m
do
 for i in `df -h |sed -n '/\/$/p'|awk '{print $5}'|sed 's/\%//g'`
   do echo $i
      if [ $i -ge 40 ]
      then
      echo "more than 40% linux of disk space,"
      fi
   done
done[root@localhost script]# bash disk
63
more than 40% linux of disk space,

12 如何判断某个目录是否存在,不存在则新建,存在则打印信息

[root@localhost script]# bash dir
[root@localhost script]# ls
1-5  cc       dir   file   fun-name    nomima  read  user1
aa   compare  disk  for    login       nopwd   test  while
abc  curuser  echo  fun-1  networkcfg  passwd  user  yesno

[root@localhost script]# bash dir
the directory already exists,please exit
[root@localhost script]# cat dir
#!/bin/bash
if [ ! -d /home/script/test ]
then mkdir /home/script/test
else echo "the directory already exists,please exit"
fi


13

[root@localhost script]# cat >if-1
#!/bin/bash
echo -e "are you ok(y/n or maybe)? \c"
read answer
if [[ $answer == [yY]* || $answer = maybe ]]
then echo "glad to hear it"
fi
[root@localhost script]# bash if-1
are you ok(y/n or maybe)? dk
[root@localhost script]# bash if-1
are you ok(y/n or maybe)? y
glad to hear it
[root@localhost script]# bash if-1
are you ok(y/n or maybe)? Y
glad to hear it
[root@localhost script]# bash if-1
are you ok(y/n or maybe)? maybe
glad to hear it


14

[root@localhost script]# cat >if-2
#!/bin/bash
echo -e "how old are you? \c"
read age
if (( age < 0 || age > 120 ))
then echo "you are so old"
elif (( age >=0 && age <= 12 ))
then echo "you are child"
elif (( age >=13 && age <=19 ))
then echo "you are 13-19 years old"
elif (( age >=20 && age <=29 ))
then echo "you are 20-29 years old"
elif (( age >=30 && age <=39 ))
then echo "you are 30-39 years old"
else echo "you are above 40"
fi
[root@localhost script]# bash if-2
how old are you? 30
you are 30-39 years old
[root@localhost script]# bash if-2
how old are you? 28
you are 20-29 years old
[root@localhost script]# bash if-2
how old are you? 400
you are so old
[root@localhost script]# bash if-2
how old are you? 0
you are child


你可能感兴趣的:(shell,脚本实例)