您的位置:首页 > 运维架构 > Shell

linux 常用shell

2015-06-23 17:09 435 查看
#!/bin/bash

#这个脚本用于判断服务是否正常启动

#作者:xuehen

port=$(nmap -sT 10.0.1.99|grep tcp|grep http |awk '{print $2}')

if [ "$port" == "open" ]

    then

      echo "$(date) apache is running" > /root/log_acc

     else

          service httpd restart

      echo "$(date) service httpd restart" > /root/log_err
fi

##########################################

#!/bin/bash

read -p "please choice yes/no :" -t 30 cho

   case $cho in 

           "yes")

         echo "your choose is yes"

             ;;

           "no")

            ;;

       *)

       echo "your choose is wrong"

            ;;
      esac

#####################################

#!/bin/bash

#该脚本为多分支if案例

#-p:提示用户输入信息

#-s:用于加密,输入的内容不显示

#-t:等待用户输入的时间

read -p "please input a file_name:" file

 if [ -z "$file" ]

   then 

     echo "your put is empty"

  elif [ -f "$file" ]

   then 

       echo "your put is regeular file"

  elif [ -d "$file" ]

     then

         echo "your put is document"

   elif [ ! -e "$file" ]

     then 

        echo "your put is not exist"
 fi

######################################

#!/bin/bash

date=$(date +%y%m%d)

size=$(du -sh /etc )

if  [ -e /tmp/dbbak ]

then

   echo "Date:$date" > /tmp/dbbak/db.txt

   echo "Size:$size" >> /tmp/dbbak/db.txt

 cd /tmp/dbbak

  tar -zcf etc_"$date"_tar.gz  /etc  /tmp/dbbak/db.txt &>/dev/null

  rm -rf /tmp/dbbak/db.txt 

else 

  mkdir /tmp/dbbak

  echo "Date:$date" > /tmp/dbbak/db.txt

   echo "Size:$size" >> /tmp/dbbak/db.txt

 tar -zcf etc_"$date"_tar.gz  /etc  /tmp/dbbak/db.txt &>/dev/null

 rm -rf /tmp/dbbak/db.txt
fi 

###############################################

#!/bin/bash

s=0

for ((i=1;i<=100;i++))

  do 

   s=$(($s+$i))

done
 echo  "the result is:" $s

###################################

#!/bin/bash

for time in moring  noon after evening

   do 

       echo "Now is $time"

 done

#!/bin/bash

for time in morning noo afternoon evening

      do 

         echo "Now is $time!"
      done

###############################################

#!/bin/bash

rate=$(df -h |grep /dev/sda5|awk '{print $5}'|cut -d"%" -f1)

  if [ $rate -ge 4 ]

    then 

   echo "warining !!! Date:$(date) is full" 

      mail -s " rate is used " xuehen < /etc/passwd

     exit 1
 fi 

#################################

#!/bin/bash

read -p "please input four parameters that is number:"

for x in "$*"

    do 

          echo "$*"    

    done

for y in "$@"

    do 

           echo $y
  done

####################################

#!/bin/bash

#字符串判断

#-z:为空返回真

read -p "please input your name:" $name

  if [ -z = $1 ] ;then

 

       echo -e "\e[1;34m the put is empty \e[0m"

      

 fi
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: