一叶笑天
雄关漫道真如铁, 而今迈步从头越。 从头越, 苍山如海, 残阳如血。
posts - 73,comments - 7,trackbacks - 0
 

Korn Shell

Korn和Bash shells 非常相似.

Korn语法和结构:

Shbang

"shbang" 是脚本起始行,告诉kernel那个shell解析. #!位于行头。例如 #!/bin/ksh

注释

行注释用#符号.例如:

# This program will test some files

通配符

*,?, 和 [ ]用于文件名扩展.例如<, >, 2>, >>, 和 | 用于IO和重定向. 为了保证这些符号不被解析,这个字符要被引起来。 例如:rm *; ls ??;  cat file[1-3];

echo "How are you?"

输出显示

输出屏幕echo和print,例如:

echo "Who are you?"

print "How are you?"

局部变量

局部变量作用于当前shell,shell结束时局部变量失效.例如

variable_name=value

typeset variable_name=value

name="John Doe"

x=5

全局变量

全局变量也称为环境变量. 例如:

export VARIABLE_NAME =value

export PATH=/bin:/usr/bin:.

从变量中提取值

使用$.例如:

echo $variable_name

echo $name

echo $PATH

读取用户输入

使用read。例如:

read name?"What is your name?"

The prompt is in quotes. After it is displayed, the read command waits for user input

print -n "What is your name?"

read name

read name1 name2 ...

参数

可以从命令行传入参数。位置参数用于从脚本中接收值。例如:

At the command line:

$ scriptname arg1 arg2 arg3 ...

In a script:

echo $1 $2 $

位置参数, $1 分配为 arg1, $2 is 分配为arg2, ...

echo $*

所有位置参数

echo $#

位置参数号

数组

Bourne shell 利用位置参数创建字符列表.除位置参数外,Korn shell也支持数组语法,起始位置为0. Korn shell数组用set –A命令创建.例如

set apples pears peaches

位置参数

print $1 $2 $3

$1 is apples, $2 is pears, $3 is peaches

set -A array_name word1 word2 word3 ...

set -A fruit apples pears plums

Array

print ${fruit[0]}

Prints apple

${fruit[1]} = oranges

Assign a new value

算术

Korn shell 支持整数算术.typeset i命令会声明一个整数类型变量. Integer算术能够在变量上完成。否则,(( )) 语法 (let command)用于算术操作。例如:

typeset -i variable_name

声明integer

typeset -i num

num=5+4

num is declared as an integer

print $num

Prints 9

(( n=5 + 5 ))

The let command

print $n

Prints 10

命令替换

像C/TC shells 和Bourne shell,Korn shell提供一种新的语法,将命名放在()中,前面加$.例如:

variable_name=`command`

variable_name=$( command )

echo $variable_name

echo "Today is `date`"

echo "Today is $(date)"

操作符

Korn shell使用内建的test命令操作符,类似于C 语言操作符.例如:

相等性:

比较性:

=

string, equal to

greater than

!=

string, not equal to

>=

greater than, equal to

==

number, equal to

less than

!=

number, not equal to

<=

less than, equal to

逻辑性:

&&

and

||

Or

!

Not

条件语句

if 语句条件放在()。then关键字位于()后. If用fi结束. [[ ]] 用于模式匹配. [ ]用于兼容Bourne shell. Case命令是另外一种if/else.例如:

The if construct is:

if command

then

   block of statements

fi

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

if [[ string expression ]]

then

   block of statements

fi

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

if (( numeric expression ))

then

   block of statements

fi

The if/else construct is:

if command

then

   block of statements

else

   block of statements

fi

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

if [[ expression ]]

then

   block of statements

else

   block of statements

fi

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

if (( numeric expression ))

then

   block of statements

else

   block of statements

fi

The case construct is:

case variable_name in

   pattern1)

      statements

         ;;

   pattern2)

      statements

         ;;

   pattern3)

         ;;

esac

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

case "$color" in

   blue)

      echo $color is blue

         ;;

   green)

      echo $color is green

         ;;

   red|orange)

      echo $color is red or orange

         ;;

esac

The if/else/else if construct is:

if command

then

   block of statements

elif command

then

   block of statements

elif command

then

   block of statements

else

   block of statements

fi

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

if [[ string expression ]]

then

   block of statements

elif [[ string expression ]]

then

   block of statements

elif [[ string expression ]]

then

   block of statements

else

   block of statements

fi

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

if (( numeric expression ))

then

   block of statements

elif (( numeric expression ))

then

   block of statements

elif (( numeric expression ))

then

   block of statements

else

   block of statements

fi

循环

四种类型循环: while, until, for, 和 select.

while循环 跟随do。

until循环。

for循环。

select loop is used to provide a prompt (PS3 variable) and a menu of numbered items from which the user inputs a selection The input will be stored in the special built-in REPLY variable. The select loop is normally used with the case command.

循环控制命令,例如:

while command

do

   block of statements

done

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

while [[ string expression ]]

do

   block of statements

done

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

while (( numeric expression ))

do

    block of statements

done

until command

do

block of statements

done

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

until [[ string expression ]]

do

   block of statements

done

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

until (( numeric expression ))

do

   block of statements

done

for variable in word_list

do

   block of statements

done

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

for name in Tom Dick Harry

do

   print "Hi $name"

done

select variable in word_list

do

   block of statements

done    

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

PS3="Select an item from the menu"

for item in blue red green

   echo $item

done

Shows menu:

  1. blue
  2. red
  3. green

文件测试

Korn shell使用test command来评估表达式,例如:

-d

File is a directory

-a

File exists and is not a directory

–r

Current user can read the file

–s

File is of nonzero size

–w

Current user can write to the file

–x

Current user can execute the file

Example 2.5.

   #!/bin/sh

1   if [ –a file ]

    then

      echo file exists

   fi

2    if [ –d file ]

    then

        echo file is a directory

   fi

3   if [ -s file ]

    then

       echo file is not of zero length

   fi

4   if [ -r file -a -w file ]

    then

       echo file is readable and writable

   fi

函数

函数容许定义一段shell,而且给这段代码给一个名字.有两种格式:一种来自于Bourne shell,另一种来自于Korn shell.例如:

function_name() {

   block of code

}

function function_name {

   block of code

}

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

function lister {

   echo Your present working directory is `pwd`

   echo Your files are:

   ls

}

Korn Shell脚本:

例子

1   #!/bin/ksh

2   # The Party Program––Invitations to friends from the "guest" file

3   guestfile=~/shell/guests

4   if [[ ! –a "$guestfile" ]]

    then

        print "${guestfile##*/} non–existent"

        exit 1

    fi

5   export PLACE="Sarotini's"

6   (( Time=$(date +%H) + 1 ))

7   set -A foods cheese crackers shrimp drinks "hot dogs" sandwiches

8   typeset -i n=0

9   for person in $(< $guestfile)

    do

10      if [[ $person = root ]]

        then

              continue

        else

              # Start of here document

11           mail –v –s "Party" $person <<- FINIS

             Hi ${person}! Please join me at $PLACE for a party!

             Meet me at $Time o'clock.

             I'll bring the ice cream. Would you please bring

             ${foods[$n]} and anythin else you would like to eat? Let

             me know if you can make it.

                   Hope to see you soon.

                            Your pal,

                            ellie@`hostname`

             FINIS

12           n=n+1

13           if (( ${#foods[*]} == $n ))

             then

14             set -A foods cheese crackers shrimp drinks "hot dogs"

               sandwiches

             fi

          fi

15 done

    print "Bye..."

解释:

  1. 让Kernal知道在运行Korn shell script.
  2. 注释

3.    变量guestfile被设置为文件的全路径名,叫做guests.

  1. 行读入
  2. 环境变量.
  3. the hour of the day指定给变量Time.
  4. 数组foods赋值,使用 set –A 命令.项开始索引0.
  5. typeset –i 命令创建integer值.
  6. For循环.
  7. 条件测试.
  8. The mail message is sent. The message body is contained in a here document.
  9. 变量n增加1.
  10. 如果数组中的元素号等于变量值,则到达了数据末端.
  11. 结束循环.
posted on 2008-06-30 10:25 一叶笑天 阅读(408) 评论(0)  编辑  收藏 所属分类: Shell技术

只有注册用户登录后才能发表评论。


网站导航: