ºìÁªLinuxÃÅ»§
Linux°ïÖú

Shell Script¼ò½é

·¢²¼Ê±¼ä:2006-04-01 00:33:03À´Ô´:ºìÁª×÷Õß:google
ÒýÓÃ:
¡¾µ¼¶Á¡¿ÔÚUNIXÖÐ ´ó¼Ò×ʹÓÃBourne ShellÒÔ¼°C Shell £¬±¾ÎÄÈ«Ãæ½²½âÁËShell ScriptµÄÕâÁ½ÖÖScriptµÄд·¨




Ç°ÑÔ

ÔÚDOS ÖУ¬Äã¿ÉÄÜ»á´ÓÊÂһЩÀýÐеÄÖظ²ÐÔ¹¤×÷£¬´ËʱÄã»á½«ÕâЩÖظ²ÐÔµÄÃüÁîд³ÉÅú´Îµµ£¬Ö»ÒªÖ´ÐÐÕâ¸öÅú´Îµµ¾ÍµÈì¶Ö´ÐÐÕâЩÃüÁî¡£´ó¼Ò»áÎÊÔÚUNIXÖÐÊÇ·ñÓÐÅú´Î´¦ÀíÕâ¸ö¶«¶«£¬´ð°¸ÊÇÓеġ£ÔÚUNIXÖв»Ö»ÓÐÈçDOS µÄÅú´Î´¦Àí£¬ËüµÄ¹¦ÄܱÈÆðDOS ¸üÇ¿´ó£¬Ïà¶ÔµØÒ²½Ï¸´ÔÓ£¬ÒѾ­ºÍÒ»°ãµÄ¸ß½×ÓïÑÔ²»ÏàÉÏÏ¡£ÔÚUNIXÖдó¼Ò¶¼²»½Ð×öÅú´Îµµ£¬¶ø½Ð×öShell Script¡£

Ò»°ã¶øÑÔ£¬Shell ScriptµÄµØλºÍÆäËüµÄ¿ÉÖ´Ðеµ£¨»òÃüÁÊÇÍêÈ«ÏàͬµÄ£¬Ö»²»¹ýShell ScriptÊÇÒÔÎÄ×ÖµµµÄ·½Ê½´¢´æ£¬¶ø·Ç¶þ½øλµµ¡£¶øÖ´ÐÐShell Scriptʱ£¬±ØÐëÓÐÒ»¸ö³Ìʽ½«ÆäÄÚÈÝת³ÉÒ»µÀµÀµÄÃüÁîÖ´ÐУ¬¶øÕâ¸ö³ÌʽÆäʵ¾ÍÊÇShell £¬ÕâÒ²¾ÍÊÇΪʲ÷áÎÒÃǽÐ×öShell ScriptµÄÔ­Òò£¨ÍùááÎÒÃdzÆΪScript£©¡£²»Í¬Shell µÄScript»ù±¾ÉÏ»áÓÐһЩ²îÒ죬ËùÒÔÎÒÃDz»Äܽ«Ð´¸øA shell µÄScriptÓÃB shell Ö´ÐС£¶øÔÚUNIXÖÐ ´ó¼Ò×ʹÓÃBourne ShellÒÔ¼°C Shell £¬ËùÒÔ±¾ÎľͽéÉÜÕâÁ½ÖÖScriptµÄд·¨¡£

½«ÎÄ×ÖµµÉèΪ¿ÉÖ´ÐеÄShell Script

Èç¹ûÎÒÃÇÒѾ­Ð´ºÃScript£¬ÈçºÎ½«ÆäÉè³É¿ÉÖ´ÐеµÄØ£¿ÒòΪScriptÆäʵÊÇÒ»¸ö¿ÉÖ´Ðеµ£¬ËùÒÔ±ØÐ뽫Æä´æȡȨÉ趨³É¿ÉÖ´ÐС£ÎÒÃÇ¿ÉÒÔʹÓÃÏÂÁÐÃüÁî¸ü¸Ä´æȡȨ£º

chmod u+x filename Ö»ÓÐ×Ô¼º¿ÉÒÔÖ´ÐУ¬ÆäËüÈ˲»ÄÜÖ´ÐÐ

chmod ug+x filename Ö»ÓÐ×Ô¼ºÒÔ¼°Í¬Ò»Èº¿ÉÒÔÖ´ÐУ¬ÆäËüÈ˲»ÄÜÖ´ÐÐ

chmod +x filename ËùÓÐÈ˶¼¿ÉÒÔÖ´ÐÐ

¶øÎÒÃÇÈçºÎÖ¸¶¨Ê¹ÓÃÄÇÒ»¸öShell À´½âÊÍËùдµÄScriptÄØ£¿¼¸ÖÖ»ù±¾µÄÖ¸¶¨·½Ê½ÈçÏÂËùÊö£º

1. Èç¹ûScriptµÄµÚÒ»¸ö·Ç¿Õ°××ÖÔª²»ÊÇ"#"£¬ÔòËü»áʹÓÃBourne Shell¡£

2. Èç¹ûScriptµÄµÚÒ»¸ö·Ç¿Õ°××ÖÔªÊÇ"#"ʱ£¬µ«²»ÒÔ"#!"¿ªÍ·Ê±£¬ÔòËü»áʹÓÃC Shell¡£

3. Èç¹ûScriptÒÔ"#!"¿ªÍ·£¬Ôò"#!"ááÃæËùдµÄ¾ÍÊÇËùʹÓõÄShell£¬¶øÇÒÒª½«Õû¸ö·¾¶Ãû³ÆÖ¸³öÀ´¡£

ÕâÀィÒéʹÓõÚÈýÖÖ·½Ê½Ö¸¶¨Shell £¬ÒÔÈ·±£ËùÖ´ÐеľÍÊÇËùÒªµÄ¡£Bourne ShellµÄ·¾¶Ãû³ÆΪ/bin/sh £¬¶øC Shell ÔòΪ/bin/csh¡£


1. ʹÓÃBourne Shell

©°--------------------©´ ©°--------------------©´

©¦echo enter filename ©¦ ©¦#!/bin/sh ©¦

©¦ . ©¦ or ©¦ . ©¦

©¦ . ©¦ ©¦ . ©¦

©¦ . ©¦ ©¦ . ©¦

©¸--------------------©¼ ©¸--------------------©¼

2. ʹÓÃC Shell

©°--------------------©´ ©°--------------------©´

©¦# C Shell Script ©¦ ©¦#!/bin/csh ©¦

©¦ . ©¦ ©¦ . ©¦

©¦ . ©¦ ©¦ . ©¦

©¦ . ©¦ ©¦ . ©¦

©¸--------------------©¼ ©¸--------------------©¼

3. ʹÓÃ/etc/perl

©°--------------------©´

©¦#! /etc/perl ©¦

©¦ . ©¦

©¦ . ©¦

©¦ . ©¦

©¸--------------------©¼

³ýÁËÔÚScriptÄÚÖ¸¶¨ËùʹÓõÄShell Í⣬ÄãÒ²¿ÉÒÔÔÚÃüÁîÁÐÖÐÇ¿ÖÆÖ¸¶¨¡£±ÈÈçÄãÒªÓÃC Shell Ö´ÐÐij¸öScript£¬Äã¿ÉÒÔÏÂÕâ¸öÃüÁ csh filename

´ËʱµÄScriptµÄ´æȡȨ¾Í²»Ò»¶¨ÒªÎª¿ÉÖ´Ðеµ£¬ÆäÄÚ²¿ËùÖ¸¶¨µÄShell Ò²»áÎÞЧ£¬ÏêϸµÄÇéÐÎááÃæ»áÌÖÂÛ¡£

ScriptµÄ»ù±¾½á¹¹¼°¹ÛÄî

ScriptÊÇÒÔÐÐΪµ¥Î»£¬ÎÒÃÇËùдµÄScript»á±»·Ö½â³ÉÒ»ÐÐÒ»ÐÐÀ´Ö´ÐС£¶øÿһÐпÉÒÔÊÇÃüÁî¡¢×¢½â¡¢»òÊÇÁ÷³Ì¿ØÖÆÖ¸ÁîµÈ¡£Èç¹ûijһÐÐÉÐδÍê³É£¬¿ÉÒÔÔÚÐÐÄ©¼ÓÉÏ"\" £¬Õâ¸öʱºòÏÂÒ»ÐеÄÄÚÈݾͻá½Óµ½ÕâÒ»ÐеÄááÃ棬³ÉΪͬһÐУ¬ÈçÏÂ

©°----------------------©´

©¦echo The message is \ ©¦

©¦too long so we have \ ©¦

©¦to split it into \ ©¦

©¦several lines ©¦

©¸----------------------©¼

µ±ScriptÖгöÏÖ"#" ʱ£¬ÔÙËüááÃæµÄͬһÐÐÎÄ×Ö¼´Îª×¢½â£¬Shell ²»»á¶ÔÆä·­Òë¡£ÔÚScriptÖÐÒªÖ´ÐÐÒ»¸öÃüÁîµÄ·½·¨ºÍÔÚÃüÁîÁÐÖÐÒ»Ñù£¬Äã¿ÉÒÔÇ°¾°»ò±³¾°Ö´ÐУ¬Ö´ÐÐÃüÁîʱҲ»áÐèÒªÉ趨һЩ»·¾³±äÊý¡£ScriptµÄÁ÷³Ì¿ØÖƺÍÒ»°ã¸ß½×ÓïÑÔµÄÁ÷³Ì¿ØÖÆûÓÐʲ÷áÁ½Ñù£¬Ò²ºÍ¸ß½×ÓïÑÔÒ»ÑùÓÐ

¸±³Ìʽ¡£ÕâЩʹµÃScriptµÄ¹¦Äܸü¼ÓÇ¿´ó¡£ÎªÁË´ïµ½Óë¸ß½×ÓïÑÔÏàͬµÄЧ¹û£¬ÎÒÃÇÒ²¿ÉÒÔÔÚScriptÖÐÉ趨±äÊý£¬Èç´ËʹScript³ÉΪһ¸öÃû¸¶ÆäʵµÄ¸ß½×ÓïÑÔ¡£

Bourne Shell

Ò»¡¢±äÊý

Bourne ShellµÄ±äÊýÐÍֻ̬ÓÐ×Ö´®±äÊý£¬ËùÒÔҪʹÓÃÊýÖµÔËËãÔò±ØÐë¿¿ÍⲿÃüÁî´ï³ÉÄ¿µÄ¡£¶øÆä±äÊýÖÖÀàÓÐÏÂÁм¸ÖÖ£º

1. ʹÓÃÕß±äÊý

ÕâÊÇ×ʹÓõıäÊý£¬ÎÒÃÇ¿ÉÒÔÈκβ»°üº¬¿Õ°××ÖÔªµÄ×Ö´®À´µ±×ö±äÊýÃû³Æ¡£É趨±äÊýֵʱÔòÓÃÏÂÁз½Ê½£º

var=string

È¡ÓñäÊýʱÔòÔÚ±äÊýÃû³ÆÇ°¼ÓÉÏÒ»"$" ºÅ¡£



©°--------------©´

©¦name=Tom ©¦

©¦echo name ©¦

©¦echo $name ©¦

©¸--------------©¼

½á¹ûÈçÏ£º

name

Tom

2. ϵͳ±äÊý£¨»·¾³±äÊý£©

ºÍʹÓÃÕß±äÊýÏàËÆ£¬Ö»²»¹ý´ËÖÖ±äÊý»á½«ÆäÖµ´«¸øÆäËùÖ´ÐеÄÃüÁî¡£Òª½«Ò»Ê¹ÓÃÕß±äÊýÉ趨Ϊϵͳ±äÊý£¬Ö»Òª¼ÓÉÏ£º

export var

©°--------------©´

©¦name=Tom ©¦

©¦export name ©¦

©¸--------------©¼

ÒÔÏÂÊÇʹÓÃÕßÒ»½øÈëϵͳ֮áá¾ÍÒÑÉ趨ºÃµÄϵͳ±äÊý£º

$HOME ʹÓÃÕß×Ô¼ºµÄĿ¼

$PATH Ö´ÐÐÃüÁîʱËùËÑÑ°µÄĿ¼

$TZ ʱÇø

$MAILCHECK ÿ¸ô¶àÉÙÃë¼ì²éÊÇ·ñÓÐеÄÐżþ

$PS1 ÔÚÃüÁîÁÐʱµÄÌáʾºÅ

$PS2 µ±ÃüÁîÉÐδ´òÍêʱ£¬Shell ÒªÇóÔÙÊäÈëʱµÄÌáʾºÅ

$MANPATH man Ö¸ÁîµÄËÑѰ·¾¶

3. Ψ¶ÁµÄʹÓÃÕß±äÊý

ºÍʹÓÃÕß±äÊýÏàËÆ£¬Ö»²»¹ýÕâЩ±äÊý²»Äܱ»¸Ä±ä¡£Òª½«Ê¹ÓÃÕß±äÊýÉè³ÉΨ¶ÁµÄ£¬Ö»Òª¼ÓÉÏ£º

readonly var

¶øÈôÖ»´òreadonlyÔò»áÁгöËùÓÐΨ¶ÁµÄ±äÊý¡£»¹ÓÐÒ»µã£¬ÏµÍ³±äÊý²»¿ÉÒÔÉ趨³ÉΨ¶ÁµÄ¡£



©°--------------©´

©¦name=Tom ©¦

©¦readonly name ©¦

©¦echo $name ©¦

©¦name=John ©¦

©¦readonly ©¦

©¸--------------©¼

½á¹ûÈçÏ£º

Tom

name: is read only

readonly name

readonly ......

4. ÌØÊâ±äÊý

ÓÐЩ±äÊýÊÇÒ»¿ªÊ¼Ö´ÐÐScriptʱ¾Í»áÉ趨£¬²¢ÇÒ²»ÒÔ¼ÓÒÔÐ޸ģ¬µ«ÎÒÃDz»½ÐËüΨ¶ÁµÄϵͳ±äÊý£¬¶ø½ÐËüÌØÊâ±äÊý£¨ÓÐЩÊé»á½ÐËüΨ¶ÁµÄϵͳ±äÊý£©£¬ÒòΪÕâЩ±äÊýÊÇÒ»Ö´ÐгÌʽʱ¾ÍÓÐÁË£¬¿öÇÒʹÓÃÕßÎÞ·¨½«Ò»°ãµÄϵͳ±äÊýÉ趨³ÉΨ¶ÁµÄ¡£ÒÔÏÂÊÇһЩµÈÊâ±äÊý£º

$0 Õâ¸ö³ÌʽµÄÖ´ÐÐÃû×Ö

$n Õâ¸ö³ÌʽµÄµÚn¸ö²ÎÊýÖµ£¬n=1..9

$* Õâ¸ö³ÌʽµÄËùÓвÎÊý

$# Õâ¸ö³ÌʽµÄ²ÎÊý¸öÊý

$$ Õâ¸ö³ÌʽµÄPID

$! Ö´ÐÐÉÏÒ»¸ö±³¾°Ö¸ÁîµÄPID

$? Ö´ÐÐÉÏÒ»¸öÖ¸ÁîµÄ·µ»ØÖµ

µ±ÄãÖ´ÐÐÕâ¸ö³ÌʽʱµÄ²ÎÊýÊýÄ¿³¬¹ý9 ¸öʱ£¬ÎÒÃÇ¿ÉÒÔʹÓÃshift ÃüÁ²ÎÊýÍùÇ°ÒÆÒ»¸ñ£¬Èç´Ë¼´¿ÉʹÓõÚ10¸öÒÔááµÄ²ÎÊý¡£³ý´ËÖ®Í⣬ÎáÈË¿ÉÒÔÓÃset ÃüÁî¸Ä±ä$n¼°$*£¬·½·¨ÈçÏ£º

set string

Èç´Ë$*µÄÖµ¼´Îªstring£¬¶ø·Ö½âááÔò»á·ÅÈë$n¡£Èç¹ûset ÃüÁîááÃæûÓвÎÊý£¬Ôò»áÁгöËùÓÐÒѾ­É趨µÄ±äÊýÒÔ¼°ÆäÖµ¡£

µµÃû£ºex1 ²ÎÊý£ºthis is a test

©°----------------------©´

©¦echo Filename: $0 ©¦

©¦echo Arguments: $* ©¦

©¦echo No. of args.: $# ©¦

©¦echo 2nd arg.: $2 ©¦

©¦shift ©¦

©¦echo No. of args.: $# ©¦

©¦echo 2nd arg.: $2 ©¦

©¦set hello, everyone ©¦

©¦echo Arguments: $* ©¦

©¦echo 2nd arg.: $2 ©¦

©¸----------------------©¼

½á¹ûÈçÏ£º

Filename: ex1

Arguments: this is a test

No. of args.: 4

2nd arg.: is

No. of args.: 3

2nd arg.: a

Arguments: hello, everyone

2nd arg.: everyone

ÖµµÃÒ»ÌáµÄÊÇ£¬µ±ÄãÏë´Ó¼üÅÌÊäÈëÒ»±äÊýֵʱ£¬Äã¿ÉÒÔʹÓÃÏÂÃæµÄÃüÁ

read var1 var2.....

Õâʱread»á½«Ò»¸ö×Ö·Ö¸øÒ»¸ö±äÊý¡£Èç¹ûÊäÈëµÄ×ֱȱäÊý»¹¶à£¬×îááÒ»¸ö±äÊý»á½«Ê£ÏµÄ×Öµ±³ÉÆäÖµ¡£Èç¹ûÊäÈëµÄ×ֱȱäÊý»¹ÉÙ£¬ÔòááÃæµÄ±äÊý»áÉè³É¿Õ×Ö´®¡£Èç¹ûÐèÒª´¦ÀíÊýÖµÔËË㣬ÎÒÃÇ¿ÉÒÔʹÓÃexprÃüÁî¡£Æä²ÎÊý¼°Êä³öÁÐ춸½Â¼£Á¡£

¶þ¡¢Ö´ÐÐÃüÁî

ÔÚBourne ShellÖÐÓÐÎåÖÖ·½·¨Ö´ÐÐÒ»¸öÃüÁ¶øÕâÎåÖÖ·½Ê½Ëù²úÉúµÄ¹ûÓÐЩÐíµÄ²»Í¬¡£

1. Ö±½ÓÏÂÃüÁî

Õâ¸ö·½Ê½ºÍÔÚÃüÁîÁÐÖÐÖ±½ÓÏÂÃüÁîµÄЧ¹ûÒ»Ñù¡£

2. ʹÓÃshÃüÁî



sh command

Õâ¸öµµ°¸±ØÐëÊÇBourne ShellµÄScript£¬µ«Õâ¸öµµ°¸²¢²»Ò»¶¨ÒªÉè³É¿ÉÖ´ÐС£³ý´ËÖ®ÍâºÍÖ±½ÓÏÂÃüÁîµÄ·½Ê½Ò»Ñù¡£

3. ʹÓÃ"."ÃüÁî

. command

ÕâʱºÍʹÓÃshÃüÁîÏàËÆ£¬Ö»²»¹ýËü²»ÏñshÒ»°ã»á²úÉúеÄprocess £¬Ïà·´µØ£¬Ëü»áÔÚÔ­ÓеÄprocess ÏÂÍê³É¹¤×÷¡£

4. ʹÓÃexecÃüÁî



exec command

´ËʱÕâ¸öScript½«»á±»ËùÖ´ÐеÄÃüÁîËùÈ¡´ú¡£µ±Õâ¸öÃüÁîÖ´ÐÐÍê±ÏÖ®áᣬÕâ¸öScriptÒ²»áËæÖ®½áÊø¡£

5. ʹÓÃÃüÁîÌæ»»

ÕâÊÇÒ»¸öÏ൱ÓÐÓõķ½·¨¡£Èç¹ûÏëҪʹij¸öÃüÁîµÄÊä³ö³ÉΪÁíÒ»¸öÃüÁîµÄ²ÎÊýʱ£¬¾ÍÒ»¶¨ÒªÊ¹ÓÃÕâ¸ö·½·¨¡£ÎÒÃǽ«ÃüÁîÁÐì¶Á½¸ö"`" ºÅÖ®¼ä£¬¶øShell »áÒÔÕâ¸öÃüÁîÖ´ÐÐááµÄÊä³ö½á¹û´úÌæÕâ¸öÃüÁîÒÔ¼°Á½¸ö"`" ·ûºÅ¡£



str='Current directory is '`pwd`

echo $str

½á¹ûÈçÏ£º

Current directory is /users/cc/mgtsai

Õâ¸öÒâ˼ÊÇpwd Õâ¸öÃüÁîÊä³ö"/users/cc/mgtsai"£¬¶øááÕû¸ö×Ö´®´úÌæÔ­À´µÄ`pwd` É趨str ±äÊý£¬ËùÒÔstr ±äÊýµÄÄÚÈÝÔò»áÓÐpwd ÃüÁîµÄÊä³ö¡£

number=`expr $number + 1`

Õâ¾ÍÊÇÏÈÇ°ËùÌáÒª×÷ÊýÖµÔËËãµÄ·½·¨£¬»ù±¾ÉÏexprÃüÁîÖ»½«ÔËËãʽ½â£¬¶øááÊä³öµ½±ê×¼Êä³öÉÏ¡£Èç¹ûÒª½«Ä³±äÊýÉ趨³ÉÆäÖµ£¬·ÇµÃ¿¿ÃüÁîÌæ»»µÄ·½

ʽ²»¿É¡£Õâ¸öÀý×ÓÊǽ«number±äÊýµÄÖµ¼Ó1 ááÔÙ´æ»Ønumber±äÊý¡£

Èý¡¢Á÷³Ì¿ØÖÆ

ÔÚ½éÉÜÁ÷³Ì¿ØÖÆ֮ǰ£¬ÎÒÃÇÏÈÀ´¿´¿´testÃüÁî¡£testÃüÁîµÄ²ÎÊýÊÇÌõ¼þÅжÏʽ£¬µ±Ìõ¼þΪÕæʱÔò´«»Ø·ÇÁãÖµ£¬¶øÌõ¼þΪαʱÔò´«»ØÁã¡£ÔÚËùÓеÄÁ÷³Ì¿ØÖƶ¼±ØÐëÓõ½testÃüÁîÀ´ÅжÏÕæα¡£¶øtestÃüÁîµÄʹÓ÷½·¨ÔòÁÐ춸½Â¼£Â¡£

test $# = 0

Èç¹ûÖ´ÐÐÕâ¸ö³ÌʽûÓвÎÊýʱ£¬»á´«»Ø·ÇÁãÖµ´ú±í"$# = 0"Õâ¸öÌõ¼þ³ÉÁ¢¡£·´Ö®Ôò»á´«»ØÁã¡£

ÒÔϽéÉܸ÷ÖÖÁ÷³Ì¿ØÖÆ£º

1. if then

Óï·¨ÒÔ¼°Á÷³ÌͼÈçÏÂ

©¦ FALSE

if (condition) £¼condition£¾--©´

then ©¦TRUE ©¦

then-commands then-commands ©¦

fi ©À--------©¼

©¦

condition ÊÇÒ»¸ötestÃüÁî¡£ÍùááËù½éÉܵĸ÷ÖÖÁ÷³ÌÖеÄcondition ¶¼ÊÇtest

ÃüÁî¡£

µµÃû£ºchkarg

©°----------------------©´

©¦if (test $# != 0) ©¦

©¦ then ©¦

©¦ echo Arg1: $1 ©¦

©¦fi ©¦

©¸----------------------©¼

$ chkarg Hello

Arg1: Hello

$ chkarg

$

2. if then else

Óï·¨ÒÔ¼°Á÷³ÌͼÈçÏÂ

©¦ FALSE

if (condition) £¼condition£¾----------©´

then ©¦TRUE ©¦

then-commands then-commands else-commands

else ©À----------------©¼

else-commands ©¦

fi

3. if then elif

Óï·¨ÒÔ¼°Á÷³ÌͼÈçÏÂ

©¦ FALSE

if (condition1) £¼condition1£¾--©´

then ©¦TRUE ©¦ FALSE

commands1 commands1 £¼condition2£¾--©´

elif (condition2) ©¦ ©¦TRUE ©¦

then ©¦ commands2 commands3

commands2 ©À----------©Ø--------©¼

else ©¦

commands3

commands3

fi

echo 'word 1: \c'

read word1

echo 'word 2: \c'

read word2

echo 'word 3: \c'

read word3

if (test "$word1" = "$word2" -a "$word2" = "$word3")

then

echo 'Match: words 1, 2, & 3'

elif (test "$word1" = "$word2")

then

echo 'Match: words 1 & 2'

elif (test "$word1" = "$word3")

then

echo 'Match: words 1 & 3'

elif (test "$word2" = "$word3")

then

echo 'Match: words 2 & 3'

else

echo 'No match'

fi

4. for in

Óï·¨ÒÔ¼°Á÷³ÌͼÈçÏÂ

©¦ FALSE

for var in arg-list ©°--£¼arg-list»¹Óж«Î÷Â𣿣¾--©´

do ©¦ ©¦TRUE ©¦

commands ©¦ ´Óarg-listÈ¡µÃÒ»Ïî ©¦

done ©¦ ·Åµ½±äÊývar ©¦

©¦ ©¦ ©¦

©¦ commands ©¦

©¸------------©¼ ©¦

©°----------------------©´ ©°--------------©¼

©¦for a in xx yy zz ©¦ ©¦

©¦ do ©¦

©¦ echo $a ©¦

©¦done ©¦

©¸----------------------©¼

½á¹ûÈçÏ£º

xx

yy

yy

zz

5. for

Óï·¨ÒÔ¼°Á÷³ÌͼÈçÏÂ

©¦ FALSE

for var ©°--£¼²ÎÊýÖл¹Óж«Î÷Â𣿣¾--©´

do ©¦ ©¦TRUE ©¦

commands ©¦ ´Ó²ÎÊýÖÐÈ¡µÃÒ»Ïî ©¦

done ©¦ ·Åµ½±äÊývar ©¦

©¦ ©¦ ©¦

©¦ commands ©¦

©¸----------©¼ ©¦

µµÃû£ºlstarg ©°--------------©¼

©°----------------------©´ ©¦

©¦for a ©¦

©¦ do ©¦

©¦ echo $a ©¦

©¦done ©¦

©¸----------------------©¼

$lstarg xx yy zz

xx

yy

yy

zz

6. while

Óï·¨ÒÔ¼°Á÷³ÌͼÈçÏÂ

©¦ FALSE

while (condition) ©°--£¼condition£¾--©´

do ©¦ ©¦TRUE ©¦

commands ©¦ commands ©¦

done ©¸--------©¼ ©¦

©°--------©¼

©¦



©°------------------------------©´

©¦number=0 ©¦

©¦while (test $number -lt 10) ©¦

©¦ do ©¦

©¦ echo "$number\c" ©¦

©¦ number=`expr $number + 1` ©¦

©¦done ©¦

©¦echo ©¦

©¸------------------------------©¼

½á¹ûÈçÏ£º

0123456789

7. until

Óï·¨ÒÔ¼°Á÷³ÌͼÈçÏÂ

©¦ TRUE

until (condition) ©°--£¼condition£¾--©´

do ©¦ ©¦FALSE ©¦

commands ©¦ commands ©¦

done ©¸--------©¼ ©¦

©°--------©¼

©¦

ËüºÍwhile µÄ²»Í¬Ö»ÔÚì¶while ÊÇÔÚÌõ¼þΪÕæʱִÐлØȦ£¬¶øuntil ÊÇÔÚÌõ¼þΪ¼ÙʱִÐлØȦ¡£

8. break¼°continue

ÕâÁ½ÕßÊÇÓÃì¶for, while, until µÈ»ØȦ¿ØÖÆÏ¡£break »áÌøÖÁdoneáá·½Ö´ÐУ¬¶øcontinue»áÌøÖÁdoneÖ´ÐУ¬¼ÌÐøÖ´ÐлØȦ¡£

9. case

Óï·¨ÒÔ¼°Á÷³ÌͼÈçÏÂ

©¦ TRUE

case str in £¼str=pat1£¾--------commands1--©´

pat1) commands1;; ©¦FALSE TRUE ©¦

pat2) commands2;; £¼str=pat2£¾--------commands2--©È

pat3) commands3;; ©¦FALSE TRUE ©¦

esac £¼str=pat3£¾--------commands3--©È

©¦FALSE ©¦

©À------------------------©¼

©¦

¶øpat ³ýÁË¿ÉÒÔÖ¸¶¨Ò»Ð©È·¶¨µÄ×Ö´®£¬Ò²¿ÉÒÔÖ¸¶¨×Ö´®µÄ¼¯ºÏ£¬ÈçÏÂ

* ÈÎÒâ×Ö´®

? ÈÎÒâ×ÖÔª

[abc] a, b, »òcÈý×ÖÔªÆäÖÐÖ®Ò»

[a-n] ´Óaµ½nµÄÈÎÒ»×ÖÔª

| ¶àÖØÑ¡Ôñ



©°------------------------------©´

©¦echo 'Enter A, B, or C: \c' ©¦

©¦read letter ©¦

©¦case $letter in ©¦

©¦ A|a) echo 'You entered A.';;©¦

©¦ B|b) echo 'You entered B.';;©¦

©¦ C|c) echo 'You entered C.';;©¦

©¦ *) echo 'Not A, B, or C';; ©¦

©¦esac ©¦

©¸------------------------------©¼

10. º¯Êý

¸ñʽÈçÏÂ

function-name()

{

commands

}

¶øÒªºô½Ð´Ëº¯Êý£¬¾ÍÏñÔÚÃüÁîÁÐÏÂÖ±½ÓÏÂÃüÁîÒ»°ã¡£

C Shell



C Shell ÓÐЩÌØÐÔºÍBourne ShellÒ»Ñù£¬µ«ÓÐЩ²»Ïàͬ¡£ÕâÀï½éÉÜÓëBourne Shell²»ÏàͬµÄµØ·½¡£

Ò»¡¢±äÊý



1. ×Ö´®±äÊý

Õâ¸ö²¿·ÖºÍBourne ShellµÄ±äÊýÒ»Ñù£¬Ö»²»¹ýÔÚÉ趨±äÊýֵʱ²»ÄÜʹÓÃBourne ShellµÄ·½Ê½£¬¶ø±ØÐë´ò£º

set var=value

2. Êý×ÖÔËËã

»ù±¾ÉÏC Shell ûÓÐÊý×Ö±äÊý£¬µ«C Shell È´Óмòµ¥µÄ·½·¨´¦ÀíÊý×ÖÔËË㣺

@ var operator expression

operator¿ÉÒÔÊÇC ÓïÑÔÖеÄ=, +=, -=,......£¬¶øexpressionÔòÊÇÔËËãʽ¡£ÔËËãʽµÄÔËËã×ÓÈçÏ£º

A. () ¸Ä±ä¼ÆËãµÄ˳Ðò

~@

B. ~ λԪNOTÔËËã

@~~

! Âß¼­·ñ¶¨

C. % È¡âÅÊý

C. % È¡âÅÊý

/ ³ý

* ³Ë

- ¼õ

+ ¼Ó

D. >> ÓÒÒÆ

<< ×óÒÆ

E. > ´óì¶

< Сì¶

>= ´ó춵Èì¶

<= С춵Èì¶

!= ²»µÈì¶

== µÈì¶

F. & λԪANDÔËËã

^ λԪXORÔËËã

| λԪOR ÔËËã

G. && Âß¼­AND

|| Âß¼­OR

³ý´ËÖ®Í⣬ÎÒÃÇÒ²¿ÉÒÔ¼ìÑéÒ»¸öµµ°¸µÄ״̬£¬ÈçÏÂ

-n filename

¶ø-n¿ÉΪÏÂÁÐÖ®Ò»

-d µµ°¸ÊÇÒ»¸öĿ¼µµ°¸

-e µµ°¸´æÔÚ

-f µµ°¸ÎªÒ»°ãµÄµµ°¸

-o ʹÓÃÕßÓµÓÐÕâ¸öµµ°¸

-r ʹÓÃÕß¿ÉÒÔ¶ÁÈ¡Õâ¸öµµ°¸

-w ʹÓÃÕß¿ÉÒÔдÈëÕâ¸öµµ°¸

-x ʹÓÃÕß¿ÉÒÔÖ´ÐÐÕâ¸öµµ°¸

-z µµ°¸³¤¶ÈΪ0



@ count = count + 1

@ flag = -e /users/cc/mgtsai/mail && -e /usr/spool/mail

3. ÕóÁÐ

ÔÚC Shell ÖУ¬ÎÒÃÇ¿ÉÒÔÐû¸æÕóÁбäÊý£¬·½Ê½ÈçÏÂ

set var=(val1 val2 ......)

¶øvar[1]ֵ֮Ϊval1£¬var[2]ֵ֮Ϊval2......¡£¶ø$var´ú±íÕû¸öÕóÁС£ÎÒÃÇ¿ÉÒÔÓÃ$#var À´¼ÆËãÕóÁиöÊý£¬Ò²¿ÉÒÔÓÃ$?var À´¼ì²éij¸ö±äÊýÊÇ·ñÒÑÐû¸æ¡£

4. ÌØÊâ±äÊý

$argv ºÍBourne ShellµÄ$*ÏàËÆ£¬Ö»²»¹ýÕâÊÇÒ»¸öÕóÁС£

$argv[n] ºÍBourne ShellµÄ$nÏàͬ£¬µ«²»ÊܸöÊýÏÞÖÆ¡£

$#argv ºÍBourne ShellµÄ$#Ïàͬ

$home ºÍBourne ShellµÄ$HOMEÏàͬ

$path ºÍBourne ShellµÄ$PATHÏàËÆ£¬Ö»²»¹ýÕâÊÇÒ»¸öÕóÁÐ

$prompt ºÍBourne ShellµÄ$PS1Ïàͬ

$shell ShellµÄ·¾¶Ãû³Æ

$status ºÍBourne ShellµÄ$?Ïàͬ

$$ ºÍBourne ShellµÄ$$Ïàͬ

$< ¼üÅÌÊäÈë

¶þ¡¢Ö´ÐÐÃüÁî

»ù±¾ÉϺÍBourne ShellÏàͬ£¬Ö»ÓÐÒ»µãÔÚBourne ShellÖеÄ"." ÃüÁîÔÚC Shell ÖÐÔòΪ"source"ÃüÁî¡£

Èý¡¢Á÷³Ì¿ØÖÆ

ÔÚC Shell ÖÐÁ÷³Ì¿ØÖƲ»ÏñBourne ShellÖÐÒ»°ãÐèҪʹÓÃtestÃüÁî¡£Ïà·´µØ£¬ËüºÍC ÓïÑÔÀàËÆÖ»ÒªÔÚÌõ¼þÖÐд³öÔË¡õʽ¼´¿É¡£µ±ÔËËã½á¹û²»ÎªÁãʱ£¬ÆäֵΪÕ棬ΪÁãʱÆäֵΪα¡£ÒÔÏÂÊÇC ShellµÄÁ÷³Ì¿ØÖÆ

1. if

Óï·¨ÈçÏÂ

if (expression) simple-command

2. goto

Óï·¨ÈçÏÂ

goto label

Õâʱ³Ìʽ»áÌøÖÁÒÔl"label:"¿ªÍ·µÄÄÇÒ»ÐÐÖ´ÐÐ



if ($#argv == 2) goto goodargs

echo 'Please use two arguments.'

exit

goodrags:

...

3. if then else

ÕâºÍBourne ShellµÄif then, if then else, if then elif ÏàËÆ¡£Óï·¨ÈçÏÂ

A. if (expression) then

commands

endif

B. if (expression) then

commands

else

commands

endif

C. if (expression) then

commands

else if (expression) then

commands

else

commands

endif

4. foreach

ÕâºÍBourne ShellµÄfor inÏàËÆ¡£Óï·¨ÈçÏÂ

foreach var (arg-list)

commands

end

5. while

ÕâºÍBourne ShellµÄwhileÏàËÆ¡£Óï·¨ÈçÏÂ

while (expression)

commands

end

6. break¼°continue

ÕâºÍBourne ShellµÄbreak ¼°continueÏàËÆ£¬ÊÇÓÃÀ´ÖжÏforeach ¼°while »ØȦ¡£

7. switch

ÕâºÍBourne ShellµÄcaseÏàËÆ¡£Óï·¨ÈçÏÂ

switch (string)

case pat1:

commands1

breaksw

case pat2:

commands2

breaksw

case pat3:

commands3

breaksw

endsw

¸½Â¼£Á exprÃüÁî

ÃüÁî¸ñʽ

expr expression

ÐðÊö

expressionÊÇÓÉ×Ö´®ÒÔ¼°ÔËËã×ÓËù×é³É£¬Ã¿¸ö×Ö´®»òÊÇÔËËã×ÓÖ®¼ä±ØÐëÓÿհ׸ô¿ª¡£Ï±íÊÇÔËËã×ÓµÄÖÖÀ༰¹¦ÄÜ£¬¶øÓÅÏÈ˳ÐòÔòÒÔÏÈáá´ÎÐòÅÅÁУ¬ÎÒÃÇ¿ÉÒÔÀûÓÃСÀ¨ºÅÀ´¸Ä±äÔËËãµÄÓÅÏÈ´ÎÐò¡£ÆäÔËËã½á¹ûÔòÊä³öÖÁ±ê×¼Êä³öÉÏ¡£

: ×Ö´®±È½Ï¡£±È½ÏµÄ·½Ê½ÊÇÒÔÁ½×Ö´®µÄµÚÒ»¸ö×Öĸ¿ªÊ¼£¬¶øÒÔµÚ¶þ¸ö×Ö´®µÄ×Öĸ½áÊø¡£Èç¹ûÏàͬʱ£¬ÔòÊä³öµÚ¶þ¸ö×Ö´®µÄ×Öĸ¸öÊý£¬Èç¹û²»Í¬Ê±Ôò´«»Ø0 ¡£

* ³Ë·¨

/ ³ý·¨

% È¡âÅÊý

+ ¼Ó·¨

- ¼õ·¨

< Сì¶

<= С춵Èì¶

= µÈì¶

!= ²»µÈì¶

>= ´ó춵Èì¶

> ´óì¶

& ANDÔËËã

| ORÔËËã

µ±expressionÖк¬ÓÐ"*", "(", ")" µÈ·ûºÅʱ£¬±ØÐëÔÚÆäÇ°Ãæ¼ÓÉÏ"\" £¬ÒÔÃâ±»Shell ½âÊͳÉÆäËüÒâÒå¡£

expr 2 \* \( 3 + 4 \) ÆäÊä³öΪ14

¸½Â¼£Â testÃüÁî

ÃüÁî¸ñʽ

test expression

ÐðÊö

expressionÖаüº¬Ò»¸öÒÔÉϵÄÅжÏ×¼ÔòÒÔ×÷ΪtestÆÀÚ¬µÄ±ê×¼¡£Á½×¼Ôò¼äÓÃ"-a"´ú±íÂß¼­AND ÔËË㣬"-o"´ú±íÂß¼­ORÔËË㣬¶øÔÚ×¼ÔòÇ°·ÅÖÃÒ»"!" ´ú±íNOT ÔËËã¡£Èç¹ûûÓÐÀ¨ºÅ£¬ÔòÓÅÏÈȨÔòΪ"!" > "-a" > "-o" ¡£ºÍexprÃüÁîÏàͬ£¬ÏàʹÓÃ×óÓÒÀ¨ºÅʱ£¬±ØÐëÔÚÆäÇ°Ãæ¼ÓÉÏ"\" ¡£ÒÔÏÂÊÇÓйØ×¼ÔòµÄÐðÊö£¨ºÏÐðÊöʱ´«»ØÕ棬·ñÔò´«»Øᣩ£º

string string²»Îª¿Õ°××Ö´®

-n string stringµÄ³¤¶È´óì¶0

-z string stringµÄ³¤¶ÈµÈì¶0

string1=string2 string1µÈì¶string2

string1!=string2 string1²»µÈì¶string2

int1 -gt int2 int1´óì¶int2

int1 -ge int2 int1´ó춵Èì¶int2

int1 -eq int2 int1µÈì¶int2

int1 -ne int2 int1²»µÈì¶int2

int1 -le int2 int1С춵Èì¶int2

int1 -lt int2 int1Сì¶int2

-r filename µµ°¸¿É¶ÁÈ¡

-w filename µµ°¸¿ÉдÈë

-x filename µµ°¸¿ÉÖ´ÐÐ

-f filename µµ°¸ÎªÒ»°ãµµ

-d filename µµ°¸ÎªÄ¿Â¼

-s filename µµ°¸Îª·Ç¿ÕµÄÒ»°ãµµ

test -r "$filename" -a -s "$filename"
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ