shell script - VIII class case, while, until

for:
  for variables in the list; do
    loop
  done

  for ((initial statement; Analyzing statement; value change statement)); do
    loop
  done

case:
  branch
  case variable in
  PAT1)
    executing a statement
    ;;
  PAT2)
    executing a statement
    ;;
  *)
    default execution statement
    ;;
  esac

while:
  loop
  format:

  Definition of the initial value
  while judging condition; do
    loop
    value change statements
  done

  Other two special statement format:
  for $ I in (CAT / etc / the passwd &> / dev / null) writing errors

  while read variable; do
    loop ($ variable)
  DONE << / path / filename

  Is capricious - to write an infinite loop
  while true; do
    loop
  DONE

  BREAK out of the loop
  continue to jump out of this cycle
  after sleep extension of how long to continue to execute the command down

until:
  circulation
  and usage would like to use while
  the difference: only judge when the condition is False, will enter the loop;
  . .

IF:
  IF condition judgment; then
    execute the statement
  Fi

  IF condition is determined; then
    execute statement. 1
  the else
    execute statements 2
  Fi

  IF condition is determined; then
    execute the statement. 1
  elif condition judgment; then
    execute the statement 2
  elif condition judgment; then
    execute statement. 3
  the else
    execute statements 4
  fi

---------------------------------------------------------------------------

回顾:
  1、sed
    行编辑
    sed [option]... {sed固定的语法格式} file ...
    option : -i -n -r -e -f -l
    固定语法格式:
      address地址定界
        1 第一行
        1,10 第一行到第十行
        5,$ 指定行尾
          【vim . 代表光标所在当前行 : ":.,$s/^/#/"】
        1~3 1,4,7,11... 指定步长
        /pattern1/,/pattern2/
          从模式1匹配到的行,到模式2匹配到的行
        数字,+N
          【vim ":.,+3s/^#//"】
        /pattern1/,+N
      sed的常见子命令
        p 一直与-n一起使用
        w 指定保存到新的文件
        c 替换后面指定的内容
        i 在匹配行的上一行去添加指定内容
          sed "/for/i \abc" 99xfb.sh
        a 在匹配行的下一行添加指定内容
          sed "/for/a \abc" 99xfb.sh
        d 删除
          sed "/for/d" 99xfb.sh
        s 替换 -- 格式:s/pattern/字符串内容/ 默认:sed只替换每行中匹配的第一内容
        g -- 全部替换
          \1 \2 \3 \4
        & -- 在 字符串 内容中应用 pattern 匹配到的内容

  2、常见的逻辑关系语句
    掌握 if case while for until
    判断:1、test [] [[ && || ]] 2、指定命令( `命令` $(命令) )    需求:写一个脚本,使用chkconfig 命令,循环执行,关闭所有在5等级上的服务;

      chkconfig --level 5 [name] off
      [name]的遍历

for i in `chkconfig --list | awk '{print $1}'`;do
   if chkconfig --list $i | grep "5:off" &> /dev/null ; then
     echo "$i 这个服务已经关闭"
  else
    chkconfig --level 5 $i off
    echo "关闭了 $i 这个服务"
  fi
 done

练习:
  写一个脚本,完成如下任务
  1、显示一个如下的菜单
    Cpu)显示cpu信息
      cat /proc/cpuinfo
    Mem)显示内存信息
      free -m
    Disk)显示磁盘信息
      fdisk -l
    Quit)退出
      quit
  cat

  2、提示用户选项
    read -p "xxx"
  3、那些是用户选项的内容
    while

  if语句的写法:
  case语句的写法:

  read

  cpu
    cat /proc/cpuinfo
  mem
    free -m
  disk
    fdisk -l
  quit
    echo ""
    exit 0

Guess you like

Origin www.cnblogs.com/zwl123456/p/11442038.html