linuxѧϰ±Ê¼Ç

5. Ò»°ãÀ´Ëµ£¬ÔÚÌõ¼þÅжÏÖУ¬if then¡­¡­[else]¡­¡­if,(else ¿ÉÒÔ²»Òª)£¬ÊdzÉ

¶Ô³öÏֵģ¬Ò²ÊÇ×ñÑ­C ÓïÑÔÖÐif µÄ¾Í½üÔ­Ôò£¬¼´else¡¢fi ¶¼ÊÇÓëÆä×î½üÏàÁÚ

µÄif Åä¶ÔµÄ¡£¶ÔÓÚcase,Ò²ÊÇÀàËÆC ÓïÑÔÖеÄswitch Óï¾ä£¬²ÎÕÕswitch Àí½â£¬

ÿһ¸öcase Ò»¶¨ÓÐÒ»¸öesac ÓëÖ®Åä¶Ô¡£

3.testÃüÁî £¨1£©ÕûÊý²âÊÔÔËËã

test int1 -eq int2: ÅжÏÁ½¸öÊýÊÇ·ñÏàµÈ test int1 -ne int2: ÅжÏÁ½¸öÊýÊÇ·ñ²»ÏàµÈ test int1 -gt int2: ÅжÏÕûÊý1 ÊÇ·ñ´óÓÚÕûÊý2 test int1 -ge int2: ÅжÏÕûÊý1 ÊÇ·ñ´óÓÚµÈÓÚÕûÊý2

test int1 -lt int2: ÅжÏÕûÊý1 ÊÇ·ñСÓÚÕûÊý2 test int1 -le int2: ÅжÏÕûÊý1 ÊÇ·ñСÓÚµÈÓÚÕûÊý2 ÕûÊý²âÊÔÒ²¿ÉÒÔʹÓÃlet ÃüÁî»òË«Ô²À¨ºÅ Ïà¹Ø²Ù×÷Ϊ£º== ¡¢!= ¡¢> ¡¢>= ¡¢< ¡¢<= È磺

x=1; [ $x -eq 1 ]; echo $? x=1; let \x=1; (($x+1>= 2 )); echo $? Á½ÖÖ²âÊÔ·½·¨µÄÇø±ð£º ʹÓõIJÙ×÷·û²»Í¬

let ºÍË«Ô²À¨ºÅÖпÉÒÔʹÓÃËãÊõ±í´ïʽ£¬¶øÖÐÀ¨ºÅ²»ÄÜ let ºÍË«Ô²À¨ºÅÖУ¬²Ù×÷·ûÁ½±ß¿ÉÒÔ²»Áô¿Õ¸ñ

£¨2£©test -z string: ÅжÏ×Ö·û´®³¤¶ÈÊÇ·ñΪ0£¬¼´ÅжÏ×Ö·û´®ÊÇ·ñΪ¿Õ£¬Îª¿Õ·µ »ØÕ棬·Ç¿Õ·µ»Ø¼Ù

test -n string: ÅжÏ×Ö·û´®³¤¶ÈÊÇ·ñ²»Îª0£¬¼´ÅжÏ×Ö·û´®ÊÇ·ñ·Ç¿Õ£¬Îª¿Õ

·µ»Ø¼Ù£¬·Ç¿Õ·µ»ØÕæ

test str1 =str2:ÅжÏÁ½¸ö×Ö·û´®ÊÇ·ñÏàµÈ test str1!=str2:ÅжÏÁ½¸ö×Ö·û´®ÊÇ·ñ²»µÈ È磺

name=Tom; [ -z $name ]; echo $?

name2=Andy; [ $name = $name2 ] ; echo $? £¨3£©Îļþ²âÊÔÔËËã

test -r filename: ÅжÏÓû§¶ÔÎļþfilename ÊÇ·ñÓжÁȨÏÞ test -w filename: ÅжÏÓû§¶ÔÎļþfilename ÊÇ·ñÓÐдȨÏÞ test -x filename: ÅжÏÓû§¶ÔÎļþfilename ÊÇ·ñÓпÉÖ´ÐÐȨÏÞ test -f filename: ÅжÏÎļþfilename ÊÇ·ñΪÆÕͨÎļþ test -d filename: ÅжÏÎļþfilename ÊÇ·ñΪĿ¼ test -c filename: ÅжÏÎļþfilename ÊÇ·ñΪ×Ö·ûÉ豸 test -b filename: ÅжÏÎļþfilename ÊÇ·ñΪ¿éÉ豸 test -s filename: ÅжÏÎļþfilename ÊÇ·ñ´óС²»Îª0

test -t fnum: ÅжÏÓëÎļþÃèÊö·ûfnum£¨Ä¬ÈÏֵΪ1£©Ïà¹ØµÄÉ豸ÊÇ·ñÊÇ Ò»¸öÖÕ¶ËÉ豸 £¨4£©Âß¼­ÔËËã

test ±í´ïʽ1 -a ±í´ïʽ2£ºÓ루and£©Âß¼­Åжϡ£Èç¹ûÁ½¸ö±í´ïʽͬʱΪÕæ

Ôò·µ»ØÕ棬·ñÕß·µ»Ø¼Ù¡£

test ±í´ïʽ1 -o ±í´ïʽ2£º»ò£¨or£© Âß¼­Åжϡ£Ö»ÒªÁ½¸ö±í´ïʽÓÐÒ»¸öΪ

ÕæÔò·µ»ØÕ棬·ñÔò·µ»Ø¼Ù¡£ È磺

x=1; name=Tom;

[ $x -eq 1 ¨Ca ¨Cn $name ]; echo $? ×¢£º²»ÄÜËæ±ãÌí¼ÓÀ¨ºÅ С½á£º

½á¹¹»¯ÃüÁîÔÊÐí¸Ä±äshell½Å±¾ÖеÄÆÕִͨÐÐÁ÷¡£×î»ù±¾µÄ½á¹¹»¯ÃüÁîÊÇif-thenÓï¾ä¡£¸ÃÓï¾äÔÊÐíÆÀ¹ÀÒ»ÌõÃüÁ²¢»ùÓÚÆÀ¹ÀÃüÁîµÄ½á¹ûÖ´ÐÐÆäËûÃüÁî¡£

Èç¹ûÖ¸¶¨µÄÃüÁîҲʧ°ÜÁË£¬¿ÉÒÔÀ©Õ¹if-thenÓï¾äÒÔ°üº¬bash shellÖ´ÐеÄÒ»×éÃüÁî¡£Ö»Óе±±»¹À¼ÆµÄÃüÁî·µ»Ø·ÇÁãÍ˳ö״̬Âëʱ£¬if-then-elseÓï¾ä²ÅÔÊÐíÖ´ÐÐÃüÁî¡£

Ò²¿ÉÒÔʹÓÃelifÓï¾ä½«if-then-elseÓï¾äÁ¬½ÓÆðÀ´¡£ElifÓëʹÓÃelse ifÓï¾äµÈͬ£¬Èç¹û±»¹À¼ÆµÄԭʼÃüÁîʧ°Ü£¬¿ÉÒÔÌṩ¶îÍâµÄ¼ì²é¡£

·½À¨ºÅÊÇÒ»¸öÌØÊâµÄbashÃüÁËüÊÇtestÃüÁîµÄͬÒå´Ê¡£ÔÚif-thenÓï¾äÖеĿÉÒÔʹÓ÷½À¨ºÅ°üΧ²âÊÔÌõ¼þ£¬À´¼ì²éÊý×Ö¡£×Ö·û´®¡£ºÍÎļþÌõ¼þ¡£

µÚʮգº¸ü¶à½á¹¹»¯ÃüÁî ±¾ÕÂÄÚÈÝ£º

1.ʹÓÃforÓï¾äÑ­»· 2.ʹÓÃuntilÓï¾äµü´ú 3.ʹÓÃwhileÓï¾ä 4.½áºÏÑ­»·

ÁªÏµ¿Í·þ£º779662525#qq.com(#Ì滻Ϊ@)