shell编程(bash shell)

一.特殊符合 1  | 管道符号 2  >重定向 3  >>重定向 4  2>重定向错误输出 5  >/dev/null  2>&1 6  *匹配任意字符     ?一个字符 7   \  转义字符 3\*5 8   ;   命令分隔符 9.  “”把内容做为普通字符输出  有几个除外  $    ``   ‘’  10  & 后台进程符 11   &&  逻辑与 12   || 逻辑或 13   !逻辑非  排除指定范围  ls  a[!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    例:   vi  11 内容以下  #!/bin/bash clear tput cup 10 40 echo -n  "please type in the username:" tput cup 11 40 read  AA if   grep   $AA  /etc/passwd >/dev/null then tput cup  12 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 本身写一个小程序 System  manage ************************ 1.       show  the  user 2.       test  the 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 ID:" 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
相关文章
相关标签/搜索