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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章