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

Unix/Linux ƽ̨ÈÎÎñµÄ×Ô¶¯»¯

·¢²¼Ê±¼ä:2006-03-24 00:14:21À´Ô´:ºìÁª×÷Õß:[Hl.y]
ÒýÓÃ:
±¾ÕÂÒªµã£º

±¾Õ½éÉÜÓÃÀ´Ìæ´úshell½Å±¾µÄ¹¤¾ß£¬ÈçTCLºÍperl¡£

±¾Õ¾ßÌå°üÀ¨ÒÔÏÂÄÚÈÝ¡£

TCL/expectµÄʹÓÃ

awkÓïÑԵĻù±¾ÖªÊ¶

perlÓïÑԵĻù±¾ÖªÊ¶


11.1 TCLºÍexpect

TCLÊÇÒ»ÖÖÀàËÆshell½Å±¾µÄÓïÑÔ£¬Äã¿ÉÒÔʹÓÃËüÀ´Íê³ÉÐí¶à²Ù×÷¡£²»¹ý£¬ÎÒ½éÉÜËüµÄÒªÔ­ÒòÊÇexpectÊÇ´ÓËü·¢Õ¹³öÀ´µÄ¡£Èç¹ûÄãÏëҪдһ¸öÄܹ»×Ô¶¯´¦ÀíÊäÈëÊä³öµÄ½Å±¾£¨ÈçÏòÓû§ÌáÎʲ¢ÇÒÑéÖ¤ÃÜÂ룩ÓÖ²»ÏëÃæ¶ÔC»òÕßPerl£¬ÄÇôexpectÊÇÄãµÄΨһѡÔñ¡£

11.1.1 TCLÓïÑÔ

ҪʹÓÃTCL£¬Äã±ØÐëÏÈ°²×°Õâ¸ö³ÌÐò£º

% rpm -q tcl

tcl-8.0.5-30

TCLÓïÑÔ¿ÉÒÔÓý»»¥Ê½»òÕ߽ű¾µÄ·½Ê½Ö´ÐУ¬ÒªÊ¹Óý»»¥Ê½µÄTCL»·¾³£¬Ö»ÒªÊäÈë

$ tclsh

%

³öÏÖµÄ"%"·ûºÅÊÇTCLµÄÌáʾ·û£¬È»ºó¾Í¿ÉÒÔʹÓÃTCLÃüÁîµÄ¡£

Èç¹ûÄãҪʹÓýű¾·½Ê½µÄTCL£¬Ê×ÏÈ°ÑÄãµÄ½Å±¾Ð´³ÉÒ»¸öÎı¾Îļþ£¬ÀýÈçtest.tcl£¬È»ºóÖ´ÐÐ

$ tclsh test.tcl

ÔÚtcl½Å±¾ÖУ¬Ã¿Ò»ÐлòÕßÊÇÒ»¸öÃüÁîÐУ¬»òÕßÊÇÒ»¸ö×¢ÊÍ¡£×¢ÊÍÐбØÐëÒÔ#·ûºÅ¿ªÍ·£¬¶øÃüÁîÐÐ×îºÃÒԷֺŽáÊø£¬ËäÈ»²»Ò»¶¨ÒªÕâÑù×ö£¬µ«ÊÇÕâÑù×ö¿ÉÒÔÃâÈ¥²»ÉÙÂé·³¡£

±äÁ¿

ÔÚtclÖУ¬ÓÐÁ½ÖÖ»ù±¾ÀàÐ͵ıäÁ¿£¬¼´±êÁ¿ºÍÊý×é¡£±êÁ¿¾ÍÊÇÒ»°ãµÄÊý×Ö»òÕß×Ö·û´®±äÁ¿£¬¿ÉÒÔÓÃsetÓï¾ä¶¨Òåͬʱ¸³Öµ£º

% set i 1

1

×Ö·û´®Ó¦¸ÃÓÃÒýºÅÀ¨ÆðÀ´£º

% set str "test"

'test'

ÒªÊä³öÒ»¸ö±êÁ¿µÄÄÚÈÝ£¬Ê¹ÓÃputÓï¾ä£º

% puts $str

test

$ÓÃÀ´ËµÃ÷strÊÇÒ»¸ö±äÁ¿¡£putsº¯ÊýÔÚ±ê×¼Êä³öÏÔʾ±äÁ¿µÄÄÚÈÝ¡£

Êý×éÒ²¿ÉÒÔÓÃsetÓï¾ä¶¨Ò壬ʵ¼ÊÉÏ£¬tclÖн¨Á¢Êý×éÖ»Êǵ¥¸ö½¨Á¢Êý×éµÄÔªËØ¡£ÀýÈç £¬

% set arr(1) 0

0

% set arr(2) 1

1

ÕâÑù¾Í½¨Á¢ÁËÒ»¸öÁ½¸öÔªËصÄÊý×éarr¡£ÔÚTCLÖУ¬²»´æÔÚÏ൱ÓÚÊý×é±ß½çÕâÑùµÄ¶«Î÷£¬ÀýÈç

% set arr(100) to

to

ÕâʱÊý×éÖÐʵ¼ÊÖ»´æÔÚarr(1)£¬arr(2)ºÍarr(100)£¬ÕâÊǺÍCÓïÑÔ²»Í¬µÄµØ·½¡£ÓÃarray sizeÃüÁî¿ÉÒÔ·µ»ØÊý×éµÄ´óС£º

% array size arr

3

·ÃÎÊÊý×éµÄ·½·¨ºÍ·ÃÎʱêÁ½Êµ¼ÊÊÇÒ»ÑùµÄ£¬ÀýÈ磺

% puts $arr(100)

to

¿ÉÒÔÓÃͬÑùµÄ·½·¨´´½¨¶àάÊý×é¡£

ҪʹÓÃÊý×éÖеÄËùÓÐÔªËØ£¬ÐèҪʹÓÃÒ»ÖÖÌØÊâµÄ±ãÀû·½Ê½¡£Ê×ÏÈÒªÆô¶¯startsearsh:

% array startsearch arr

s-1-arr

ÕâÀï·µ»ØÁËÒ»¸öËÑË÷id£¬Äã¿ÉÒÔ°ÑËü´«µÝ¸øij¸ö±äÁ¿£¬ÒòΪÒÔºó»¹ÒªÊ¹ÓÃËü½øÐнøÒ»²½µÄËÑË÷£º

% set my_id [array startsearch arr]

s-1-arr

ÏÖÔÚmy_idµÄÄÚÈÝÊÇs-1-arr£¬È»ºó£¬¾Í¿ÉÒÔËÑË÷arrµÄÄÚÈÝÁË£º

% array nextelement arr $my_id

whi

ÕâÀïµÄarray nextelement·µ»ØµÄÊÇʲô£¿¿ÉÄÜÓеã³öºõÄãµÄÒâÁÏ£¬ÊÇarrÊý×éµÄϱ꣬ÔÙÖ´ÐÐÒ»´Îarray nextelementÃüÁîÓÖ»áÕÒ³öÁíÍâÒ»¸öϱ꣺

% array nextelement arr $my_id

4

ÕâÑù±éÀúÏÂÈ¥£¬¿ÉÒÔÕÒ³öarrÊý×éµÄËùÓÐϱ꣬¶øÖªµÀϱêÖ®ºó£¬¾Í¿ÉÒÔÓÃ$arr(4)Ö®ÀàµÄ·½Ê½·ÃÎÊarrµÄÄÚÈÝÁË¡£µ±±éÀúÍê³ÉÖ®ºó£¬array nextelementÃüÁ¼òµ¥µØ·µ»Ø£º

% array nextelement arr $my_id

%

Õâʱ¾Í¿ÉÒÔÍ£Ö¹±éÀú¹ý³ÌÁË£¬Èç¹ûÄãÏëÈ·ÈϱéÀúÊÇ·ñÍê³É£¬¿ÉÒÔʹÓÃarray anymoreÃüÁ

% array anymore arr $my_id

0

·µ»Ø0˵Ã÷±éÀúÒѾ­Íê³É¡£

´®´¦Àí

TCLÖпÉÒÔ½øÐÐÒ»°ãµÄ´®´¦Àí¹ý³Ì£¬Õâ¿ÉÒÔʹÓÃstringÃüÁîºÍappendÃüÁappendÃüÁij¸ö×Ö·û´®¼Óµ½ÁíÍâÒ»¸ö×Ö·û´®µÄºóÃ棺

% set str1 "test "

test

% set str2 "cook it"

cook it

% append str1 $str2 " and other"

test cook it and other

stringÃüÁî¿ÉÒÔÖ´ÐÐ×Ö·û´®µÄ±È½Ï£¬É¾³ýºÍ²éѯ£¬Æä¸ñʽÊÇ string [²ÎÊý] string1 [string2]

²ÎÊý¿ÉÒÔÊÇÏÂÃæµÄÃüÁîÖ®Ò»£º

compare °´ÕÕ×Öµä˳Ðò¶Ô×Ö·û´®½øÐбȽϣ¬¸ù¾ÝÏà¶Ô¹Øϵ·µ»Ø-1£¬0»òÕß+1¡£

first ·µ»Østring2ÖеÚÒ»´Î³öÏÖstring1µÄλÖã¬Èç¹ûʧ°Ü£¬·µ»Ø-1¡£

last ·µ»Østring2ÖÐ×îºóÒ»´Î³öÏÖstring1µÄλÖã¬Èç¹ûʧ°Ü£¬·µ»Ø-1

trim ´Óstring1ÖÐɾ³ý¿ªÍ·ºÍ½áβµÄ³öÏÖÔÚstring2ÖеÄ×Ö·û

trimleft ´Óstring1ÖÐɾ³ý¿ªÍ·µÄ³öÏÖÔÚstring2ÖеÄ×Ö·û¡£

trimright ´Óstring1ÖÐɾ³ý½áβµÄ³öÏÖÔÚstring2ÖеÄ×Ö·û

ÏÂÃ漸¸öÓÃÔÚstringÖеIJÎÊý²»ÐèÒªstring2±äÁ¿£º

length ·µ»Øtring1µÄ³¤¶È

tolower ·µ»Ø½«string1È«²¿Ð¡Ð´»¯µÄ´®

toupper ·µ»Ø½«string1È«²¿´óд»¯µÄ´®

ÔËËã

TCLµÄÔËË㷽ʽ±È½Ï±ðŤ£¬ËüʹÓÃexprÃüÁî×÷Ϊ¼ÆËã·ûºÅ£¬ÆäÓ÷¨ÀàËÆCÓïÑÔµÄ+=ºÍ/= £¬ÀýÈ磬

% set j [expr $i/5]

1

×¢ÒâTCL»á×Ô¶¯Ñ¡ÔñÕûÊý»òÕ߸¡µã¼ÆË㣺

% set l [ expr $i /4.0]

1.25

% set l [ expr $i /4]

1

ÔÚTCLÀïÃæ¿ÉÒÔʹÓÃ+ - * /ºÍ%×÷Ϊ»ù±¾ÔËËã·û£¬ÁíÍâͨ³£»¹°üÀ¨Ò»Ð©Êýѧº¯Êý£¬Èçabs£¬sin£¬cos£¬expºÍpower(³Ë·½)µÈµÈ¡£

ÁíÍ⣬»¹ÓÐÒ»¸öÆðÔËËã·û×÷ÓõÄÃüÁîincr£¬ËüÓÃÀ´¶Ô±äÁ¿¼ÓÒ»£º

% set i 1

1

% incr i

2

Á÷³Ì¿ØÖÆ

tclÖ§³Ö·ÖÖ§ºÍÑ­»·¡£·ÖÖ§Óï¾ä¿ÉÒÔʹÓÃifºÍswitchʵÏÖ¡£ifÓï¾äµÄºÍCÓïÑÔÀàËÆ£¬Èç

if { $ x < 0 } {

set y 10;

}

×¢ÒâÅжÏ×Ó¾äÒ²ÐèҪʹÓû¨À¨ºÅ¡£

ÓëCÓïÑÔÒ»Ñù£¬tclµÄifÓï¾äÒ²¿ÉÒÔʹÓÃelseºÍelseif¡£

switchÓï¾äµÄÓ÷¨ÓеãÀàËÆÕâÑù£º

switch $x {

0 { set y 10;}

10 { set y 100;}

20 { set y 400;}

}

ÓëCµÄswitchÓï¾ä²»Í¬£¬Ã¿´ÎÖ»ÓзûºÏ·ÖÖ§ÖµµÄ×Ó¾ä²Å±»Ö´ÐС£

Ñ­»·ÃüÁîÖ÷ÒªÓÉfor£¬foreachºÍwhile¹¹³É£¬¶øÇÒÿһ¸ö¶¼¿ÉÒÔʹÓÃbreakºÍcontinue×Ӿ䡣

forÓï¾äµÄ¸ñʽÓеãÀàËÆÕâÑù£º

for { set i 0} {$i < 10} { incr i} {puts $i}

½«»áÊä³ö´Ó1µ½9µÄÕûÊý¡£

Èç¹ûÓÃwhileÑ­»·£¬Õâ¸ö¾ä×Ó¿ÉÒÔд³É

while {$i < 10 } {

puts $i;

incr i;

}

foreachÊǶÔÓÚ¼¯ºÏÖеÄÿһ¸öÔªËØÖ´ÐÐÒ»´ÎÃüÁ´óÖµÄÃüÁî¸ñʽÊÇ

foreach [±äÁ¿] { ¼¯ºÏ } {

Óï¾ä£»

}

ÀýÈç

% foreach j { 1 3 5} {

put $j;

}

1

3

5

º¯Êý

ÈçͬÔÚÒ»°ãµÄ±à³ÌÓïÑÔÀïÃæÒ»Ñù£¬ÔÚtclÀïÃæÒ²¿ÉÒÔ¶¨Ò庯Êý£¬ÕâÊÇͨ¹ýprocÃüÁîʵÏֵģº

proc my_proc {i}{

puts $i;

}

ÕâÑù¾Í¶¨ÒåÁËÒ»¸öÃû×Ö½ÐprocµÄº¯Êý£¬ËüÖ»ÊÇÔÚÖÕ¶ËÏÔʾÊäÈë±äÔªµÄÄÚÈÝ¡£

ҪʹÓÃÕâ¸öº¯Êý£¬¼òµ¥µØÊäÈëËüµÄÃû×Ö£º

% my_proc { 5 }

5

Èç¹û±äÔªµÄÊýÄ¿ÊÇ0£¬Ö»ÒªÊ¹ÓÿյıäÔªÁÐ±í£¬ÀýÈç proc my_proc {} {Óï¾ä£»}


¾¡¹Ütcl»¹¿ÉÒÔ´¦Àí¸ü¸´ÔӵĹý³Ì£¬µ«ÊÇÎÒÃDz»ÔÙ½éÉÜÁË£¬ÀýÈçÎļþµÄ¶ÁдÒÔ¼°tkͼÐÎÓïÑÔ£¬ÒòΪÎÒÃÇ´¦ÀítclµÄÖ÷ҪĿ±ê¾ÍÊÇÀí½âexpect£¬¶ÔÓÚ¸ü¸´Ôӵıà³Ì¹¤×÷£¬ÎÒÃǽ¨ÒéÄãʹÓÃperl¡£

11.1.2 expect

expectÊǽ¨Á¢ÔÚtcl»ù´¡ÉϵÄÒ»¸ö¹¤¾ß£¬ËüÓÃÀ´ÈÃһЩÐèÒª½»»¥µÄÈÎÎñ×Ô¶¯»¯µØÍê³É¡£ÎÒÃÇÊ×ÏÈ´ÓÒ»¸ö¼òµ¥µÄÀý×Ó¿ªÊ¼£¬ÈçͬÔÚÕâÒ»½ÚÒ»¿ªÊ¼¾ÍÌáµ½µÄ£¬ÎÒÃÇÏëÉèÖÃÒ»¸ö×Ô¶¯µÄÎļþÏÂÔسÌÐò¡£

ÎÒÃÇ¿´Ò»¿´ÕâÑùµÄÒ»¸öÀý×ӽű¾£º

#! /usr/bin/expect

spawn ftp 202.199.248.11

expect "Name"

send "ftp\r"

expect "Password:"

send "nothing\r"

expect "apply"

send "cd /pub/UNIX/Linux/remoteX\r"

expect "successful."

send "bin\r"

expect "set to I"

send "get exceed5.zip\r"

expect "complete."

send "quit\r"

Õâ¸öÊÇʲôÒâ˼£¿ºÇºÇ£¬¾ÍÊǸö×Ô¶¯ÏÂÔسÌÐò¡£µÚÒ»ÐÐ˵Ã÷Õâ¸ö³ÌÐòÓ¦¸Ãµ÷ÓÃ/usr/bin/expectÈ¥Ö´ÐУ¬È»ºóµÄ¾ÍÊÇexpectÃüÁî¡£

²ì¿´expectµÄÊÖ²áÒ³Ãæ(man expect)¿ÉÒԵõ½Ò»¸öºÜ³¤µÄexpect˵Ã÷£¬¿ÉϧÆäÖйØÓÚexpectµÄÓï·¨ÈÔÈ»½éÉܵIJ»¹»¡£Ò»°ãÀ´Ëµ£¬ expectÖ÷ÒªÓÃÔÚÐèÒª×Ô¶¯Ö´ÐÐÈË»ú½»»¥µÄ¹ý³ÌÖУ¬ÀýÈçfsck³ÌÐò£¬Õâ¸ö³ÌÐò»á²»¶ÏµØÌáÎÊ"yes/no"£¬ÏñÕâÑùµÄÃüÁî¾Í¿ÉÒÔÓÃexpect À´Íê³É¡£

spawnÓï¾äÔÚexpect½Å±¾ÖÐÓÃÓÚÆô¶¯Ò»¸öеĽø³Ì£¬ÔÚÎÒÃǵijÌÐòÖУ¬spawn ftp 202.199.248.11¾ÍÊÇÈ¥Ö´ÐÐftp³ÌÐò£¬½ÓÏÂÀ´£¬¾ÍÊÇexpectºÍsendµÄÖ¸Áî¶ÔÁË¡£

ÿһ¶ÔexpectºÍsendÖ¸Áî´ú±íÒ»¸öÐÅÏ¢/»ØÓ¦¡£Èç¹ûÕâÑù˵²»ºÃÀí½âµÄ»°£¬ÄÇô¿ÉÒÔ¿´Ò»¿´ftpµÄ¾ßÌåÖ´Ðйý³Ì£º

ftp 202.199.248.11

Connected to 202.199.248.11.

220 mail.asnc.edu.cn FTP server (BeroFTPD 1.3.3(3) Sun Feb 20 15:52:49 CST 2000.

Name (202.199.248.11:wanghy):

ÏÔÈ»£¬Ò»µ©Á¬½Ó³É¹¦£¬·þÎñÆ÷»á·µ»ØÒ»¸öName(202.199.248.11:wanghy):µÄ×Ö·û´®À´ÒªÇó¿Í»§¸ø³öÓû§Ãû¡£expectÓï¾ä¼òµ¥µØÔÚ·µ»ØÐÅÏ¢ÖвéѯÄã¸ø³öµÄ×Ö·û´®£¬Ò»µ©³É¹¦¾ÍÖ´ÐÐÏÂÃæµÄÃüÁÏÖÔÚ£¬expect " Name"ÒѾ­³É¹¦µØÕÒµ½ÁËName×Ö·û´®£¬½ÓÏÂÀ´¿ÉÒÔÖ´ÐÐ sendÃüÁîÁË¡£

sendÃüÁî±ÈexpectÃüÁî¸ü¼òµ¥£¬Ëü¼òµ¥µØÏò±ê×¼ÊäÈëÌá½»ÄãÉ趨µÄ×Ö·û´®£¬ÏÖÔÚÉèÖÃΪsend "ftp\r"±íʾµÈµ½µÇ¼ÐÅÏ¢Ö®ºó¾Í¸ø³öÒ»¸öÊäÈëftp»Ø³µ£¬Ò²¾ÍÊDZê×¼µÄµÇ¼¹ý³Ì¡£

ÏÂÃæµÄÐÐÓëÕâЩÐÐÍêÈ«Ò»Ñù£¬Ö»ÊÇ»úеµØµÈ´ý·þÎñÆ÷µÄ»ØÓ¦£¬²¢ÇÒÌá½»×Ô¼ºµÄÊäÈë¡£

ҪʹÓÃÕâ¸öexpect½Å±¾£¬ÄãÖ»ÐèÒª½«ËüÉèÖÃΪ¿ÉÖ´ÐеÄÊôÐÔ£¬È»ºóÖ´ÐÐËü£¬expect¾Í»áÖ´ÐÐÄãÐèÒªµÄ·þÎñ¡£

ÓÉÓÚexpectÊÇtclµÄÀ©Õ¹£¬ËùÒÔÄãÔÚexpectÎļþÖпÉÒÔÏótcl½Å±¾Ò»ÑùÉèÖñäÁ¿ºÍ³ÌÐòÁ÷³Ì¡£

ÏÖÔÚÎÒÃÇ¿´Ò»¿´ÎÒÃÇ»¹Äܹ»ÈçºÎ¸Ä½øÎÒÃǵÄexpect½Å±¾¡£ftpÃüÁî¿ÉÄÜ»áʧ°Ü£¬±ÈÈçÔ¶¶ËµÄ»úÆ÷¿ÉÄÜ»áÎÞ·¨Ìṩ·þÎñ£¬»òÕßÔÚÆô¶¯ftpÃüÁîʱ±¾µØ»úÆ÷·¢ÉúÎÊÌ⡣ΪÁË´¦ÀíÕâÒ»ÀàµÄÎÊÌ⣬ÎÒÃÇ¿ÉÒÔʹÓÃexpectµÄtimeoutÑ¡ÏîÀ´ÉèÖó¬Ê±µÄ»°expect½Å±¾×Ô¶¯Í˳ö£º

#! /usr/bin/expect

spawn ftp 202.199.248.11

expect {

timeout exit

Connect

}

¡­¡­¡­¡­¡­¡­

×¢ÒâÕâÀïÃæʹÓõĻ¨À¨ºÅ¡£ËüµÄº¬ÒåÊÇʹÓÃÒ»×é²¢Áбí´ïʽ¡£Ê¹Óò¢Áбí´ïʽµÄÖ÷ÒªÔ­ÒòÊÇÕâÑù£ºÈç¹ûʹÓÃÏÂÃæµÄÖ¸Áî¶Ô£º

expect timeout

exit

ÄÇôÓÉÓÚexpect½Å±¾ÊÇ˳ÐòÖ´Ðеģ¬ÄÇôµ±³ÌÐòÖ´Ðе½Õâ¸öexpectµÄʱºò¾Í»á×èÈû£¬ËùÒÔ³ÌÐò»áÒ»Ö±µÈ´ýµ½timeoutÈ»ºóÍ˳ö¡£²¢Áбí´ïʽÔòÊÇÏ൱ÓÚswitchµÄÐÐΪ£¬Ö»ÒªÁгöµÄ¼¸ÏîÄÚÈÝÓÐÒ»ÏîµÃµ½Âú×㣬expectÃüÁî¾ÍµÃµ½Âú×㣬ÓÚÊdzÌÐò¿ÉÒÔÕý³£Ö´ÐС£ÉÏÃæµÄ½Å±¾±íʾ£¬Èç¹ûÁ¬½ÓftpµÄʱºò·¢ÉúÁ˳¬Ê±£¬ÄÇô¾ÍÍ˳ö£¬·ñÔò£¬Ò»µ©·¢ÏÖConnectÓ¦´ð£¬ËµÃ÷·þÎñÆ÷ÒѾ­Õý³£ÁË£¬ÄÇô¾Í¿ÉÒÔ¼ÌÐøÔËÐÐÁË¡£

ÎÒÃÇ¿ÉÒÔ¿´¿´ÓÃtclÄܹ»¶ÔÎÒÃǵÄexpect½Å±¾Ìṩʲô°ïÖú¡£ÎÒÃÇ¿ÉÒÔÉèÖÃÈÃexpect½Å±¾²»¶ÏµØÁ¬½ÓÔ¶¶Ë·þÎñÆ÷µÄ·þÎñ£¬Ö±µ½Õý³£½¨Á¢Á¬½Ó¿ªÊ¼£¬Îª´Ë£¬ÎÒÃÇ¿ÉÒ԰ѽ¨Á¢Á¬½ÓµÄÃüÁî·ÅÔÚÒ»¸öÑ­»·ÀïÃ棬²¢ÇÒ¸ù¾Ý»ØÓ¦µÄ²»Í¬×Ô¶¯Ñ¡ÔñÖØÐÂÊäÈëÃüÁÊǼÌÐøÖ´ÐУº

spawn ftp

while {1} {

expect "ftp>"

send "o 202.199.248.11\r"

expect {

"Connected" break

"refused" { sleep 10} ;

}

}

ÕâÀïʹÓÃÁËÎÒÃÇÔÚtclÓïÑÔÖн²µ½µÄwhileºÍbreakÃüÁÊìϤCµÄ¶ÁÕßÓ¦¸ÃºÜÈÝÒ׿´³öËüµÄÐÐΪ£º²»¶ÏµØµÈ´ýftp>Ìáʾ·û£¬ÔÚÌáʾ·ûÏÂÃæ·¢ËÍÁ¬½ÓÔ¶¶Ë·þÎñÆ÷µÄÃüÁÈç¹û·þÎñÆ÷»ØÓ¦ÊÇrefused£¨Á¬½Óʧ°Ü£©£¬¾ÍµÈ´ý10ÃëÖÓ£¬È»ºó¿ªÊ¼ÏÂÒ»´ÎÑ­»·£»Èç¹ûÊÇConnected£¬ÄÇô¾ÍÌø³öÑ­»·Ö´ÐÐÏÂÃæµÄÃüÁî¡£sleepÊÇexpectµÄÒ»¸ö±ê×¼ÃüÁ±íʾÔÝÍ£Èô¸ÉÃëÖÓ¡£

expect»¹Ö§³ÖÐí¶à¸ü¸´ÔӵĽø³Ì¿ØÖÆ·½Ê½£¬Èçfork£¬disconnectµÈµÈ£¬Äã¿ÉÒÔ´ÓÊÖ²áÒ³ÃæÖеõ½ÏêϸµÄÐÅÏ¢¡£ÁíÍ⣬¸÷ÖÖtclÔËËã·ûºÍÁ÷³Ì¿ØÖÆÃüÁ°üÀ¨tclº¯ÊýÒ²¿ÉÒÔʹÓá£

ÓÐЩ¶ÁÕß¿ÉÄÜ»áÎÊ£¬Èç¹ûexpectÖ´ÐеĻ°ÊÇ·ñ¿ØÖÆ̨ÊäÈë²»ÄÜʹÓÃÁË£¬´ð°¸ÊÇ·ñ¶¨µÄ¡£expectÃüÁîÔËÐÐʱ£¬Èç¹ûij¸öµÈ´ýµÄÐÅϢûÓеõ½£¬ÄÇô³ÌÐò»á×èÈûÔÚÏàÓ¦µÄexpectÓï¾ä´¦£¬Õâʱ£¬ÄãÔÚ¼üÅÌÉÏÊäÈëµÄ¶«Î÷ÈÔÈ»¿ÉÒÔÕý³£µØ´«µÝµ½³ÌÐòÖÐÈ¥£¬Æäʵ¶ÔÓÚÄÇЩexpect´¦ÀíµÄÐÅÏ¢£¬Ô­ÔòÉÏÄãÊäÈëµÄÄÚÈÝÈÔÈ»ÓÐЧ£¬Ö»ÊÇexpectµÄ·´Ó³Ì«¿ì£¬×ÜÊÇÇÀÔÚÄãµÄÇ°Ãæ¡°ÊäÈ롱¾ÍÊÇÁË¡£ÖªµÀÁËÕâÒ»µãÖ®ºó£¬Äã¾Í¿ÉÄÜдһ¸öexpect½Å±¾£¬ÈÃexpect×Ô¶¯´¦ÀíÀ´×Ô fsckiµÄÄÇЩ¶ñÐĵÄyes/noÑ¡ÏÎÒÃǽéÉܹý£¬ÕâЩyes/noÆäʵÍêÈ«ÊǶàÓàµÄ£¬Õý³£Çé¿öÏÂÄã³ýÁËÑ¡ÔñyesÖ®ÍâʲôҲ¸É²»ÁË£©¡£

ȱʡÏ£¬expectÔÚ±ê×¼Êä³ö£¨ÄãµÄÖÕ¶ËÉÏ£©Êä³öËùÓÐÀ´×ÔÓ¦ÓóÌÐòµÄ»ØÓ¦ÐÅÏ¢£¬Äã¿ÉÒÔÓÃÏÂÃæµÄÁ½¸öÃüÁîÖض¨ÏòÕâЩÐÅÏ¢£º

log_file [ÎļþÃû]

Õâ¸öÃüÁîÈÃexpectÔÚÄãÉèÖõÄÎļþÖмǼÊä³öÐÅÏ¢¡£±ØÐë×¢Ò⣬Õâ¸öÑ¡Ïî²¢²»Ó°Ïì¿ØÖÆ̨Êä³öÐÅÏ¢£¬²»¹ýÈç¹ûÄãͨ¹ýcrondÉèÖÃexpect½Å±¾ÔÚ°ëÒ¹ÔËÐеĻ°£¬Äã¾Íȷʵ¿ÉÄÜÐèÒªÕâ¸öÃüÁîÀ´¼Ç¼¸÷ÖÖÐÅÏ¢ÁË¡£ÀýÈ磺

log_file expect.log

log_user 0/1

Õâ¸öÑ¡ÏîÉèÖÃÊÇ·ñÏÔʾÊä³öÐÅÏ¢£¬ÉèÖÃΪ1ʱÊÇȱʡֵ£¬Îª0 µÄ»°£¬expect½«²»²úÉúÈκÎÊä³öÐÅÏ¢£¬»òÕß˵¼òµ¥µØ¹ýÂ˵ô¿ØÖÆ̨Êä³ö¡£±ØÐë¼Çס£¬Èç¹ûÄãÓÃlog_user 0¹Ø±ÕÁË¿ØÖÆ̨Êä³ö£¬ÄÇôÄãͬʱҲ¾Í¹Ø±ÕÁ˶ԼǼÎļþµÄÊä³ö¡£

ÕâÒ»µãºÜÈÃÈËÀ§ÈÅ£¬Èç¹ûÄãȷʵÏëÒª¼Ç¼expectµÄÊä³öÈ´²»ÏëÈÃËüÔÚ¿ØÖÆ̨ÉÏÖÆÔìÀ¬»øµÄ»°£¬Äã¿ÉÒÔ¼òµ¥µØ°ÑexpectµÄÊä³öÖض¨Ïòµ½/dev/null£º

./test.exp > /dev/null

Äã¿ÉÒÔÏóÏÂÃæÕâÑùʹÓÃÒ»¶ÔforkºÍdisconnectÃüÁî¡£expectµÄdisconnectÃüÁʹµÃÏàÓ¦µÄ½ø³Ìµ½ºǫִ́ÐУ¬ÊäÈëºÍÊä³ö±»Öض¨Ïòµ½/dev/null£º

if [fork]!=0 exit

disconnect

forkÃüÁî»á²úÉú³öÒ»¸ö×Ó½ø³Ì£¬¶øÇÒËü²úÉú·µ»ØÖµ£¬Èç¹û·µ»ØµÄÊÇ0£¬ËµÃ÷ÕâÊÇÒ»¸ö×Ó½ø³Ì£¬Èç¹û²»Îª0£¬ÄÇôÊǸ¸½ø³Ì¡£Òò´Ë£¬Ö´ÐÐÁËforkÃüÁîÖ®ºó£¬¸¸½ø³ÌËÀÍö¶ø×Ó½ø³Ì±»disconnectÃüÁî·Åµ½ºǫִ́ÐС£×¢ÒâdisconnectÃüÁîÖ»ÄܶÔ×Ó½ø³ÌʹÓá£

11.2 awkºÍÎļþµÄ´¦Àí

UNIXÀïÃæ³ä³âן÷ÖּǼÎļþºÍÀàËƵĶ«Î÷¡£¶ÔÎı¾ÎļþµÄ´¦ÀíÊÇϵͳ¹ÜÀíԱÿÌìÖØÒªµÄ¹¤×÷£¬ÀýÈç´Óϵͳ¼Ç¼ÖвéÕÒÖØÒªµÄÄÚÈÝ£¬»òÕ߶ÔijÖÖ³ÌÐòµÄÊä³ö½øÐÐͳ¼ÆµÈµÈ¡£ÎÒÃǽ«½éÉܳ£ÓõÄÒ»¸ö´¦Àí³ÌÐò£¬¼´gawk¡£

11.2.1 grepºÍÕýÔò±í´ïʽ

ÈÃÎÒÃÇÊ×ÏÈ´ÓgrepÃüÁʼ¡£Õâ¸öÃüÁî´ó¼ÒÓ¦¸ÃºÜÊìϤÁË£¬ËüÓÃÀ´ÔÚÎļþÖвéÕÒÒ»¸ö×Ö·û´®¡£²»¹ý£¬Êµ¼ÊÉÏ£¬grepµÄ´¦Àí¹¦ÄÜҪǿ´óºÍ¸´ÔӵĶࡣ

grep ÃüÁîµÄÓï·¨ÊÇ

grep [ģʽ] [ÎļþÃû]

Èç¹ûûÓиø³öÎļþÃû£¬¾ÍȱʡʹÓñê×¼ÊäÈë¡£grepÿ´Î¶ÁÈ¡Ò»ÐУ¬²¢ÇҺ͸ø³öµÄģʽ½øÐÐÆ¥Å䣬Èç¹û³É¹¦¾Í°ÑÕâÒ»ÐлáÏÔ£¬ÀýÈ磺(´ÖÌåµÄÊÇÎÒÃÇÊäÈëµÄÄÚÈÝ)

$ grep test

close

test my hand

test my hand

grepµÄ¡°Ä£Ê½¡±Ò²³ÆΪÕýÔò±í´ïʽ£¬¿ÉÒÔÓɸ÷ÖÖ»ù±¾µÄÕýÔò±í´ïʽԪËع¹³É¡£ÕýÔò±í´ïʽԪËØÖ÷Òª°üÀ¨ÏÂÃ漸ÖÖ£º

×Ö·û´® Æ¥ÅäÈκÎ×Ö·û´®£¬ÀýÈçgrep test±íʾÔÚ±ê×¼ÊäÈëÖÐ1

[...] ·â±Õ¼¯ÖÐÆ¥ÅäÒ»¸ö×Ö·û£¬Èç:[abcde]¿ÉÒÔÆ¥Åäa,b,c,d,e

[^...] Çó²¹¼¯ÖÐÆ¥ÅäÒ»¸ö×Ö·û£¬ÀýÈç[^ABC]Æ¥Åä

. Æ¥ÅäÈÎÒâ×Ö·û

\s ¿Õ°×·û

\S ·Ç¿Õ°×·û

\d Êý×Ö

\D ·ÇÊý×Ö

\w ×Öĸ»òÊý×Ö

\W ·Ç×ÖĸºÍÊý×Ö

* Æ¥ÅäÈκÎ×Ö·û

ÉÏÃæµÄÐÎʽÊÇgrepÖÐʹÓõĻù±¾ÕýÔò±í´ïʽ£¬ÁíÍ⣬»¹¿ÉÒÔʹÓÃegrep£¬egrepÊÇgrepµÄÒ»¸öÀ©Õ¹°æ±¾£¬Ö§³ÖÏÂÃæÕâЩÀ©Õ¹µÄÕýÔò×Ö·û´®£º

^ Æ¥ÅäÒ»ÐеĿªÊ¼

$ Æ¥ÅäÒ»ÐеĽáβ

( ) È·¶¨ÕýÔò±í´ïʽÇóֵ˳Ðò£¬ºÍÕý³£ÑÝËãÖеÄÀ¨ºÅÒâ˼²î²»¶à¡£

(...|...|...) »ò,¿ÉÑ¡ÏîÖ®Ò»½øÐÐÆ¥Å䣬ÀýÈç:(abc|dev|ghi)¿ÉÒÔÆ¥Åäabc,dev,ghi£¬¶ø(ww|gg)do¿ÉÒÔÆ¥Åäwwdo»òÕßggdo¡£

+ Ò»´Î»ò¶à´Îģʽ

Èç:aba+Æ¥Åäaba,abaa...²»Æ¥Åäab

ͨ³££¬ÎÒÃÇÓÐÁ½ÖÖ·½·¨Ê¹ÓÃgrepºÍegrep£¬Ò»ÖÖÊÇʹÓùܵÀ£¬ÀýÈçÎÒÃÇÓ¦¸ÃÊìϤµÄps ax |grep sendmail£¬ÁíÒ»ÖÖÊÇÖ±½ÓÔÚÎļþÖÐËÑË÷¶ÔÓ¦µÄ×Ö·û´®¡£

grep/egrep»¹¿ÉÒÔÔÚÃüÁîÐÐʹÓÿª¹Ø£¬³£ÓõĿª¹Ø°üÀ¨£º

-b ÔÚÐÐÇ°¼ÓÉÏ¿éºÅ

-c ͳ¼ÆÆ¥ÅäÐеĸöÊý

-n ÔÚÐÐÇ°¼ÓÉÏÐкÅ

-w ½«Ä£Ê½½âÊÍΪ×Ö·û´®£¬ËùÓÐÕýÔò±í´ïʽµÄ¿ØÖÆÃüÁîʧЧ

-x ¾«È·Æ¥Åä

-r ²éѯÎļþʱ°üº¬×ÓĿ¼

¾Ù¸öÀý×ÓÀ´Ëµ£¬ÎÒÃÇÏëÔÚ/var/log/httpd/access_logÖвéѯËùÓв»ÊÇÀ´×Ô±¾µØ£¨192.168.0.1£©µÄÇëÇó¼Ç¼£¬¿ÉÒÔÖ´ÐУº

grep -v "^192.168.0.1" /var/log/httpd/access_log

^ÓÃÀ´ÈÃgrep Ö»ÔÚÐÐÊ×Æ¥Åä¡£

ÔÚgrep²éѯµÄʱºò¿ÉÒÔʹÓÃͨÅä·û´ú±í¶à¸öÎļþ£¬ÀýÈ磬grep start * -r½«ÔÚµ±Ç°Ä¿Â¼ÒÔ¼°ËùÓÐ×ÓĿ¼µÄËùÓÐÎļþÖвéѯstart×Ö·û´®¡£

11.2.2 gawkµÄʹÓ÷½·¨

gawkÊÇawkµÄÒ»¸öʵÏÖ£¬awkÊÇÒ»ÖÖÓÃÀ´´¦Àí±¨¸æµÈÎı¾ÎļþµÄ½Å±¾ÓïÑÔ¡£²»¹ý£¬ÎÒÃǽéÉÜÕâ¸ö²úÆ·µÄÖ÷ҪĿ±êÊÇÓÃËüÀ´´¦Àí¸÷ÖÖ³ÌÐòµÄ¼ÇÕËÎļþ¡£¶ÔÓÚ¸´ÔӵĽű¾£¬»¹ÊÇÓÃPerl±È½ÏºÏÊÊ¡£

gawk µÄÖ÷Òª¹¦ÄÜÊÇÕë¶Ôµµ°¸µÄÿһÐÐËÑÑ°Ö¸¶¨µÄ ģʽ¡££¬Ã¿µ±ÕÒµ½Ò»¸öÆ¥ÅäµÄģʽ£¬gawk¾Í»áÈ¥Ö´ÐÐÄãÉ趨µÄ¶¯×÷¡£°´ÕÕÕâ¸ö·½Ê½£¬ gawk ÒÀ´Ë·½Ê½´¦ÀíÊäÈëµµ°¸µÄÿһÐÐÖ±µ½ÊäÈëµµ°¸½áÊø¡£Èç¹û¶ÔÓÚij¸öģʽûÓÐÉèÖöÔÓ¦µÄ¶¯×÷£¬gawk½«Ö±½Ó½«Õâ¸öÐÐÏÔʾ³öÀ´¡£

ΪÁËʹÓÃgawk£¬Äãͨ³£±ØÐëÏÈдһ¸öawk½Å±¾£¬³ý·Çģʽ/¶¯×÷·Ç³£¼òµ¥£¬¿ÉÒÔÔÚÒ»ÐÐÉÏÍê³É¡£ÎÒÃÇÓÃÒ»¸öÀý×ÓÀ´½âÊÍgawkµÄ»ù±¾Ó÷¨£¬Ê×ÏȲúÉúÒ»¸öĿ¼ÁбíÎļþ£º

ls -l /etc > list

ÏÖÔÚlistµÄÄÚÈÝÓеãÏñÕâÑù£º

total 2164

drwxr-xr-x 3 root root 4096 Feb 15 22:55 CORBA

-rw-r--r-- 1 root root 2045 Sep 24 1999 DIR_COLORS

-rw-r--r-- 1 root root 17 Mar 25 19:59 HOSTNAME

¡­¡­¡­¡­

ÏÖÔÚÎÒÃÇÑ¡ÔñÒ»¸ö×î¼òµ¥µÄÀý×Ó£¬¼òµ¥µØ²éÕÒËùÓÐÊôÐÔÊÇdrwxr-xr-xµÄĿ¼Îļþ£º

gawk '/drwxr-xr-x/ {print $0}' list

½«Êä³öËùÓÐÕâÑùµÄĿ¼¡£

Õâ¸öÀý×Ó¿´ÉÏȥûÓÐʲôʵ¼ÊÓô¦£¬ÒòΪÓÃgrepÒ²¿ÉÒÔ×öͬÑùµÄ¶¯×÷£¬ÄÇôÎÒÃÇ¿ÉÒÔ¿´Ò»¿´ÏÂÃæÕâ¸ö¹¦ÄÜ£º

$ gawk '$1=="-rwxr-xr-x" {sum=sum+$5} END {print sum}' list

15041

Õâ¸öÊÇʲôÒâ˼£¿¶ÔÓÚËùÓÐÊôÐÔÊÇ755µÄÎļþ£¬ÈÃgawk¶ÔµÚÎåÀ¸µÄÊý×ÖÇóºÍ¡£µÚÎåÀ¸ÎÒÃÇ¿ÉÒÔ¿´µ½¾ÍÊÇÎļþµÄ³¤¶È£¬Òò´ËÕâ¸öÃüÁÏÔʾËùÓÐÊôÐÔΪ755µÄÎļþµÄ×ܹ²µÄ³¤¶È¡£

$nÊÇgawkÖзdz£ÖØÒªµÄ¸ÅÄËüÓÃÀ´±íʾÎı¾´®µÄ·ÖÀ¸¡£È±Ê¡µÄÇé¿öÏ£¬gawk½«ÊäÈë×Ö·û´®£¨´ÓÎļþÖжÁÈëµÄÿһÐУ©°´ÕÕ·Ö¸îµÄ¿Õ¸ñ·Ö³ÉÈô¸É¸ö×ֶΣ¬Ã¿¸ö×Ö¶Î×÷Ϊһ¸ö±äÁ¿£¬ÀýÈçÓÐÒ»ÐÐ

my name is 3th test

ÄÇô£¬ÔÚawk¶ÁÈëÕâÒ»ÐÐÖ®ºó£¬¾Í²úÉúÁË$1µ½$5±äÁ¿£¬ÆäÖÐ$1="my"£¬$2="is"£¬¡­¡­¡­ £¬×îºó$5="test"¡£ÁíÍ⻹ÓÐÒ»¸öÌØÊâµÄ±äÁ¿$0£¬Ëü±íʾÕû¸öÊäÈëÐУ¬Ò²¾ÍÊÇÕâ¸ö×Ö·û´®"my name is test"¡£ÁíÍ⻹ÓÐÒ»¸öÌØÊâµÄ±äÁ¿NF£¬Ëü±íʾµ±Ç°ÐеÄ×ֶεĸöÊý£¬ÔÚÏÖÔÚµÄÇé¿öÏ£¬NFÓ¦¸ÃµÈÓÚ5¡£

ÔÚijЩÌØÊâµÄÇé¿öÏ£¬Äã¿ÉÄÜÐèÒª¸Ä±ä·Ö¸î·ûµÄ¶¨Ò壬Õâ¿ÉÒÔͨ¹ý¶ÔFS¸³ÖµÀ´Íê³É£¬ÀýÈçFS=","½«·Ö¸î·û¶¨ÒåΪ¶¼ºÅ¶ø²»ÊÇȱʡµÄ¿Õ¸ñ¡£

ÔÚÒ»°ãÇé¿öÏ£¬gawk¿ÉÒÔ´ÓÃüÁîÎļþÖлñµÃģʽ/¶¯×÷£¬ÃüÁîÎļþµÄ¸ñʽºÜ¼òµ¥£¬¾ÍÊÇÖ±½Ó½«Ó¦¸ÃдÔÚÃüÁîÐÐÉϵÄģʽ/¶¯×÷¶ÔдÔÚÎļþÀïÃ棬ÿ¸ö¶Ô¹¹³ÉÒ»ÐУ¬Ä£Ê½¿ÉÒÔÓÐÁ½ÖÖ£¬Ò»ÖÖÊÇģʽƥÅ䣬Ҳ¾ÍÊÇÎÒÃÇÔÚÇ°Ãæ½âÊ͵ÄÕýÔò±í´ïʽ£¬Èç¹ûʹÓÃÕýÔò±í´ïʽ£¬ÄÇôÐèÒªÓÃÁ½¸ö/°ÑËüÃǼÐÔÚÒ»Æð£¬ÀýÈç/[A-Z]/±íʾÕýÔò±í´ïʽ[A-Z]¡£

ÁíÒ»ÖÖģʽÊDZȽÏÖ¸Á±È½ÏÖ¸Áî¿ÉÒÔÓñȽϲÙ×÷·ûºÍÂß¼­ÔËËã·ûÀ´¹¹³É£¬³£ÓõıȽϲÙ×÷·ûÓУº

== µÈÓÚ <= ²»´óÓÚ ~ °´ÕÕÕýÔò±í´ïʽƥÅä

< СÓÚ >= ²»Ð¡ÓÚ !~ °´ÕÕÕýÔò±í´ïʽ²»Æ¥Åä

> ´óÓÚ != ²»µÈÓÚ

Âß¼­ÔËËã·ûÓÐ

&& ºÍ || »ò ! ·Ç ()À¨ºÅ


É趨ÁËģʽºó£¬¾Í¿ÉÒÔÉèÖöÔÓ¦µÄ¶¯×÷ÁË£¬ÔÚgawkÖУ¬¶¯×÷±ØÐëÓû¨À¨ºÅÀ¨ÆðÀ´¡£ga
wkÄÜÍê³ÉµÄ¶¯×÷²¢²»¶à£¬±Ï¾¹ËüÊÇÒ»ÖÖ±¨¸æ·ÖÎöÓïÑÔ¡£Ò»°ãÇé¿öÏ£¬Ö»ÒªÊìϤprintºÍp
rintfÃüÁî¾Í×ã¹»ÁË£¬printÃüÁîµÄ¸ñʽ·Ç³£¼òµ¥£º

print item1,item2,¡­¡­¡­¡­

Êä³öʱ£¬Ã¿¸öÏîÄ¿Êä³öÒ»À¸£¬ÖмäÓÿոñ·Ö¿ª¡£Ò»¸öprintºóÃæ²»¸ú×ÅÈκαäÁ¿»áµ¼ÖÂgawkÏÔʾµ±Ç°µÄÊäÈëÐУ¨$0£©¡£Èç¹ûÒªÊä³öÒ»¸ö×Ö·û´®£¬Ê¹ÓÃÒýºÅ°ÑËüÀ¨ÆðÀ´£¬ÌرðÊÇÈç¹ûÒªÊä³öÒ»¸ö¿ÕÐУ¬Ê¹ÓÃprint ""¡£ÕâÀïÊÇÒ»¸öÀý×Ó£¬Ëü½«listÎļþµÄÍ·Á½À¸Êä³ö£º

gawk '{print $1,$2}' list

ÓÉÓÚÊäÈëµÄÎı¾ÎļþÄÚÈÝÓжàÐУ¬ÄãÔÚÃüÁîÀ¸ÖÐÉè¼ÆµÄģʽ/¶¯×÷»á¶ÔÿһÐÐÖ´ÐÐÒ»´Î¡£¾ÍÊÇ£º

total 2164

drwxr-xr-x 3

-rw-r--r-- 1

-rw-r--r-- 1

-rw-r--r-1

¡­¡­¡­¡­¡­¡­¡­

Èç¹ûÄãÒª¾«È·µØ¿ØÖÆÊä³ö£¬Ò²¿ÉÒÔʹÓÃprintfÃüÁÕâ¸öÃüÁîµÄ¸ñʽÊÇ£º

printf format, item1, item2, ...

format²ÎÊý¾ÍÊÇCÓïÑÔÀïÃæµÄ¸ñʽ¿ØÖÆ·û£¬ÀýÈç%c£¬%d£¬%fµÈµÈ¡£ÔÚ % Óë¸ñʽ¿ØÖÆ×Öĸ֮¼ä¿É¼ÓÈë modifier£¬modifier ÊÇÓÃÀ´½øÒ»²½¿ØÖÆÊä³öµÄ¸ñʽ¡£¿ÉÄÜµÄ modifier ÈçÏÂËùʾ£º

'-' ʹÓÃÔÚ width ֮ǰ£¬Ö¸Ã÷ÊÇÏò×ó¿¿Æë¡£Èç¹û'-'ûÓгöÏÖ£¬Ôò»áÔÚ±»Ö¸¶¨µÄ¿í¶ÈÏòÓÒ¿¿Æë¡£ÀýÈ磺

printf "%-4S", "foo"»áÓ¡³ö'foo '¡£

'width' ÕâÒ»¸öÊý×ÖָʾÏà¶ÔÓ¦µÄÀ¸Î»Ó¡³öʱµÄ¿í¶È¡£ÀýÈ磺

printf "%4s","foo" »áÓ¡³ö' foo'¡£

width µÄÖµÊÇÒ»¸ö×îС¿í¶È¶ø·Ç×î´ó¿í¶È¡£Èç¹ûÒ»¸ö item µÄÖµÐèÒªµÄ¿í¶È±È width ´ó£¬Ôò²»ÊÜ width µÄÓ°Ïì¡£ÀýÈçprintf "%4s","foobar"½«Ó¡³ö'foobar'¡£

'.prec' ´ËÊý×ÖÖ¸¶¨Ó¡³öʱµÄ¾«È·¶È¡£ËüÖ¸¶¨Ð¡ÊýµãÓұߵÄλÊý¡£Èç¹ûÊÇÒªÓ¡³öÒ»¸ö×Ö´®£¬ËüÖ¸¶¨´Ë×Ö´®×î¶à»á±»Ó¡³ö¶àÉÙ¸ö×Ö·û¡£

×÷ΪһÖֽű¾ÓïÑÔ£¬gawkÔÊÐíʹÓñäÁ¿£¬¶¨Òå±äÁ¿·Ç³£¼òµ¥£¬¾ÍÊÇÖ±½ÓÓõȺŶÔËü¸³Öµ¡£ÎªÁËÔÚgawk³ÌÐòµÄ¿ªÊ¼´¦¶Ô±äÁ¿¸³Öµ£¬gawkרÃÅÌṩÁËBEGINÓï¾ä£¬Õâ¸öÓï¾ä½«ÔÚËùÓÐÐỶÁÈë֮ǰִÐУ¬¶øÇÒÖ»Ö´ÐÐÒ»´Î£¬Í¨³£ÓÃËüÀ´Ö´Ðгõʼ»¯ÃüÁÀýÈç

BEGIN { sum=0;count=0;average=0.0;}

¶ÔÓÚ±äÁ¿¿ÉÒÔʹÓÃÊýѧ±í´ïʽ½øÐÐÔËË㣬ÔËËã·û°üÀ¨³£¼ûµÄ¼Ó¼õ³Ë³ýËã·û£¬ÒÔ¼°^(³Ë·½)£¬%(È¡Óà)ºÍÖøÃûµÄ++£¬--¡£²»¹ý×¢ÒâgawkÔÚ×ö³ý·¨µÄʱºò×ÜÊÇʹÓø¡µã³ý·¨£¬³ýÁËÈ¡ÓàËã·û%¡£

º¯Êý

ÁíÍ⣬gawk°üº¬ÏÂÁк¯Êý£º

Êýѧº¯Êý

atan2(x,y) y/xµÄÕýÇÐ

cos(x) ÓàÏÒº¯Êý

sin(x) ÕýÏÒº¯Êý

int(x) È¡Õû

log(x) È¡×ÔÈ»¶ÔÊý

exp(x) Ö¸Êýº¯Êý

rand(x) Éú³ÉÒ»¸ö0µ½1Ö®¼äµÄËæ»úÊý

srand() ³õʼ»¯Ëæ»úÊý·¢ÉúÆ÷

systime() ·µ»Ø´Ó1970Äê1ÔÂ1ÈÕ0:00µ½µ±Ç°Ê±¼äµÄÃëÊý

sqrt(x) È¡xµÄƽ·½¸ù

×Ö·û´®º¯Êý

index(string1,string2 )

Ëü»áÔÚstring1 ÀïÃ棬ѰÕÒstring2 µÚÒ»´Î³öÏֵĵط½£¬·µ»ØÖµÊÇ×Ö´®string2³öÏÖÔÚ×Ö´®string1 ÀïÃæµÄλÖá£Èç¹ûÕÒ²»µ½£¬·µ»ØֵΪ 0¡£

ÀýÈ磺

print index("peanut","an")

»áÓ¡³ö 3¡£

length(string)

string×Ö·û´®µÄ³¤¶È

ÀýÈ磺

length("abcde")

ÊÇ 5¡£

match(string,regexp)

match º¯Êý»áÔÚ×Ö´® string ÀïÃ棬ѰÕÒ·ûºÏ regexp µÄ×¡¢×î¿¿×ó±ßµÄ×Ó×Ö´®¡£·µ»ØÖµÊÇ regexp ÔÚ string µÄ¿ªÊ¼Î»Ö㬼´ indexÖµ¡£Õâ¸öº¯Êý»áÉ趨ÄÚ²¿±äÁ¿ RSTART µÈì¶ index£¬ÄÚ²¿±äÁ¿RLENGTH µÈ춷ûºÏµÄ×Ó´®¸öÊý¡£Èç¹û²»·ûºÏ£¬Ôò»áÉ趨 RSTART Ϊ0¡¢RLENGTH Ϊ -1¡£

sprintf(format,expression1,...)

¸úCÓïÑÔµÄsprintf²î²»¶à¡£

ÀýÈ磺

sprintf("pi = %.2f (approx.)',22/7)

´«»ØµÄ×Ö´®Îª"pi = 3.14 (approx.)"


sub(regexp, replacement,target)

ÔÚ×Ö´® target ÀïÃ棬ѰÕÒ·ûºÏ regexp µÄ×¡¢×î¿¿×ó±ßµÄµØ·½£¬²¢ÇÒÒÔ×Ö´® replacement ´úÌæ×î×ó±ßµÄ regexp¡£

ÀýÈ磺

str = "water, water, everywhere"

sub(/at/, "ith",str)

½á¹û×Ö´®str»á±ä³É

"wither, water, everywhere"


gsub(regexp, replacement, target)

gsub ÓëÇ°ÃæµÄ sub ÀàËÆ¡£ÔÚ×Ö´® target ÀïÃ棬ѰÕÒ·ûºÏ regexp µÄËùÓеط½£¬ÒÔ×Ö´® replacement ´úÌæËùÓÐµÄ regexp¡£

ÀýÈ磺

str="water, water, everywhere"

gsub(/at/, "ith",str)

½á¹û×Ö´®str»á±ä³É

'wither, wither, everywhere"

substr(string, start, length)

´«»Ø×Ö´® string µÄ×Ó×Ö´®£¬Õâ¸ö×Ó×Ö´®µÄ³¤¶ÈΪ length ¸ö×Ö·û£¬´ÓµÚ start ¸öλÖÿªÊ¼¡£

ÀýÈ磺

substr("washington",5,3)

´«»ØֵΪ"ing"

Èç¹û length ûÓгöÏÖ£¬Ôò´«»ØµÄ×Ó×Ö´®ÊÇ´ÓµÚ start ¸öλÖÿªÊ¼ÖÁ½áÊø¡£

ÀýÈ磺

substr("washington",5)

´«»ØֵΪ"ington"

tolower(string)

½«×Ö´®stringµÄ´óд×Öĸ¸ÄΪСд×Öĸ¡£

ÀýÈ磺

tolower("MiXeD cAsE 123")

´«»ØֵΪ"mixed case 123"


toupper(string)

½«×Ö´®stringµÄСд×Öĸ¸ÄΪ´óд×Öĸ¡£

ÀýÈ磺

toupper("MiXeD cAsE 123")

´«»ØֵΪ"MIXED CASE 123"

ÆäËûº¯Êý

system(command)

´Ëº¯Ê½ÔÊÐíʹÓÃÕßÖ´ÐÐ×÷ҵϵͳµÄÖ¸ÁִÐÐÍê±ÏáὫ»Øµ½ gawk ³Ìʽ¡£

ÀýÈ磺

BEGIN {system("ls")}

¿ØÖÆÁ÷

ÔÚgawkÃüÁî½Å±¾ÖпÉÒÔʹÓÿØÖÆÁ÷£¬Ö÷ÒªÊÇif£¬for£¬whileµÈÓï¾ä£¬Ó÷¨ºÍCÓïÑÔÏ൱ÀàËÆ£º


if (condition) then-body [else else-body]

Èç¹û condition ΪÕæ(true)£¬ÔòÖ´ÐÐ then-body£¬·ñÔòÖ´ÐÐ else-body¡£

¾ÙÒ»¸öÀý×ÓÈçÏ£º

if (x % 2 == 0)

print "x is even"

else

print "x is odd"


while (condition)

body

while Óï¾ä²âÊÔ condition±í´ïʽ¡£¼ÙÈç condition ΪÕæÔòÖ´ÐÐ body µÄÓï¾ä¡£Ò»´ÎÖ´ÐÐÍêáᣬ»áÔÙ²âÊÔ condition£¬¼ÙÈç condition ΪÕ棬Ôò body »áÔٶȱ»Ö´ÐС£Õâ¸ö¹ý³Ì»áÒ»Ö±±»Öظ´Ö±µ½condition ²»ÔÙÊÇÕæ¡£Èç¹û condition µÚÒ»´Î²âÊÔ¾ÍÊÇα(false)£¬Ôòbody ´ÓûÓб»Ö´ÐС£

ÏÂÃæµÄÀý×Ó»áÓ¡³öÿ¸öÊäÈëÐеÄÇ°Èý¸öÀ¸Î»¡£

gawk '{ i=1

while (i <= 3) {

print $i

i++

}

}'

do

body

while (condition)

Õâ¸ö do loop Ö´ÐÐ body Ò»´Î£¬È»ááÖ»Òª condition ÊÇÕæÔò»áÖظ´Ö´ÐÐ body¡£¼´Ê¹¿ªÊ¼Ê± condition ÊÇᣬbody Ò²»á±»Ö´ÐÐÒ»´Î¡£

ÏÂÃæµÄÀý×Ó»áÓ¡³öÿ¸öÊäÈë¼Ç¼ʮ´Î¡£

gawk '{ i= 1

do {

print $0

i++

} while (i <= 10)

}'

for (initialization; condition; increment)

body

´ËÐðÊö¿ªÊ¼Ê±»áÖ´ÐÐinitialization£¬È»ááÖ»Òª conditionÊÇÕ棬Ëü»áÖظ´Ö´ÐÐbodyÓë×öincrement ¡£

ÏÂÃæµÄÀý×Ó»áÓ¡³öÿ¸öÊäÈë¼Ç¼µÄÇ°Èý¸öÀ¸Î»¡£

gawk '{ for (i=1; i<=3; i++)

print $i

}'

break »áÌø³ö°üº¬ËüµÄ for¡¢while¡¢do-while Ñ­»·µÄ×îÄڲ㡣

ÏÂÃæµÄÀý×Ó»áÕÒ³öÈκÎÕûÊýµÄ×îС³ýÊý£¬ËüÒ²»áÅжÏÊÇ·ñΪÖÊÊý¡£

gawk '# find smallest divisor of num

{ num=$1

for (div=2; div*div <=num; div++)

if (num % div == 0)

break

if (num % div == 0)

printf "Smallest divisor of %d is %d\n", num, div

else

printf "%d is prime\n", num }'

continue ʹÓÃÓÚ for¡¢while¡¢do-while Ñ­»·ÄÚ²¿£¬Ëü»áÌø¹ýÑ­»·ÌåµÄÊ£Óಿ·Ö£¬Á¢¿Ì½øÐÐÏÂÒ»´ÎÑ­»·µÄÖ´ÐС£

ÏÂÃæµÄÀý×Ó»áÓ¡³ö 0 ÖÁ 20 µÄÈ«²¿Êý×Ö£¬µ«ÊÇ 5 ²¢²»»á±»Ó¡³ö¡£

gawk 'BEGIN {

for (x=0; x<=20; x++) {

if (x==5)

continue

printf ("%d",x)

}

print ""

}'

next Óï¾äÇ¿ÆÈ gawk Á¢¿ÌÍ£Ö¹´¦ÀíÄ¿Ç°µÄÐжø¼ÌÐøÏÂÒ»¸öÊäÈëÐС£

exit Óï¾ä»áʹµÃ gawk ³ÌʽִֹͣÐжøÌø³ö¡£È»¶ø£¬Èç¹û END ³öÏÖ£¬Ëü»áÈ¥Ö´ÐÐ END µÄ actions¡£


×Ô¶¨Ò庯Êý

Äã¿ÉÒÔ¶¨Òå×Ô¼ºµÄº¯Êý£¬Æä¸ñʽÊÇ

function name (parameter-list) {

body-of-function

}


name ÊÇËù¶¨ÒåµÄº¯ÊýÃû×Ö¡£ parameter-list ÊǺ¯ÊýµÄ±äÁ¿ÁÐ±í¡£±äÁ¿¼äʹÓöººÅ·Ö¿ª¡£

º¯Êý¿ÉÒÔÔÚ³ÌÐòµÄÈκεط½¶¨Ò壬²»¹ýÏ°¹ßÉÏ×ÜÊǶ¨ÒåÔÚ³ÌÐòµÄ¿ªÍ·²¿·Ö¡£

ÏÂÃæÕâ¸öÀý×Ó£¬»á½«Ã¿¸ö¼Ç¼µÄµÚÒ»¸öÀ¸Î»Ö®ÖµµÄƽ·½ÓëµÚ¶þ¸öÀ¸Î»Ö®ÖµµÄƽ·½¼ÓÆðÀ´¡£

{print "sum =",SquareSum($1,$2)}


function SquareSum(x,y) {

sum=x*x+y*y

return sum

}

Èç¹ûÄãÊìϤÈκαà³ÌÓïÑÔ£¬ÄÇôÕÆÎÕawk¶¼ÊǺÜÇáËɵÄÊÂÇ飬Èç¹ûÄ㲻ϲ»¶Ëü£¬ÄÇôÄã¿ÉÒԲο¼ÎÒÃÇÏÂÃæ½éÉܵÄperl¡£

11.3 Perl

Perl ÊÇ´Óawk·¢Õ¹ÆðÀ´µÄ£¬ËüÓÉLarry WallÔÚ1986Äê·¢Ã÷¡£ËüÊÇÒ»ÖÖ¹¦ÄÜÇ¿´óµÄ±à³ÌÓïÑÔ£¬¶øÇÒ¿ÉÒÔÔÚÐí¶àƽ̨ÉÏʹÓá£Êµ¼ÊÉÏ£¬ÄãÍêÈ«¿ÉÒÔ½« Perl×÷ΪһÖÖ±ê×¼±à³ÌÓïÑÔ£¨ ²»Êǽű¾ÓïÑÔ£©À´Ê¹Ó㬱ÊÕ߷dz£Ï²»¶Ëü£¬²¢ÇÒ½¨ÒéËùÓв»ÏëѧϰCÓïÑÔµÄUNIX¹ÜÀíÔ±Ó¦¸ÃÕÆÎÕPerlµÄ»ù±¾±à³Ì¼¼Êõ¡£Ä¿Ç°£¬³£Óõİ汾ÊÇperl 5£¬¼¸ºõËùÓеÄLinux·¢Ðа汾¶¼»á°üº¬Ëü£¬È±Ê¡Ê±£¬linuxµÄperl 5°²×°ÔÚ/usr/binÏ£¬ÃüÁîÊÇ /usr/bin/perl¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ