liujjun 发表于 2018-8-20 12:38:50

linux里的shell编程(bash shell)

  一.特殊符合
  1| 管道符号
  2>重定向
  3>>重定向
  42>重定向错误输出
  5>/dev/null2>&1
  6*匹配任意字符   ?一个字符
  7   \转义字符 3\*5
  8   ;   命令分隔符
  9.“”把内容作为普通字符输出有几个除外$    ``   ‘’
  10& 后台进程符
  11   &&逻辑与
  12   || 逻辑或
  13   !逻辑非排除指定范围lsa[!2-4]
  14   ``把他们所包含的内容作为命令去执行.
  二.循环
  1.       for循环。
  For变量名
  In   数值列表(值1,值2,……)(可省略)
  Do
  语句
  Done
  2.       while循环。
  While   命令/条件
  Do
  语句
  Done      while条件成功则执行do,否则。。。。
  3.       if语句
  if 命令/条件    若为真,则执行then后语句
  then
  语句
  Else 语句(改句话可以没有)
  Fi
  例:   vi11 内容如下
  #!/bin/bash
  clear
  tput cup 10 40
  echo -n"please type in the username:"
  tput cup 11 40
  readAA
  if   grep   $AA/etc/passwd >/dev/null
  then
  tput cup12 40
  echo"$AA is a vilid user!"
  else
  tput cup 12 40
  echo   "$AA is not a vilid user!"
  fi
  4.         if 命令/条件               若条件成功,则执行1;若不成功,则根据2判断,
  then                     成功,则执行2;否则,执行语句3
  语句1
  Elif条件/命令2
  Then
  语句2
  Else
  语句3
  Fi
  5.       case 语句(多条件判断)             (系统中例子有很多)
  case$变量名in
  值1)语句1
  ;;
  值2)语句2
  ;;
  值3)语句3
  ;;
  值4)语句4
  ;;
  Esac
  自己写一个小程序
  Systemmanage
  ************************
  1.       showtheuser
  2.       testthe network
  3.       show the PID
  4.       kill the process
  5.       shutdown the system
  6.       reboot the system
  0.      exit
  ************************
  Please type in the optin:_
  答案:
  #!/bin/bash
  clear
  tput cup 2 28
  echo "System Manage"
  tput cup 3 24
  echo "******************"
  tput cup 4 24
  echo "1.show the user"
  tput cup 5 24
  echo "2.test the network"
  tput cup 6 24
  echo "3.show the PID"
  tput cup 7 24
  echo "4.kill the process"
  tput cup 8 24
  echo "5.shutdown the system"
  tput cup 9 24
  echo "6.reboot the system"
  tput cup 10 24
  echo "0.exit"
  tput cup 11 24
  echo "******************"
  tput cup 12 24
  echo -n "Please type in the option:"
  read AA
  case $AA in
  1)w
  ;;
  2)tput cup 13 24
  echo -n "please type in the IP:"
  read BB
  if ping -c1 $BB >/dev/null 2>&1
  then
  tput cup 14 24
  echo "chenggong"
  else
  tput cup 14 24
  echo "shibai"
  fi
  ;;
  3)tput cup 13 24

  echo -n "Please type in the terminal>  read CC
  ps -t $CC
  ;;
  4)tput cup 13 24
  echo -n "please type in the PID:"
  read DD
  kill -9$DD
  ;;
  5)init 0
  ;;
  6)init 6
  ;;
  0)exit
  ;;
  *)tput cup 13 24
  echo -n "input error,please retry!"
  ;;
  esac
  read AAA

页: [1]
查看完整版本: linux里的shell编程(bash shell)