mindong 发表于 2018-8-25 06:46:06

shell 的一些案例以及语法

  #大于95 输出yes
  BootUsage=`df -hT | grep "/boot" | awk '{print $6}' | cut -d "%" -f 1`
  [ $BootUsage -gt 95 ] && echo "YES"
  #如果输入的是/etc/inittab 那么输出YES
  read -p "Location:" FilePath
  [ $FilePath = "/etc/inittab" ] && echo "YES"
  #/boot文件夹大于80%执行
  #!/bin/bash
  RATE=`df -hT | grep "/boot" | awk '{print $6}' | cut -d "%" -f1 `
  if[$RATE-gt80]
  then
  echo "Warning,DISK is full!"
  fi
  #检测mysql服务是否启动停止就启动
  #!/bin/bash
  service mysqld status &> /dev/null
  if[$?-eq0]
  then
  echo"mysqld service is running."
  else
  /etc/init.d/mysqldrestart
  fi
  #超过100的用户打印
  #!/bin/bash
  DIR="/opt"
  LMT=100
  ValidUsers=`grep "/bin/bash" /etc/passwd | cut -d ":" -f 1`
  for UserNamein$ValidUsers
  do
  Num=`find $DIR -user $UserName | wc -l`
  if[$Num-gt$LMT];then
  echo "$UserName have $Num files."
  fi
  done
  #while语法
  while可用内存 /dev/null
  i=`expr $i + 1`或者用lee i++
  done
  #删除20个用户
  #!/bin/bash
  i=1
  while[$i-le20]
  do
  userdel -r stu$i
  i=`expr $i + 1`
  done
  #创建启动脚本
  #!/bin/bash
  case   $1   in
  start)
  echo"Start MySQL service."
  ;;
  stop)
  echo"Stop MySQL service."
  ;;
  *)
  echo"Usage:$0start|stop"
  ;;
  esac
  #判断输入的是字符还是字母
  #!/bin/bash
  read-p"Press some key, then press Return:“KEY
  case"$KEY“in
  |)
  echo "It's a letter."
  ;;
  )
  echo "It's a digit."
  ;;
  *)
  echo "It's function keys、Spacebar or other keys. "
  esac

页: [1]
查看完整版本: shell 的一些案例以及语法