SFD2008 in GuiZhou University Eddan

Friday Mar 27, 2009

一、循环

for循环

for i in list

do

    commands

done

eg.下面的脚本遍历当前目录中的所有文件。对于每个文件,都运行proof脚本5次。

for  FILENAME in *

do

    echo "Printing 5 copies of  $FILENAME"

    for  x  in 1  2  3  4  5

    do 

             proof   $FILENAME

     done

done

while 与 until 循环

while  testcommand

do

     commandlist

done

eg.打印整数1至10的平方值

i=1

while  [  $i -le 10  ]

do

     expr  $i   \*   $i

     i='expr  $i  + 1 '

done

用select 命令打印菜单

#!/bin/bash

#   startMenu  -  Provide  a  menu  of common  actions.

PS3='What  would  you like  to do? (enter 1-3)  '

select ACTION in "Read Mail with Pine"  "Start  XWindows"  "Exit  this  Menu"

do

     case  $ACTION  in

     "Read  Mail  with  Pine")

         # run  the  pine  mailreader;  return  to this  menu  when done

         pine

         ;;

     "Start XWindows")

          # start XWindows,and do not return to this script

          # replace this process with the X process

          exec  startx

          ;;

      "Exit  this Menu")

           echo  "Returning to your login shell."

           break

           ;;

       *)

           echo "Response not recognized,try again."

           ;;

        esac

done

二、Shell 的输入与输出

echo命令

                        echo转义字符             说明

                        \b                               退格

                        \c                               打印不换行

                        \f                                换页

                        \n                               换行

                        \r                                回车

                        \t                                 制表

                        \v                               垂直制表

                       \\                                 反斜线

read命令

read命令从标准输入读取一行数据并将其保存到一个或多个Shell变量中,eg

   echo  "Enter  your  name."

   read   NAME

   echo  "Hello, $NAME"

即时文档

    “即时文档”功能是将多行输入数据提供给Shell脚本中的一个或多个命令,并保留输入数据中的换行符。eg.

     echo  "Reminder:  team  meeting  is  in  one  hour," > message

     echo  "in the second floor meeting room." >> message

     echo  "Please reply if you can't make it." >> message

      mail dbp etch a-liu < message

      rm message

创建函数

   在ksh 与 bash中,可以创建函数。

   function  factorial {

       n=$1

       FACT=1

       while  [  $n  -gt  0  ]

       do

             FACT='expr  $FACT  \*  $n'

             n='expr  $n  - 1'

             done

             echo  "$1  factorial  is  $FACT"

}

for NUM  in  $*

do

       factorial  $NUM

done

Comments:

Thanks for your information, i have read it, very good!

Posted by Tiffany Bangles on November 14, 2009 at 09:53 AM CST #

Post a Comment:
  • HTML Syntax: NOT allowed