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:
|
|||||
文件測試 |
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..."
解釋:
- 讓Kernal知道在運行Korn shell script.
- 注釋
3. 變量guestfile被設置為文件的全路徑名,叫做guests.
- 行讀入
- 環境變量.
- the hour of the day指定給變量Time.
- 數組foods賦值,使用 set –A 命令.項開始索引0.
- typeset –i 命令創建integer值.
- For循環.
- 條件測試.
- The mail message is sent. The message body is contained in a here document.
- 變量n增加1.
- 如果數組中的元素號等于變量值,則到達了數據末端.
- 結束循環.