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

UNIXµÄ¿ÚÁîÎļþ

·¢²¼Ê±¼ä:2006-07-30 10:13:08À´Ô´:ºìÁª×÷Õß:bear10214
/etc/passwdÎļþÊÇUNIX°²È«µÄ¹Ø¼üÎļþÖ®Ò».¸ÃÎļþÓÃÓÚÓû§µÇ¼ʱУÑéÓû§µÄ¿ÚÁî,µ±È»Ó¦µ±½ö¶Ôroot¿Éд.ÎļþÖÐÿÐеÄÒ»°ã¸ñʽΪ:
¡¡¡¡LOGNAME:PASSWORD:UID:GID:USERINFO:HOME:SHELL
¡¡¡¡¡¡
¡¡¡¡Ã¿ÐеÄÍ·Á½ÏîÊǵǼÃûºÍ¼ÓÃܺóµÄ¿ÚÁî,ºóÃæµÄÁ½¸öÊýÊÇUIDºÍGID,½Ó×ÅµÄ Ò»ÏîÊÇϵͳ¹ÜÀíÔ±ÏëдÈëµÄÓйظÃÓû§µÄÈκÎÐÅÏ¢,×îºóÁ½ÏîÊÇÁ½¸ö·¾¶Ãû: Ò»¸öÊÇ·ÖÅä¸øÓû§µÄHOMEĿ¼,µÚ¶þ¸öÊÇÓû§µÇ¼ºó½«Ö´ÐеÄshell(ÈôΪ¿Õ¸ñÔò ȱʡΪ/bin/sh).
¡¡¡¡
¡¡¡¡(1)¿ÚÁîʱЧ
¡¡¡¡/etc/passwdÎļþµÄ¸ñʽʹϵͳ¹ÜÀíÔ±ÄÜÒªÇóÓû§¶¨ÆڵظıäËûÃǵĿÚÁî. ÔÚ¿ÚÁîÎļþÖпÉÒÔ¿´µ½,ÓÐЩ¼ÓÃܺóµÄ¿ÚÁîÓжººÅ,¶ººÅºóÓм¸¸ö×Ö·ûºÍÒ»¸öðºÅ.Èç:
¡¡¡¡steve:xyDfccTrt180x,M.y8:0:0:admin:/:/bin/sh
¡¡¡¡restrict:pomJk109Jky41,.1:0:0:admin:/:/bin/sh
¡¡¡¡pat:xmotTVoyumjls:0:0:admin:/:/bin/sh
¡¡¡¡¡¡
¡¡¡¡¿ÉÒÔ¿´µ½,steveµÄ¿ÚÁºÅºóÓÐ4¸ö×Ö·û,restrictÓÐ2¸ö,patûÓжººÅ.
¡¡¡¡¶ººÅºóµÚÒ»¸ö×Ö·ûÊÇ¿ÚÁîÓÐЧÆÚµÄ×î´óÖÜÊý,µÚ¶þ¸ö×Ö·û¾ö¶¨ÁËÓû§ÔÙ´ÎÐ޸ĿÚÁî֮ǰ,Ô­¿ÚÁîӦʹÓõÄ×îСÖÜÊý(Õâ¾Í·ÀÖ¹ÁËÓû§¸ÄÁËпÚÁîºóÁ¢¿Ì ÓָĻسÉÀÏ¿ÚÁî).ÆäÓà×Ö·û±íÃ÷¿ÚÁî×îÐÂÐÞ¸Äʱ¼ä.
¡¡¡¡
¡¡¡¡ÒªÄܶÁ¶®¿ÚÁîÖжººÅºóµÄÐÅÏ¢,±ØÐëÊ×ÏÈÖªµÀÈçºÎÓÃpasswd_esc¼ÆÊý,¼ÆÊýµÄ·½·¨ÊÇ: .=0 /=1 0-9=2-11 A-Z=12-37 a-z=38-63
¡¡¡¡ÏµÍ³¹ÜÀíÔ±±ØÐ뽫ǰÁ½¸ö×Ö·û·Å½ø/etc/passwdÎļþ,ÒÔÒªÇóÓû§¶¨ÆÚµÄÐ޸ĿÚÁî,ÁíÍâÁ½¸ö×Ö·ûµ±Óû§Ð޸ĿÚÁîʱ,ÓÉpasswdÃüÁîÌîÈë.
¡¡¡¡×¢Òâ:ÈôÏëÈÃÓû§Ð޸ĿÚÁî,¿ÉÔÚ×îºóÒ»´Î¿ÚÁî±»ÐÞ¸Äʱ,·ÅÁ½¸ö".",ÔòÏÂÒ»´ÎÓû§µÇ¼ʱ½«±»ÒªÇóÐÞ¸Ä×Ô¼ºµÄ¿ÚÁî.
¡¡¡¡ÓÐÁ½ÖÖÌØÊâÇé¿ö:
¡¡¡¡. ×î´óÖÜÊý(µÚÒ»¸ö×Ö·û)СÓÚ×îСÖÜÊý(µÚ¶þ¸ö×Ö·û),Ôò²»ÔÊÐíÓû§Ð޸ĿÚÁî,½ö³¬¼¶Óû§¿ÉÒÔÐÞ¸ÄÓû§µÄ¿ÚÁî.
¡¡¡¡. µÚÒ»¸ö×Ö·ûºÍµÚ¶þ¸ö×Ö·û¶¼ÊÇ".",ÕâʱÓû§Ï´εǼʱ±»ÒªÇóÐ޸ĿÚÁî,Ð޸ĿÚÁîºó,passwdÃüÁ"."ɾ³ý,´ËºóÔÙ²»»áÒªÇóÓû§Ð޸ĿÚÁî.
¡¡¡¡(2)UIDºÍGID
¡¡¡¡/etc/passwdÖÐUIDÐÅÏ¢ºÜÖØÒª,ϵͳʹÓÃUID¶ø²»ÊǵǼÃûÇø±ðÓû§.Ò»°ãÀ´Ëµ,Óû§µÄUIDÓ¦µ±ÊǶÀÒ»ÎÞ¶þµÄ,ÆäËûÓû§²»Ó¦µ±ÓÐÏàͬµÄUIDÊýÖµ.¸ù¾Ý¹ßÀý,´Ó0µ½99µÄUID±£ÁôÓÃ×÷ϵͳÓû§µÄUID(root,bin,uucpµÈ).
¡¡¡¡Èç¹ûÔÚ/etc/passwdÎļþÖÐÓÐÁ½¸ö²»Í¬µÄÈë¿ÚÏîÓÐÏàͬµÄUID,ÔòÕâÁ½¸öÓû§¶ÔÏ໥µÄÎļþ¾ßÓÐÏàͬµÄ´æȡȨÏÞ.
¡¡¡¡
¡¡¡¡/etc/groupÎļþº¬ÓйØÓÚС×éµÄÐÅÏ¢,/etc/passwdÖеÄÿ¸öGIDÔÚ±¾ÎļþÖÐÓ¦µ±ÓÐÏàÓ¦µÄÈë¿ÚÏî,Èë¿ÚÏîÖÐÁгöÁËС×éÃûºÍС×éÖеÄÓû§.ÕâÑù¿É·½±ãµØÁ˽âÿ¸öС×éµÄÓû§,·ñÔò±ØÐë¸ù¾ÝGIDÔÚ/etc/passwdÎļþÖдÓÍ·ÖÁβµØÑ°ÕÒͬ×éÓû§.
¡¡¡¡
¡¡¡¡/etc/groupÎļþ¶ÔС×éµÄÐí¿ÉȨÏ޵ĿØÖƲ¢²»ÊDZØÒªµÄ,ÒòΪϵͳÓÃUID,GID (È¡×Ô/etc/passwd)¾ö¶¨Îļþ´æȡȨÏÞ,¼´Ê¹/etc/groupÎļþ²»´æÔÚÓÚϵͳÖÐ,¾ßÓÐÏàͬµÄGIDÓû§Ò²¿ÉÒÔС×éµÄ´æÈ¡Ðí¿ÉȨÏÞ¹²ÏíÎļþ.
¡¡¡¡
¡¡¡¡Ð¡×é¾ÍÏñµÇ¼Óû§Ò»Ñù¿ÉÒÔÓпÚÁî.Èç¹û/etc/groupÎļþÈë¿ÚÏîµÄµÚ¶þ¸öÓò Ϊ·Ç¿Õ,Ôò½«±»ÈÏΪÊǼÓÃÜ¿ÚÁî,newgrpÃüÁҪÇóÓû§¸ø³ö¿ÚÁî,È»ºó½«¿ÚÁî¼ÓÃÜ,ÔÙÓë¸ÃÓòµÄ¼ÓÃÜ¿ÚÁî±È½Ï.
¡¡¡¡
¡¡¡¡¸øС×齨Á¢¿ÚÁîÒ»°ã²»ÊǸöºÃ×÷·¨.µÚÒ»,Èç¹ûС×éÄÚ¹²ÏíÎļþ,ÈôÓÐijÈ˲Â×ÅС×é¿ÚÁî,Ôò¸Ã×éµÄËùÓÐÓû§µÄÎļþ¾Í¿ÉÄÜй©;Æä´Î,¹ÜÀíС×é¿ÚÁîºÜ·ÑÊÂ, ÒòΪ¶ÔÓÚС×éûÓÐÀàËƵÄpasswdÃüÁî.¿ÉÓÃ/usr/lib/makekeyÉú³ÉÒ»¸ö¿ÚÁîдÈë /etc/group.
¡¡¡¡
¡¡¡¡ÒÔÏÂÇé¿ö±ØÐ뽨Á¢ÐÂ×é:
¡¡¡¡(1)¿ÉÄÜÒªÔö¼ÓÐÂÓû§,¸ÃÓû§²»ÊôÓÚÈκÎÒ»¸öÏÖÓеÄС×é.
¡¡¡¡(2)ÓеÄÓû§¿ÉÄÜʱ³£ÐèÒª¶À×ÔΪһ¸öС×é.
¡¡¡¡(3)ÓеÄÓû§¿ÉÄÜÓÐÒ»¸öSGID³ÌÐò,ÐèÒª¶À×ÔΪһ¸öС×é.
¡¡¡¡(4)ÓÐʱ¿ÉÄÜÒª°²×°ÔËÐÐSGIDµÄÈí¼þϵͳ,¸ÃÈí¼þϵͳÐèÒª½¨Á¢Ò»¸öÐÂ×é.
¡¡¡¡¡¡¡¡
¡¡¡¡ÒªÔö¼ÓÒ»¸öÐÂ×é,±ØÐë±à¼­¸ÃÎļþ,ΪÐÂ×é¼ÓÒ»¸öÈë¿ÚÏî. ÓÉÓÚÓû§µÇ¼ʱ,ϵͳ´Ó/etc/passwdÎļþÖÐÈ¡GID,¶ø²»ÊÇ´Ó/etc/groupÖÐ È¡GID,ËùÒÔgroupÎļþºÍ¿ÚÁîÎļþÓ¦µ±¾ßÓÐÒ»ÖÂÐÔ.¶ÔÓÚÒ»¸öÓû§µÄС×é,UIDºÍGIDÓ¦µ±ÊÇÏàͬµÄ.¶àÓû§Ð¡×éµÄGIDÓ¦µ±²»Í¬ÓÚÈκÎÓû§µÄUID,Ò»°ãΪ5λÊý,ÕâÑùÔڲ鿴/etc/passwdÎļþʱ,¾Í¿É¸ù¾Ý5λÊý¾ÝµÄGIDʶ±ð¶àÓû§Ð¡×é,Õ⽫¼õÉÙÔö¼ÓÐÂ×é,ÐÂÓû§Ê±¿ÉÄܲúÉúµÄ»ìÏý¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 2 ÌõÆÀÂÛ

  1. qqttss25 ÓÚ 2006-07-30 12:14:41·¢±í:

    Äñ¸ç SHELL ¾­µäÈëÃŲÄÁÏ!




    ѧϰ Shell Scripts
    Èç¹ûÄúÕæµÄºÜÏëÒª×ßÐÅÏ¢ÕâÌõ·£¬²¢ÇÒÏëÒªºÃºÃµÄ¹ÜÀíºÃÊôÓÚÄúµÄÖ÷»ú£¬ÄÇô£¬±ð˵Äñ¸ç²»¸æËßÄú£¬ Shell Scripts ÕæµÄÊDZØÐëҪѧϰµÄÒ»Ïî¿ÎÌâÄØ£¡»ù±¾ÉÏ£¬ shell script ÓеãÏñÊÇÔçÆÚµÄÅú´Îµµ£¬ Ò༴Êǽ«Ò»Ð©Ö¸Áî»ãÕûÆðÀ´Ò»´ÎÖ´ÐУ¬µ«ÊÇ Shell script ÓµÓиüÇ¿´óµÄ¹¦ÄÜ£¬ÄǾÍÊÇ£¬ Ëû¿ÉÒÔ½øÐÐÀàËƳÌÐò (program) µÄ׫д£¬²¢ÇÒ£¬²»ÐèÒª¾­¹ý±àÒë (compiler) ¾ÍÄܹ»Ö´ÐУ¬ ÕæµÄºÜ·½±ã¡£¼ÓÉÏ£¬ÎÒÃÇ¿É͸¹ý shell script À´¼ò»¯ÎÒÃÇÈÕ³£µÄ¹¤×÷¹ÜÀí£¬ ¶øÇÒ£¬Õû¸ö Linux »·¾³ÖУ¬Ò»Ð©·þÎñ (services) µÄÆô¶¯¶¼ÊÇ͸¹ý shell script µÄ£¬ Èç¹ûÄú¶ÔÓÚ script ²»Á˽⣬ºÙºÙ£¡·¢ÉúÎÊÌâʱ£¬¿ÉÕæÊÇ»áÇóÖúÎÞÃÅร¡ ËùÒÔ£¬ºÃºÃµÄѧһѧËû°É£¡
    1. ʲôÊÇ Shell Script
    ¡¡¡¡1.1 ¸ÉÂïѧϰ shell scripts?
    ¡¡¡¡1.2 µÚÒ»Ö§ script µÄ׫дÓëÖ´ÐÐ
    ¡¡¡¡1.3 ׫д shell script µÄÁ¼ºÃÏ°¹ß½¨Á¢
    2. ¼òµ¥µÄ shell script Á·Ï°£º
    3. ÉÆÓÃÅжÏʽ£º
    ¡¡¡¡3.1 ÀûÓà test Ö¸ÁîµÄ²âÊÔ¹¦ÄÜ
    ¡¡¡¡3.2 ÀûÓÃÅжϷûºÅ [ ]
    ¡¡¡¡3.3 Shell script µÄÔ¤Éè±äÊý($0, $1...)
    4. Ìõ¼þÅжÏʽ£º
    ¡¡¡¡4.1 ÀûÓÃ if .... then
    ¡¡¡¡4.2 ÀûÓà case ..... esac ÅжÏ
    ¡¡¡¡4.3 ÀûÓà function ¹¦ÄÜ
    5. Ñ­»· (loop)
    ¡¡¡¡5.1 while....do....done, until....do....done
    ¡¡¡¡5.2 for...do...done
    6. shell script µÄ×·×ÙÓë debug
    7. ±¾ÕÂÏ°ÌâÁ·Ï°
    --------------------------------------------------------------------------------
    ʲôÊÇ Shell scripts £¿
    Õâ¸öÓÐȤµÄÎÊÌâ¸Ï½ôÀ´»Ø´ð¿´¿´£¬Ê²Ã´ÊÇ shell script ÄØ£¿ shell ÎÒÃÇÔÚ ÈÏʶ bash µ±ÖÐÒѾ­Ìá¹ýÁË£¬ÄÇÊÇÒ»¸öÎÄ×Ö½Ó¿Úµ×ÏÂÈÃÎÒÃÇÓëϵͳ¹µÍ¨µÄÒ»¸ö¹¤¾ß½Ó¿Ú£¬ÄÇô script ÊÇɶ£¿ ×ÖÃæÉϵÄÒâÒ壬 script ÊÇ¡º½Å±¾¡¢¾ç±¾¡»µÄÒâ˼¡£Õû¾ä»°ÊÇ˵£¬ shell script ÊÇÕë¶Ô shell ËùдµÄ¡º¾ç±¾£¡¡» ʲô¶«Î÷°¡£¿ºÇºÇ£¡Æäʵ£¬ shell script ÊÇÀûÓà shell µÄ¹¦ÄÜËùдµÄÒ»¸ö¡º³ÌÐò (program)¡»£¬Õâ¸ö³ÌÐòÊÇʹÓô¿ÎÄ×ÖÎļþ£¬½«Ò»Ð© shell µÄÓï·¨ÓëÖ¸ÁîдÔÚÀïÃ棬 ´îÅäÕý¹æ±íʾ·¨¡¢¹ÜÏßÃüÁîÓëÊý¾ÝÁ÷Öص¼ÏòµÈ¹¦ÄÜ£¬ÒÔ´ïµ½ÎÒÃÇËùÏëÒªµÄ´¦ÀíÄ¿µÄ¡£
    ËùÒÔ£¬¼òµ¥µÄ˵£¬ shell script ¾ÍÏñÊÇÔçÆÚ DOS Äê´úµÄÅú´Î? (.bat) £¬×î¼òµ¥µÄ¹¦ÄܾÍÊǽ«Ðí¶àÖ¸Áî»ãÕûдÔÚÒ»Æ𣬠ÈÃʹÓÃÕߺÜÇáÒ׵ľÍÄܹ» one touch (Ö´ÐÐÒ»¸öµµ°¸ "shell script" £¬¾ÍÄܹ»Ò»´ÎÖ´Ðжà¸öÖ¸Áî)£¬ ¶ø£¬ shell script ¸üÌṩÊý×顢ѭ»·¡¢Ìõ¼þÓëÂß¼­ÅжϵÈÖØÒª¹¦ÄÜ£¬ÈÃʹÓÃÕßÒ²¿ÉÒÔÖ±½ÓÒÔ shell À´×«Ð´³ÌÐò£¬¶ø²»±ØʹÓÃÀàËÆ C ³ÌÐòÓïÑԵȴ«Í³³ÌÐò׫дµÄÓï·¨ÄØ£¡
    ÄÇ£¬Õâô˵Äú¿ÉÒÔÁ˽âÁËÂð£¿Êǵģ¡ shell script ¿ÉÒÔ¼òµ¥µÄ±»¿´³ÉÊÇÅú´Îµµ£¬ Ò²¿ÉÒÔ±»Ëµ³ÉÊÇÒ»¸ö³ÌÐòÓïÑÔ£¬ÇÒÕâ¸ö³ÌÐòÓïÑÔÓÉÓÚ¶¼ÊÇÀûÓà shell ÓëÏà¹Ø¹¤¾ßÖ¸Á ËùÒÔ²»ÐèÒª±àÒë¼´¿ÉÖ´ÐУ¬ÇÒÓµÓв»´íµÄ³ý´í (debug) ¹¤¾ß£¬ËùÒÔ£¬Ëû¿ÉÒÔ°ïÖúϵͳ¹ÜÀíÔ±¿ìËٵĹÜÀíºÃÖ÷»ú¡£
    --------------------------------------------------------------------------------
    ¸ÉÂïѧϰ shell scripts?
    ÕâÊǸöºÃÎÊÌ⣬ÎÒÓÖ¸ÉÂïÒ»¶¨ÒªÑ§ shell script £¿ÎÒÓÖ²»ÊÇÐÅÏ¢ÈË£¬Ã»ÓÐд³ÌÐòµÄ¸ÅÄ ÄÇÎÒ¸ÉÂﻹҪѧ shell script ÄØ£¿²»ÒªÑ§¿É²»¿ÉÒÔ°¡£¿ºÇºÇ¡«Èç¹û Linux ¶ÔÄú¶øÑÔ£¬ ÄúÖ»ÊÇÏëÒª¡º»áÓỶøÒÑ£¬ÄÇô£¬²»ÐèҪѧ shell script Ò²»¹ÎÞËùν£¬Õⲿ·ÖÏȸøËûÌø¹ýÈ¥£¬ µÈµ½ÓпյÄʱºò£¬ÔÙÀ´ºÃºÃµÄÇÆÒ»ÇÆ¡£µ«ÊÇ£¬Èç¹ûÄúÊÇÕæµÄÏëÒªÍæÇå³þ Linux µÄÀ´ÁúÈ¥Âö£¬ ÄÇô shell script ¾Í²»¿É²»Öª£¬ÎªÊ²Ã´ÄØ£¿ÒòΪ£º
    ×Ô¶¯»¯¹ÜÀíµÄÖØÒªÒÀ¾Ý£º
    ²»ÓÃÄñ¸ç˵ÄúÒ²ÖªµÀ£¬¹ÜÀíÒ»²¿Ö÷»úÕæ²»ÊǼþ¼òµ¥µÄÊÂÇ飬ÿÌìÒª½øÐеÄÈÎÎñ¾ÍÓУº ²éѯµÇ¼µµ¡¢×·×ÙÁ÷Á¿¡¢¼à¿ØʹÓÃÕßʹÓÃÖ÷»ú״̬¡¢Ö÷»ú¸÷ÏîÓ²É豸״̬¡¢ Ö÷»úÈí¼þ¸üвéѯ¡¢¸ü²»ÒªËµµÃÓ¦¸¶ÆäËüʹÓÃÕßµÄͻȻҪÇóÁË¡£¶øÕâЩ¹¤×÷£¬ ÄúÏëÒª×ÔÐÐÊÖ¶¯´¦Àí£¬»¹ÊÇд¸ö¼òµ¥µÄ³ÌÐòÀ´°ïÄúÿÈÕ×Ô¶¯´¦Àí·ÖÎö£¬ÈôÓÐÎÊÌâ²Å֪ͨÄúÄØ£¿ µ±È»ÊÇÈÃϵͳ×Ô¶¯¹¤×÷±È½ÏºÃ£¬¶Ô°É£¡ºÇºÇ¡«Õâ¾ÍµÃÒªÁ¼ºÃµÄ shell script À´°ïæµÄÀ²£¡
    ×·×ÙÓë¹ÜÀíϵͳµÄÖØÒª¹¤×÷£º
    ËäÈ»ÎÒÃÇ»¹Ã»ÓÐÌáµ½·þÎñÆô¶¯µÄ·½·¨£¬²»¹ý£¬ÕâÀï¿ÉÒÔÏÈÌáһϣ¬ÎÒÃÇ Linux ϵͳµÄ·þÎñ ( services ) Æô¶¯µÄ½Ó¿Ú£¬ÔÚ /etc/init.d/ Õâ¸öĿ¼Ï£¬ËùÓеĵµ°¸¶¼ÊÇ scripts £» ÁíÍ⣬°üÀ¨¿ª»ú (booting) ¹ý³ÌÒ²¶¼ÊÇÀûÓà shell script À´°ïæËÑѰϵͳµÄÏà¹ØÉ趨Êý¾Ý£¬ È»ºóÔÙ´úÈë¸÷¸ö·þÎñµÄÉ趨²ÎÊý°¡£¡¾ÙÀýÀ´Ëµ£¬Èç¹ûÎÒÃÇÏëÒªÖØÐÂÆô¶¯ÏµÍ³µÇ¼Îļþ£¬ ¿ÉÒÔʹÓ㺡º/etc/init.d/syslogd restart¡»£¬ÄǸö syslogd µµ°¸¾ÍÊÇ script À²£¡ ÁíÍ⣬ÎÒÔø¾­ÔÚijһ´úµÄ FC ÉÏÃæ·¢ÏÖ£¬Æô¶¯ MySQL Õâ¸öÊý¾Ý¿â·þÎñʱ£¬È·ÊµÊÇ¿ÉÒÔÆô¶¯µÄ£¬ µ«ÊÇÆÁÄ»ÉÏÈ´ÀÏÊdzöÏÖ¡ºfailure¡»£¬ºóÀ´²Å·¢ÏÖ£¬Ô­À´ÊÇÆô¶¯ MySQL ÄǸö script »áÖ÷¶¯µÄÒÔ¡º¿ÕµÄÃÜÂ롻ȥ³¢ÊÔµÇÈë MySQL £¬µ«ÎÒÐ޸Ĺý MySQL µÄÃÜÂë?¡«µ±È»¾ÍµÇÈëʧ°Ü¡« ºóÀ´¸ÄÁË¸Ä script £¬¾ÍÂÔÈ¥Õâ¸öÎÊÌâÀ²£¡Èç´Ë˵À´£¬ script ȷʵÊÇÐèҪѧϰµÄ°¡£¡
    ¼òµ¥ÈëÇÖÕì²â¹¦ÄÜ£º
    µ±ÎÒÃǵÄϵͳÓÐÒì״ʱ£¬´ó¶à»á½«ÕâЩÒì×´¼Ç¼ÔÚϵͳ¼Ç¼Æ÷£¬Ò²¾ÍÊÇÎÒÃdz£Ìáµ½µÄ¡ºÏµÍ³µÇ¼Îļþ¡»£¬ ÄÇôÎÒÃÇ¿ÉÒÔÔڹ̶¨µÄ¼¸·ÖÖÓÄÚÖ÷¶¯µÄÈ¥·ÖÎöϵͳµÇ¼Îļþ£¬Èô²ì¾õÓÐÎÊÌ⣬¾ÍÁ¢¿Ìͨ±¨¹ÜÀíÔ±£¬ »òÕßÊÇÁ¢¿Ì¼ÓÇ¿·À»ðǽµÄÉ趨¹æÔò£¬Èç´ËÒ»À´£¬ÄúµÄÖ÷»ú¿É¾ÍÄܹ»´ïµ½¡º×ÔÎÒ±£»¤¡»µÄ´ÏÃ÷ѧϰ¹¦ÄÜÀ²¡« ¾ÙÀýÀ´Ëµ£¬ÎÒÃÇ¿ÉÒÔͨ¹ý shell script È¥·ÖÎö¡ºµ±¸Ã·â°ü³¢ÊÔ¼¸´Î»¹ÊÇÁª»úʧ°ÜÖ®ºó£¬¾ÍÓèÒÔµÖµ²×¡¸Ã IP¡»Ö®ÀàµÄ¾Ù¶¯£¬ÀýÈçÄñ¸çд¹ýÒ»¸ö¹ØÓÚµÖµ²¿³Õ¾Èí¼þµÄ shell script £¬ ¾ÍÊÇÓÃÕâ¸öÏ뷨ȥ´ï³ÉµÄÄØ£¡
    Á¬ÐøÖ¸Áîµ¥Ò»»¯£º
    Æäʵ£¬¶ÔÓÚÐÂÊÖ¶øÑÔ£¬ script ×î¼òµ¥µÄ¹¦ÄܾÍÊÇ£º¡º»ãÕûһЩÔÚ command line Ï´ïµÄÁ¬ÐøÖ¸Á½«ËûдÈë scripts µ±ÖУ¬¶øÓÉÖ±½ÓÖ´ÐÐ scripts À´Æô¶¯Ò»Á¬´®µÄ command line Ö¸ÁîÊä³öÈ룡¡»ÀýÈ磺 ·À»ðǽÁ¬Ðø¹æÔò ( iptables )£¬¿ª»ú¼ÓÔسÌÐòµÄÏîÄ¿ ( ¾ÍÊÇÔÚ /etc/rc.d/rc.local ÀïÍ·µÄÊý¾Ý ) £¬µÈµÈ¶¼ÊÇÏàËƵŦÄÜÀ²£¡ Æäʵ£¬Ëµ´©ÁË£¬Èç¹û²»¿¼ÂÇ program µÄ²¿·Ö£¬ÄÇô scripts Ò²¿ÉÒÔÏë³É£¬½öÊÇ°ïÎÒÃÇ°ÑÒ»´ó´®µÄÖ¸Áî»ãÕûÔÚÒ»¸öµµ°¸ÀïÃ棬 ¶øÖ±½ÓÖ´Ðиõµ°¸¾Í¿ÉÒÔÖ´ÐÐÄÇÒ»´®ÓÖ³ôÓÖ³¤µÄÖ¸Áî¶Î£¡¾ÍÊÇÕâô¼òµ¥À²£¡
    ¼òÒ×µÄÊý¾Ý´¦Àí£º
    ÓÉÇ°Ò»Õ Õý¹æ±íʾ·¨ µÄ awk ³ÌÐò˵Ã÷ÖУ¬ Äú¿ÉÒÔ·¢ÏÖ£¬ awk ¿ÉÒÔÓÃÀ´´¦Àí¼òµ¥µÄÊý¾ÝÊý¾ÝÄØ£¡ÀýÈçн×ʵ¥µÄ´¦Àí°¡µÈµÈµÄ¡£ shell script µÄ¹¦ÄܸüÇ¿´ó£¬ÀýÈçÄñ¸çÔø¾­Óà shell script Ö±½Ó´¦ÀíÊý¾ÝÊý¾ÝµÄ±È¶Ô°¡£¬ ÎÄ×ÖÊý¾ÝµÄ´¦Àí°¡µÈµÈµÄ£¬×«Ð´·½±ã£¬ËÙ¶ÈÓÖ¿ì(ÒòΪÔÚ Linux ЧÄܽϼÑ)£¬ ÕæµÄÊǺܲ»´íÓõÄÀ²£¡
    ¿çƽ̨֧³ÖÓëѧϰÀú³Ì½Ï¶Ì£º
    ¼¸ºõËùÓÐµÄ Unix Like ÉÏÃ涼¿ÉÒÔÅÜ shell script £¬Á¬ MS Windows ϵÁÐÒ²ÓÐÏà¹ØµÄ·ÂÕæÆ÷¿ÉÒÔÓ㬠´ËÍ⣬ shell script µÄÓï·¨ÊÇÏ൱Ç׺͵ģ¬¿´¶¼¿´µÄ¶®µÃÎÄ×Ö£¬¶ø²»ÊÇ»úÆ÷Â룬 ºÜÈÝÒ×ѧϰ¡«ÕâЩ¶¼ÊÇÄú¿ÉÒÔ¼ÓÒÔ¿¼ÂǵÄѧϰµã°¡£¡
    ÉÏÃæÕâЩ¶¼ÊÇÄú¿¼ÂÇѧϰ shell script µÄÌص㡫´ËÍ⣬ shell script »¹¿ÉÒÔ¼òµ¥µÄÒÔ vi À´Ö±½Ó±àд£¬ÊµÔÚÊǺܷ½±ãµÄºÃ¶«Î÷£¡ËùÒÔ£¬»¹Êǽ¨ÒéÄúѧϰһÏÂÀ²¡£
    ²»¹ý£¬ËäÈ» shell script ºÅ³ÆÊdzÌÐò (program) £¬µ«Êµ¼ÊÉÏ£¬ shell script ´¦ÀíÊý¾ÝµÄËÙ¶ÈÉÏÊDz»Ì«¹»µÄ¡£ÒòΪ shell script ÓõÄÊÇÍⲿµÄÖ¸ÁîÓë bash shell µÄһЩԤÉ蹤¾ß£¬ËùÒÔ£¬Ëû³£³£»áÈ¥ºô½ÐÍⲿµÄº¯Ê½¿â£¬Òò´Ë£¬ÔËËãËÙ¶ÈÉÏÃ浱Ȼ±È²»ÉÏ´«Í³µÄ³ÌÐòÓïÑÔ¡£ ËùÒÔ?£¬ shell script ÓÃÔÚϵͳ¹ÜÀíÉÏÃæÊǺܺõÄÒ»Ï¾ß£¬µ«ÊÇÓÃÔÚ´¦Àí´óÁ¿ÊýÖµÔËËãÉÏ£¬ ¾Í²»¹»ºÃÁË¡«¶øÇÒ»¹ºÜÂé·³£¬ÒòΪ£ºShell scripts µÄËٶȽÏÂý£¬ ÇÒʹÓÃµÄ CPU ×ÊÔ´½Ï¶à£¬Ôì³ÉÖ÷»ú×ÊÔ´µÄ·ÖÅä²»Á¼¡£»¹ºÃ£¬ ÎÒÃÇȷʵºÜÉÙ¿´µ½ÀûÓà shell script ÔÚ½øÐдóÁ¿Êý¾ÝÔËËãµÄ£¬ËùÒÔ£¬²»±Øµ£ÐĵÄÀ²£¡
    --------------------------------------------------------------------------------
    µÚÒ»Ö§ script µÄ׫дÓëÖ´ÐÐ
    ÈçͬǰÃæ½²µ½µÄ£¬ shell script Æäʵ¾ÍÊÇ´¿ÎÄ×ÖÎļþ (ASCII) £¬ÎÒÃÇ¿ÉÒԱ༭Õâ¸öµµ°¸£¬ È»ºóÈÃÕâ¸öµµ°¸À´°ïÎÒÃÇÒ»´ÎÖ´Ðжà¸öÖ¸Á»òÕßÊÇÀûÓÃһЩÔËËãÓëÂß¼­ÅжÏÀ´°ïÎÒÃÇ´ï³ÉijЩ¹¦ÄÜ¡£ ËùÒÔÀ²£¬Òª±à¼­Õâ¸öµµ°¸µÄÄÚÈÝʱ£¬µ±È»¾ÍÐèÒª¾ß±¸ÓÐ bash shell Ö¸ÁîÏ´ïµÄÏà¹ØÈÏʶ¡£ ÎÒÃÇ˵¹ý£¬ÒªÏ´ïÖ¸ÁîÐèҪעÒâµÄÊÂÏîÔÚ bash Õ½ÚÄÚÒѾ­Ìá¹ý£¬ ÔÚ shell script µÄ׫дͬÑùÐèÒªÓõ½ÕâЩעÒâÊÂÏîµÄ£º
    ÈçͬǰÃæ bash command Ìáµ½µÄ£¬Ö¸ÁîÓë²ÎÊý¼äµÄ¶à¸ö¿Õ°×»á±»ºöÂÔµô£»
    ¶ø¿Õ°×ÐÐÒ²½«±»ºöÂÔµô£¡£¬²¢ÇÒ [tab] Ò²ÊDz»»á±»Àí»áµÄ£¡
    Èç¹û¶ÁÈ¡µ½Ò»¸ö Enter ·ûºÅ ( CR )£©£¬¾Í³¢ÊÔ¿ªÊ¼Ö´ÐиÃÐÐÃüÁ
    ÖÁÓÚÈç¹ûÒ»ÐеÄÄÚÈÝÌ«¶à£¬Ôò¿ÉÒÔʹÓà [Enter] À´ÑÓÉìÖÁÏÂÒ»ÐУ»
    ´ËÍ⣬ʹÓÃ×î¶àµÄ # ¿É×öΪÅú×¢£¡ÈκμÓÔÚ # ºóÃæµÄ×Ö£¬½«È«²¿±»ÊÓΪÅú×¢ÎÄ×Ö¶ø±»ºöÂÔ£¡
    Èç´ËÒ»À´£¬ÎÒÃÇÔÚ script ÄÚËù׫дµÄ³ÌÐò£¬¾Í»á±»Ò»ÐÐÒ»ÐеÄÖ´ÐС£ºÃÁË£¬ÄÇôÕâ¸ö³ÌÐò¼ÙÉèÎļþÃûÊÇ shell.sh ºÃÁË£¬ÈçºÎÖ´ÐÐÕâ¸öµµ°¸£¿ºÜ¼òµ¥£¬¿ÉÒÔÓе×ϼ¸¸ö·½·¨£º
    ½« shell.sh ¼ÓÉϿɶÁÓëÖ´ÐÐ (rx) µÄȨÏÞ£¬È»ºó¾ÍÄܹ»ÒÔ ./shell.sh À´Ö´ÐÐÁË£»
    Ö±½ÓÒÔ sh shell.sh µÄ·½Ê½À´Ö±½ÓÖ´Ðм´¿É¡£
    ·´ÕýÖصã¾ÍÊÇÒªÈÃÄǸö shell.sh ÄÚµÄÖ¸Áî¿ÉÒÔ±»Ö´ÐеÄÒâ˼À²£¡ß×£¡ÄÇÎÒΪºÎÐèҪʹÓà ./shell.sh À´Ï´ïÖ¸Á »¹¼ÇµÃÎÒÃÇÔÚ bash ÀïÃæһֱǿµ÷µÄ£¬Ö¸ÁîÊÇ·ñÄܹ»±»Ö´ÐÐÓë PATH Õâ¸ö»·¾³±äÁ¿Óйأ¬ ËùÒÔ£¬ÒªÖ´ÐСºÄ¿Ç°Õâ¸öĿ¼ÏµÄij¸öµµ°¸¡»¾ÍÐèÒª¼ÓÉÏ ./ Õâ¸öĿ¼À²£¡ÁíÍ⣬ÆäʵÄúÒ²¿ÉÒÔ½« shell.sh ·ÅÔÚÄú¼ÒĿ¼Ï嵀 ~/bin Õâ¸öĿ¼ÖУ¬È»ºóÀûÓà PATH="$PATH":~/bin µÄÉ趨£¬ ºÙºÙ£¬¾ÍÄܹ»Ö±½ÓÖ´ÐÐÄúµÄ script ?¡« ^_^
    ÄÇ£¬ÎªºÎ sh shell.sh Ò²¿ÉÒÔÖ´ÐÐÄØ£¿ÕâÊÇÒòΪ /bin/sh Æäʵ¾ÍÊÇ /bin/bash £¬ ʹÓà sh shell.sh Ò༴¸æËßϵͳ£¬ÎÒÏëÒªÖ±½ÓÒÔ bash µÄ¹¦ÄÜÀ´Ö´ÐÐ shell.sh Õâ¸öµµ°¸ÄÚµÄÏà¹ØÖ¸ÁîµÄÒâ˼¡£ ¶øÎÒÃÇÒ²¿ÉÒÔÀûÓà sh µÄ²ÎÊý£¬Èç -n ¼° -x À´¼ì²éÓë×·×Ù shell.sh µÄÓï·¨ÊÇ·ñÕýÈ·ÄØ£¡ ^_^
    --------------------------------------------------------------------------------
    ׫дµÚÒ»Ö§ script
    ²»ÂÛÊÇÄǸöÃÅÅÉ£¬ÒªÑ§Î书Ҫ´ÓɨµØ×öÆð£¬ÄÇôҪѧ³ÌÐòÄØ£¿ºÇºÇ£¬¿Ï¶¨ÊÇÓÉ¡ºÐã³ö Hello World£¡¡» Õâ¸ö×ÖÑÛ¿ªÊ¼µÄ£¡OK£¡ÄÇôÄñ¸ç¾ÍÏÈдһ֧ script ¸ø´ó¼ÒÇÆÒ»ÇÆ£º
    [root@linux ~]# mkdir scripts; cd scripts
    [root@linux scripts]# vi sh01.sh
    #!/bin/bash
    # Program:
    # This program is used to show "Hello World !" in screen.
    # History:
    # 2005/08/23 VBird First release
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    echo -e "Hello World ! a "
    exit 0
    ÔÚÎÒÃÇÕâ¸öÕ½ڵ±ÖУ¬Ç뽫ËùÓеÄ׫дµÄ script ·ÅÖõ½Äú¼ÒĿ¼µÄ ~/scripts Õâ¸öĿ¼ÄÚ£¬ ±È½ÏºÃ¹ÜÀíÀ²£¡ÉÏÃæµÄд·¨µ±ÖУ¬ÎÒÖ÷Òª½«Õû¸ö³ÌÐòµÄ׫д·Ö³ÉÊý¶Î£¬´óÖÂÊÇÕâÑù£º
    µÚÒ»ÐÐ #!/bin/bash ÔÚÐû¸æÕâ¸ö script ʹÓÃµÄ shell Ãû³Æ£º
    ÒòΪÎÒÃÇʹÓõÄÊÇ bash £¬ËùÒÔ£¬±ØÐëÒªÒÔ¡º #!/bin/bash ¡»À´Ðû¸æÕâ¸öµµ°¸ÄÚµÄÓ﷨ʹÓà bash µÄÓï·¨£¡ÄÇôµ±Õâ¸ö³ÌÐò±»Ö´ÐÐʱ£¬Ëû¾ÍÄܹ»¼ÓÔØ bash µÄÏà¹Ø»·¾³É趨µµ£¬ ²¢ÇÒÖ´ÐÐ bash À´Ê¹ÎÒÃǵ×ϵÄÖ¸ÁîÄܹ»Ö´ÐУ¡ÕâºÜÖØÒªµÄ£¡(Ôںܶà×´¿öÖУ¬Èç¹ûûÓÐÉ趨ºÃÕâÒ»ÐУ¬ ÄÇô¸Ã³ÌÐòºÜ¿ÉÄÜ»áÎÞ·¨Ö´ÐУ¬ÒòΪϵͳ¿ÉÄÜÎÞ·¨ÅжϸóÌÐòÐèҪʹÓÃʲô shell À´Ö´Ðа¡£¡)
    ³ÌÐòÄÚÈݵÄÐû¸æ£º
    Õû¸ö script µ±ÖУ¬³ýÁ˵ÚÒ»ÐÐµÄ #! ÊÇÓÃÀ´Ðû¸æ shell µÄÖ®Í⣬ÆäËüµÄ # ¶¼ÊÇ¡ºÅú×¢¡»ÓÃ;£¡ ËùÒÔÉÏÃæµÄ³ÌÐòµ±ÖУ¬µÚ¶þÐÐÒÔϾÍÊÇÓÃÀ´ËµÃ÷Õû¸ö³ÌÐòµÄ״̬¡£Ò»°ãÀ´Ëµ£¬ ½¨ÒéÄúÒ»¶¨ÒªÑø³É˵Ã÷¸Ã script µÄ£º1. ÄÚÈÝÓ빦ÄÜ£» 2. °æ±¾ÐÅÏ¢£» 3. ×÷ÕßÓëÁªÂ緽ʽ£» 4. ½¨?ÈÕÆÚ£»5. ÀúÊ·¼Í¼ µÈµÈ¡£Õ⽫ÓÐÖúÓÚδÀ´³ÌÐòµÄ¸ÄдÓë debug ÄØ£¡
    Ö÷Òª»·¾³±äÁ¿µÄÐû¸æ£º
    ½¨ÒéÎñ±ØÒª½«Ò»Ð©ÖØÒªµÄ»·¾³±äÁ¿É趨ºÃ£¬Äñ¸ç¸öÈËÈÏΪ£¬ PATH Êǵ±ÖÐ×îÖØÒªµÄ£¡ Èç´ËÒ»À´£¬Ôò¿ÉÈÃÎÒÃÇÕâÖ§³ÌÐòÔÚ½øÐÐʱ£¬¿ÉÒÔÖ±½ÓÏ´ïÖ¸Á ¶ø²»±Øд¾ø¶Ô·¾¶ÄØ£¡±È½ÏºÃÀ²£¡
    Ö÷Òª³ÌÐò²¿·Ö
    ¾Í½«Ö÷ÒªµÄ³ÌÐòдºÃ¼´¿É£¡ÔÚÕâ¸öÀý×Óµ±ÖУ¬¾ÍÊÇ echo ÄÇÒ»ÐÐÀ²£¡
    Ö´Ðгɹû¸æÖª
    ÊÇ·ñ¼ÇµÃÎÒÃÇÔÚ bash ÀïÃæÒªÌÖÂÛÒ»¸öÖ¸ÁîµÄÖ´Ðгɹ¦Óë·ñ£¬¿ÉÒÔʹÓà $? Õâ¸ö±äÁ¿À´¹Û²ì¡« ÄÇôÎÒÃÇÒ²¿ÉÒÔÀûÓà exit Õâ¸öÖ¸ÁîÀ´ÈóÌÐòÖжϣ¬²¢Çһش«Ò»¸öÊýÖµ¸øϵͳ¡£ ÔÚÎÒÃÇÕâ¸öÀý×Óµ±ÖУ¬ÎÒʹÓà exit 0 £¬Õâ´ú±íÀ뿪 script £¬²¢Çһش«Ò»¸ö 0 ¸øϵͳ£¬ ËùÒÔÎÒÖ´ÐÐÍêÕâ¸ö script ºó£¬Èô½Ó×ÅÏ´ï echo $? Ôò¿ÉµÃµ½ 0 µÄֵร¡ ¸ü´ÏÃ÷µÄ¶ÁÕßÓ¦¸ÃÒ²ÖªµÀÁË£¬ºÇºÇ£¡ÀûÓÃÕâ¸ö exit n µÄ¹¦ÄÜ£¬ÎÒÃÇ»¹¿ÉÒÔ×Ô¶©´íÎóѶϢ£¬ ÈÃÕâÖ§³ÌÐò±äµÃ¸ü¼ÓµÄ smart ÄØ£¡
    ½ÓÏÂÀ´Ö´Ðп´¿´½á¹ûÊÇÔõÑù°É£¿
    [root@linux scripts]# sh sh01.sh
    Hello World !
    Äú»á¿´µ½ÆÁÄ»ÊÇÕâÑù£¬¶øÇÒÓ¦¸Ã»¹»áÌýµ½¡ºßË¡»µÄÒ»Éù£¬ÎªÊ²Ã´ÄØ£¿»¹¼ÇµÃÇ°Ò»ÕÂÌáµ½µÄ printf °É£¿Óà echo ½Ó×ÅÄÇЩÌØÊâµÄ°´¼üÒ²¿ÉÒÔ·¢ÉúͬÑùµÄÊÂÇé¡« ²»¹ý£¬ echo ±ØÐëÒª¼ÓÉÏ -e µÄ²ÎÊý²ÅÐУ¡ ºÇºÇ£¡ÔÚÄúдÍêÕâ¸öС script Ö®ºó£¬Äú¾Í¿ÉÒÔ´óÉùµÄ˵£º¡ºÎÒÒ²»áд³ÌÐòÁË¡»£¡¹þ¹þ£¡ ºÜ¼òµ¥ÓÐȤ°É¡« ^_^
    ÁíÍ⣬ÄãÒ²¿ÉÒÔÀûÓ㺡ºchmod a+x sh01.sh; ./sh01.sh¡»À´Ö´ÐÐÕâ¸ö script µÄÄØ£¡
    --------------------------------------------------------------------------------
    ׫д shell script µÄÁ¼ºÃÏ°¹ß½¨Á¢
    Ò»¸öÁ¼ºÃÏ°¹ßµÄÑø³ÉÊǺÜÖØÒªµÄ¡«´ó¼ÒÔÚ¸Õ¿ªÊ¼×«Ð´³ÌÐòµÄʱºò£¬×îÈÝÒ׺öÂÔÕⲿ·Ö£¬ ÈÏΪ³ÌÐòд³öÀ´¾ÍºÃÁË£¬ÆäËüµÄ²»ÖØÒª¡£Æäʵ£¬Èç¹û³ÌÐòµÄ˵Ã÷Äܹ»¸üÇå³þ£¬ ÄÇô¶ÔÄú×Ô¼ºÊÇÓкܴóµÄ°ïÖúµÄ¡£
    ¾ÙÀýÀ´Ëµ£¬Äñ¸ç×Ô¼ºÎªÁË×Ô¼ºµÄÐèÇó£¬Ôø¾­×«Ð´Á˲»ÉÙµÄ script À´°ïÎÒ½øÐÐÖ÷»ú IP µÄÕì²â°¡¡¢ µÇ¼µµ·ÖÎöÓë¹ÜÀí°¡¡¢×Ô¶¯ÉÏ´«ÏÂÔØÖØÒªÉ趨µµ°¡µÈµÈµÄ£¬²»¹ý£¬ÔçÆÚ¾ÍÊÇÒòΪ̫ÀÁÁË£¬ ¹ÜÀíµÄÖ÷»úÓÖÌ«¶àÁË£¬³£³£Í¬Ò»¸ö³ÌÐòÔÚ²»Í¬µÄÖ÷»úÉÏÃæ½øÐиü¸Ä£¬µ½×îºó£¬µ½µ×ÄÄÒ»Ö§²ÅÊÇ×îеĶ¼¼Ç²»ÆðÀ´£¬ ¶øÇÒ£¬ÖصãÊÇ£¬ÎÒµ½µ×ÊǸÄÁËÄÄÀ£¿ÎªÊ²Ã´×öÄÇÑùµÄÐ޸ģ¿¶¼ÍüµÄһǬ¶þ¾»¡«ÕæÒªÃü¡«
    ËùÒÔ£¬ºóÀ´Äñ¸çÔÚд³ÌÐòµÄʱºò£¬Í¨³£»á±È½Ï×ÐϸµÄ½«³ÌÐòµÄÉè¼Æ¹ý³Ì¸øËû¼Ç¼ÏÂÀ´£¬ ¶øÇÒ»¹»á¼Ç¼һЩÀúÊ·¼Í¼£¬Èç´ËÒ»À´£¬ºÃ¶àÁË¡« ÖÁÉÙºÜÈÝÒ×ÖªµÀÎÒÐÞ¸ÄÁËÄÄЩÊý¾Ý£¬ÒÔ¼°³ÌÐòÐ޸ĵÄÀíÄîÓëÂß¼­¸ÅÄîµÈµÈ£¬ ÔÚά»¤ÉÏÃæÊÇÇáËɺܶàºÜ¶àµÄร¡
    ÁíÍ⣬ÔÚһЩ»·¾³µÄÉ趨ÉÏÃ棬±Ï¾¹Ã¿¸öÈ˵Ļ·¾³¶¼²»Ïàͬ£¬ÎªÁËÈ¡µÃ½Ï¼ÑµÄÖ´Ðл·¾³£¬ ÎÒ¶¼»á×ÔÐÐÏȶ¨ÒåºÃһЩһ¶¨»á±»Óõ½µÄ»·¾³±äÁ¿£¬ÀýÈç PATH Õâ¸öÍæÒâ¶ù£¡ ÕâÑù±È½ÏºÃÀ²¡«ËùÒÔ˵£¬½¨ÒéÄúÒ»¶¨ÒªÑø³ÉÁ¼ºÃµÄ script ׫дϰ¹ß£¬ ÔÚÿ¸ö script µÄÎļþÍ·´¦¼Ç¼ºÃ£º
    script µÄ¹¦ÄÜ£»
    script µÄ°æ±¾ÐÅÏ¢£»
    script µÄ×÷ÕßÓëÁªÂ緽ʽ£»
    script µÄ°æȨÐû¸æ·½Ê½£»
    script µÄ History (ÀúÊ·¼Í¼)£»
    script ÄÚ½ÏÌØÊâµÄÖ¸ÁʹÓþø¶Ô·¾¶µÄ·½Ê½À´Ï´
    script ÔË×÷ʱÐèÒªµÄ»·¾³±äÁ¿Ô¤ÏÈÐû¸æÓëÉ趨¡£
    --------------------------------------------------------------------------------
    ¼òµ¥µÄ shell script Á·Ï°
    ÔÚµÚÒ»Ö§ shell script ׫дÍê±ÏÖ®ºó£¬ÏàÐÅÄúÓ¦¸Ã¾ßÓлù±¾µÄ׫д¹¦Á¦ÁË¡£ ½ÓÏÂÀ´£¬ÔÚ¿ªÊ¼¸üÉîÈëµÄ³ÌÐò¸ÅÄî֮ǰ£¬ÎÒÃÇÏÈÀ´ÍæһЩ±È½ÏÓÐȤµÄ¼òµ¥µÄС·¶ÀýºÃÁË¡£ µ×ϵķ¶ÀýÖУ¬´ï³É½á¹ûµÄ·½Ê½Ï൱µÄ¶à£¬½¨ÒéÄúÏÈ×ÔÐÐ׫д¿´¿´£¬Ð´ÍêÖ®ºóÔÙÓëÄñ¸çдµÄÄÚÈݱȶԣ¬ ÕâÑù²ÅÄܸü¼ÓÉî¸ÅÄîร¡ºÃ£¡²»?Ëô£¬ÎÒÃǾÍÒ»¸öÒ»¸öÀ´Íæ°É£¡
    --------------------------------------------------------------------------------
    ±äÁ¿ÄÚÈÝÓÉʹÓÃÕß¾ö¶¨
    ºÜ¶àʱºòÎÒÃÇÐèҪʹÓÃÕßÊäÈëһЩÄÚÈÝ£¬ºÃÈóÌÐò¿ÉÒÔ˳ÀûÔË×÷¡£ ¼òµ¥µÄÀ´Ëµ£¬´ó¼ÒÓ¦¸Ã¶¼Óа²×°¹ýÈí¼þµÄ¾­Ñ飬°²×°µÄʱºò£¬Ëû²»ÊÇ»áÎÊÄú¡ºÒª°²×°µ½ÄǸöĿ¼ȥ£¿¡»Â𣿠ÄǸöÈÃʹÓÃÕßÊäÈëµÄÊý¾ÝµÄ¶¯×÷£¬¾ÍÊÇÈÃʹÓÃÕßÊäÈë±äÁ¿ÄÚÈÝÀ²¡£
    ÄãÓ¦¸Ã»¹¼ÇµÃÔÚ bash µÄʱºò£¬ÎÒÃÇÓÐѧµ½Ò»¸ö read Ö¸Áî°É£¿Íü¼ÇµÄ»°£¬Çë×ÔÐлØÍ·È¥ÔĶÁÒ»·¬¡£ ÏÖÔÚ£¬ÇëÄãÒÔ read Ö¸ÁîµÄÓÃ;£¬×«Ð´Ò»¸ö script £¬Ëû¿ÉÒÔÈÃʹÓÃÕßÊäÈ룺1 first name Óë 2. last name£¬ ×îºó²¢ÇÒÔÚÆÁÄ»ÉÏÏÔʾ£º¡ºYour full name is: ¡»µÄÄÚÈÝ£º
    [root@linux scripts]# vi sh02.sh
    #!/bin/bash
    # Program:
    # Let user keyin their first and last name, and show their full name.
    # History:
    # 2005/08/23 VBird First release
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    read -p "Please input your first name: " firstname
    read -p "Please input your last name: " lastname
    echo -e " Your full name is: $firstname $lastname"
    ½«ÉÏÃæÕâ¸ö sh02.sh Ö´ÐÐһϣ¬Äã¾ÍÄܹ»·¢ÏÖʹÓÃÕß×Ô¼ºÊäÈëµÄ±äÁ¿¿ÉÒÔ±»È¡ÓõÄÁ¨£¡ ºÜ²»´í°É£¡¼ÓÓÍ£¡
    --------------------------------------------------------------------------------
    ÀûÓà date ½øÐеµ°¸µÄ½¨Á¢
    ÏëÏóÒ»¸ö×´¿ö£¬Èç¹ûÎÒÿÌìÒª½øÐб¸·Ý£¬¶ø±¸·ÝµÄÊý¾ÝÓÖ²»Ïë±»¸²¸Çµô£¬Ò²¾ÍÊÇ˵£¬ ÎÒÏëÒª½«Ã¿Ì챸·ÝµÄÊý¾Ý·ÅÔÚ²»Í¬µÄµµ°¸ÖС£ÍÛ£¡ÕâÕæÀ§ÈÅ°¡£¿ÄѵÀÒªÎÒÿÌìÈ¥ÐÞ¸Ä script £¿ ²»ÐèÒª°¡£¡ÒòΪÿÌìµÄ¡ºÈÕÆÚ¡»²¢²»Ïàͬ£¬ËùÒÔÎÒ¿ÉÒÔ½«µµÃûÈ¡³ÉÀàËÆ£º backup.20050802 £¬ ²»¾Í¿ÉÒÔÿÌìÒ»¸ö²»Í¬µµÃûÁËÂ𣿺Ǻǣ¡È·ÊµÈç´Ë¡£ºÃÁË£¬½ÓÏÂÀ´³ö¸öÀý×Ó£º ÎÒÏëÒª½¨Á¢Èý¸ö¿ÕµÄµµ°¸£¬µµÃû×ͷÓÉʹÓÃÕßÊäÈë¾ö¶¨£¬¼ÙÉèʹÓÃÕßÊäÈë filename ºÃÁË£¬ ÄǽñÌìµÄÈÕÆÚÊÇ 2005/08/23 £¬ÎÒÏëÒªÒÔÇ°Ìì¡¢×òÌì¡¢½ñÌìµÄÈÕÆÚÀ´½¨Á¢Õâ¸öµµ°¸£¬Ò༴ filename_20050821, filename_20050822, filename_20050823 £¬¸ÃÈçºÎÊǺã¿
    [root@linux scripts]# vi sh03.sh
    #!/bin/bash
    # Program:
    # User can keyin filename to touch 3 new files.
    # History:
    # 2005/08/23 VBird First release
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    # 1. ÈÃʹÓÃÕßÊäÈëÎļþÃû³Æ£¬²¢È¡µÃ fileuser Õâ¸ö±äÁ¿£»
    echo -e "I will use 'touch' command to create 3 files."
    read -p "Please input the filename what you want: " fileuser
    # 2. ΪÁ˱ÜÃâʹÓÃÕßËæÒâ°´ Enter £¬ÀûÓñäÁ¿¹¦ÄÜ·ÖÎöÎļþÃûÊÇ·ñÓÐÉ趨£¿
    filename=${fileuser:-"filename"}
    # 3. ¿ªÊ¼ÀûÓà date Ö¸ÁîÀ´È¡µÃËùÐèÒªµÄµµÃûÁË£»
    date1=`date --date='2 days ago' +%Y%m%d`
    date2=`date --date='1 days ago' +%Y%m%d`
    date3=`date +%Y%m%d`
    file1="$filename""$date1"
    file2="$filename""$date2"
    file3="$filename""$date3"
    # 4. ½«µµÃû½¨Á¢°É£¡
    touch $file1
    touch $file2
    touch $file3
    ÎÒ͸¹ýһЩ¼òµ¥µÄ¶¯×÷£¬ÕâЩ¶¯×÷¶¼¿ÉÒÔÔÚ bash ÄÇÒ»ÕÂÀïÃæÕÒµ½£¬ °üÀ¨Ð¡Ö¸Áî (`) µÄÈ¡µÃѶϢ¡¢±äÁ¿µÄÉ趨¹¦ÄÜ¡¢±äÁ¿µÄÀÛ¼ÓÒÔ¼°ÀûÓà touch Ö¸ÁÖú£¡ Èç¹ûÄú¿ªÊ¼Ö´ÐÐÕâ¸ö sh03.sh Ö®ºó£¬Äã¿ÉÒÔ½øÐÐÁ½´ÎÊäÈ룬һ´ÎÖ±½Ó°´ [Enter] À´²éÔĵµÃûÊÇɶ£¿ Ò»´Î¿ÉÒÔÊäÈëһЩ×Ö·û£¬ÕâÑùÀ´ÅжÏÄãµÄµµ°¸à¸£¡¹ØÓÚ date µÄÖ¸ÁîÓ¦Óã¬Çë man date °É£¡ ^_^
    --------------------------------------------------------------------------------
    ÊýÖµÔËËãµÄ·½·¨
    ¸÷λ¿´¹ÙÓ¦¸Ã»¹¼ÇµÃ£¬ÎÒÃÇ¿ÉÒÔʹÓà declare À´¶¨Òå±äÁ¿µÄÀàÐÍ°É£¿£¡ ÕâÑù²ÅÄܹ»½øÐмӼõÔËËã°¡£¡¿ÉϧµÄÊÇ£¬ bash shell ÀïÍ·Ô¤Éè½öÖ§³Öµ½ÕûÊýµÄÊý¾Ý¡£ OK£¡ÄÇÎÒÃÇÀ´ÍæÍæ¿´£¬Èç¹ûÎÒÃÇҪʹÓÃÕßÊäÈëÁ½¸ö±äÁ¿£¬È»ºó½«Á½¸ö±äÁ¿µÄÄÚÈÝÏà³Ë£¬ ×îºóÊä³öÏà³ËµÄ½á¹û£¬ÄÇ¿ÉÒÔÔõô×ö£¿
    [root@linux scripts]# vi sh04.sh
    #!/bin/bash
    # Program:
    # User can input 2 integer to cross by!
    # History:
    # 2005/08/23 VBird First release
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    echo -e "You SHOULD input 2 number, I will cross they! "
    read -p "first number: " firstnu
    read -p "second number: " secnu
    total=$(($firstnu*$secnu))
    echo -e " The number $firstnu x $secnu is ==>; $total"
    ÔÚÊý×ÖµÄÔËËãÉÏ£¬ÎÒÃÇ¿ÉÒÔʹÓẠdeclare -i total=$firstnu*$secnu ¡» Ò²¿ÉÒÔʹÓÃÉÏÃæµÄ·½Ê½À´½øÐУ¡»ù±¾ÉÏ£¬Äñ¸ç±È½Ï½¨ÒéʹÓÃÕâÑùµÄ·½Ê½À´½øÐÐÔËË㣺
    var=$((ÔËËãÄÚÈÝ))
    ²»µ«ÈÝÒ×¼ÇÒ䣬¶øÇÒÒ²±È½Ï·½±ãµÄ¶à¡«Î´À´Äú¿ÉÒÔʹÓÃÕâÖÖ·½Ê½À´¼ÆËãµÄѽ£¡ÖÁÓÚÊýÖµÔËËãÉϵĴ¦Àí£¬ ÔòÓУº+, -, *, /, %µÈµÈ¡£ ÄǸö % ÊÇÈ¡ÓàÊýÀ²¡«¾ÙÀýÀ´Ëµ£¬ 13 ¶Ô 3 È¡ÓàÊý£¬½á¹ûÊÇ 13=4*3+1£¬ËùÒÔÓàÊýÊÇ 1 °¡£¡¾ÍÊÇ£º
    [root@linux scripts]# nu=$((13%3)); echo $nu
    1
    ÕâÑùÁ˽âÁË°É£¿£¡¶à¶àѧϰÓëÓ¦ÓÃร¡ ^_^
    --------------------------------------------------------------------------------
    ÉÆÓÃÅжÏʽ
    ÔÚ bash Õ½ÚÖУ¬ÎÒÃÇÌáµ½¹ý $? Õâ¸ö±äÁ¿Ëù´ú±íµÄÒâÒ壬 ´ËÍ⣬Ҳ͸¹ý && ¼° || À´×÷Ϊǰһ¸öÖ¸ÁîÊÇ·ñÄܹ»³É¹¦½øÐеÄÒ»¸ö²Î¿¼¡£ ÄÇô£¬Èç¹ûÎÒÏëÒªÖªµÀ /dmtsai Õâ¸öĿ¼ÊÇ·ñ´æÔÚʱ£¬ÄѵÀÒ»¶¨ÒªÊ¹Óà ls À´Ö´ÐУ¬ È»ºóÔÙÒÔ $? À´ÅжÏÖ´ÐгɹûÂ𣿺Ǻǣ¡µ±È»²»ÐèÒª£¡ ÎÒÃÇ¿ÉÒÔ͸¹ý¡º test ¡»Õâ¸öÖ¸ÁîÀ´Õì²âÄØ£¡
    --------------------------------------------------------------------------------
    ÀûÓà test Ö¸ÁîµÄ²âÊÔ¹¦ÄÜ
    µ±ÎÒÒª¼ì²âϵͳÉÏÃæijЩµµ°¸»òÕßÊÇÏà¹ØµÄÊôÐÔʱ£¬ÀûÓà test Õâ¸öÖ¸ÁîÀ´¹¤×÷£¬ ÕæÊǺÃÓõò»µÃÁË£¬¾ÙÀýÀ´Ëµ£¬ÎÒÒª¼ì²é /dmtsai ÊÇ·ñ´æÔÚʱ£¬Ê¹Óãº
    [root@linux ~]# test -e /dmtsai
    Ö´Ðнá¹û²¢²»»áÏÔʾÈκÎѶϢ£¬µ«×îºóÎÒÃÇ¿ÉÒÔ͸¹ý $? »ò && ¼° || À´Õ¹ÏÖÕû¸ö½á¹ûÄØ£¡ ÀýÈçÎÒÃÇÔÚ½«ÉÏÃæµÄÀý×Ó¸Äд³ÉÕâÑù£º
    [root@linux ~]# test -e /dmtsai && echo "exist" || echo "Not exist"
    ×îÖյĽá¹û¿ÉÒÔ¸æÖªÎÒÃÇÊÇ¡ºexist¡»»¹ÊÇ¡ºNot exist¡»ÄØ£¡ÄÇÎÒÖªµÀ -e ÊDzâÊÔÒ»¸ö¡º¶«Î÷¡»ÔÚ²»ÔÚ£¬ Èç¹û»¹ÏëÒª²âÊÔһϸõµÃûÊÇɶÍæÒâ¶ùʱ£¬»¹ÓÐÄÄЩ±êÖ¾¿ÉÒÔÀ´ÅжϵÄÄØ£¿ºÇºÇ£¡Óе×ÏÂÕâЩ¶«Î÷ร¡
    ²âÊԵıêÖ¾ ´ú±íÒâÒå
    1. ¹ØÓÚij¸öµµÃûµÄ¡ºÀàÐÍ¡»Õì²â(´æÔÚÓë·ñ)£¬Èç test -e filename
    -e ¸Ã¡ºµµÃû¡»ÊÇ·ñ´æÔÚ£¿(³£ÓÃ)
    -f ¸Ã¡ºµµÃû¡»ÊÇ·ñΪµµ°¸(file)£¿(³£ÓÃ)
    -d ¸Ã¡ºÎļþÃû¡»ÊÇ·ñΪĿ¼(directory)£¿(³£ÓÃ)
    -b ¸Ã¡ºµµÃû¡»ÊÇ·ñΪһ¸ö block device ×°Öã¿
    -c ¸Ã¡ºµµÃû¡»ÊÇ·ñΪһ¸ö character device ×°Öã¿
    -S ¸Ã¡ºµµÃû¡»ÊÇ·ñΪһ¸ö Socket µµ°¸£¿
    -p ¸Ã¡ºµµÃû¡»ÊÇ·ñΪһ¸ö FIFO (pipe) µµ°¸£¿
    -L ¸Ã¡ºµµÃû¡»ÊÇ·ñΪһ¸öÁ¬½áµµ£¿
    2. ¹ØÓÚµµ°¸µÄȨÏÞÕì²â£¬Èç test -r filename
    -r Õì²â¸ÃµµÃûÊÇ·ñ¾ßÓСº¿É¶Á¡»µÄÊôÐÔ£¿
    -w Õì²â¸ÃµµÃûÊÇ·ñ¾ßÓСº¿Éд¡»µÄÊôÐÔ£¿
    -x Õì²â¸ÃµµÃûÊÇ·ñ¾ßÓСº¿ÉÖ´ÐС»µÄÊôÐÔ£¿
    -u Õì²â¸ÃÎļþÃûÊÇ·ñ¾ßÓСºSUID¡»µÄÊôÐÔ£¿
    -g Õì²â¸ÃÎļþÃûÊÇ·ñ¾ßÓСºSGID¡»µÄÊôÐÔ£¿
    -k Õì²â¸ÃÎļþÃûÊÇ·ñ¾ßÓСºSticky bit¡»µÄÊôÐÔ£¿
    -s Õì²â¸ÃµµÃûÊÇ·ñΪ¡º·Ç¿Õ°×µµ°¸¡»£¿
    3. Á½¸öµµ°¸Ö®¼äµÄ±È½Ï£¬È磺 test file1 -nt file2
    -nt (newer than)ÅÐ¶Ï file1 ÊÇ·ñ±È file2 ÐÂ
    -ot (older than)ÅÐ¶Ï file1 ÊÇ·ñ±È file2 ¾É
    -ef ÅÐ¶Ï file2 Óë file2 ÊÇ·ñΪͬһµµ°¸£¬¿ÉÓÃÔÚÅÐ¶Ï hard link µÄÅж¨ÉÏ¡£ Ö÷ÒªÒâÒåÔÚÅж¨£¬Á½¸öµµ°¸ÊÇ·ñ¾ùÖ¸Ïòͬһ¸ö inode Á¨£¡
    4. ¹ØÓÚÁ½¸öÕûÊýÖ®¼äµÄÅж¨£¬ÀýÈç test n1 -eq n2
    -eq Á½ÊýÖµÏàµÈ (equal)
    -ne Á½ÊýÖµ²»µÈ (not equal)
    -gt n1 ´óÓÚ n2 (greater than)
    -lt n1 СÓÚ n2 (less than)
    -ge n1 ´óÓÚµÈÓÚ n2 (greater than or equal)
    -le n1 СÓÚµÈÓÚ n2 (less than or equal)
    5. Åж¨×Ö·û´®µÄÊý¾Ý
    test -z string Åж¨×Ö·û´®ÊÇ·ñΪ 0 £¿Èô string Ϊ¿Õ×Ö·û´®£¬ÔòΪ true
    test -n string Åж¨×Ö·û´®ÊÇ·ñ·ÇΪ 0 £¿Èô string Ϊ¿Õ×Ö·û´®£¬ÔòΪ false¡£
    ×¢£º -n Òà¿ÉÊ¡ÂÔ
    test str1 = str2 Åж¨ str1 ÊÇ·ñµÈÓÚ str2 £¬ÈôÏàµÈ£¬Ôò»Ø´« true
    test str1 != str2 Åж¨ str1 ÊÇ·ñ²»µÈÓÚ str2 £¬ÈôÏàµÈ£¬Ôò»Ø´« false
    6. ¶àÖØÌõ¼þÅж¨£¬ÀýÈ磺 test -r filename -a -x filename
    -a (and)Á½×´¿öͬʱ³ÉÁ¢£¡ÀýÈç test -r file -a -x file£¬Ôò file ͬʱ¾ßÓÐ r Óë x ȨÏÞʱ£¬²Å»Ø´« true¡£
    -o (or)Á½×´¿öÈκÎÒ»¸ö³ÉÁ¢£¡ÀýÈç test -r file -o -x file£¬Ôò file ¾ßÓÐ r »ò x ȨÏÞʱ£¬¾Í¿É»Ø´« true¡£
    ! ·´Ïà״̬£¬Èç test ! -x file £¬µ± file ²»¾ßÓÐ x ʱ£¬»Ø´« true
    OK£¡ÏÖÔÚÎÒÃǾÍÀûÓà test À´°ïÎÒÃÇд¼¸¸ö¼òµ¥µÄÀý×Ó¡£Ê×ÏÈ£¬ÅжÏһϣ¬ ÈÃʹÓÃÕßÊäÈëÒ»¸öµµÃû£¬ÎÒÃÇÅжϣº
    Õâ¸öµµ°¸ÊÇ·ñ´æÔÚ£¬Èô²»´æÔÚÔò¸øÓèÒ»¸ö¡ºFilename does not exist¡»µÄѶϢ£¬²¢ÖжϳÌÐò£»
    ÈôÕâ¸öµµ°¸´æÔÚ£¬ÔòÅжÏËûÊǸöµµ°¸»òĿ¼£¬½á¹ûÊä³ö¡ºFilename is regular file¡»»ò ¡ºFilename is directory¡»
    ÅжÏһϣ¬Ö´ÐÐÕßµÄÉí·Ý¶ÔÕâ¸öµµ°¸»òĿ¼ËùÓµÓеÄȨÏÞ£¬²¢Êä³öȨÏÞÊý¾Ý£¡
    Äã¿ÉÒÔÏÈ×ÔÐд´×÷¿´¿´£¬È»ºóÔÙ¸úµ×ϵĽá¹ûÌÖÂÛÌÖÂÛ¡£×¢ÒâÀûÓà test Óë && »¹ÓÐ || µÈ±êÖ¾£¡
    [root@linux scripts]# vi sh05.sh
    #!/bin/bash
    # Program:
    # Let user input a filename, the program will search the filename
    # 1.) exist? 2.) file/directory? 3.) file permissions
    # History:
    # 2005/08/25 VBird First release
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    # 1. ÈÃʹÓÃÕßÊäÈëµµÃû£¬²¢ÇÒÅжÏʹÓÃÕßÊÇ·ñÕæµÄÓÐÊäÈë×Ö·û´®£¿
    echo -e "The program will show you that filename is exist which input by you. "
    read -p "Input a filename : " filename
    test -z $filename && echo "You MUST input a filename." && exit 0
    # 2. Åжϵµ°¸ÊÇ·ñ´æÔÚ£¿
    test ! -e $filename && echo "The filename $filename DO NOT exist" && exit 0
    # 3. ¿ªÊ¼Åжϵµ°¸ÀàÐÍÓëÊôÐÔ
    test -f $filename && filetype="regulare file"
    test -d $filename && filetype="directory"
    test -r $filename && perm="readable"
    test -w $filename && perm="$perm writable"
    test -x $filename && perm="$perm executable"
    # 4. ¿ªÊ¼Êä³öÐÅÏ¢£¡
    echo "The filename: $filename is a $filetype"
    echo "And the permission are : $perm"
    ºÜÓÐȤµÄÀý×Ó°É£¡Äú¿ÉÒÔ×ÔÐÐÔÙÒÔÆäËüµÄ°¸ÀýÀ´×«Ð´Ò»Ï¿ÉÓõŦÄÜÄØ£¡
    --------------------------------------------------------------------------------
    ÀûÓÃÅжϷûºÅ [ ]
    ³ýÁËÎÒÃǺÜϲ»¶Ê¹ÓÃµÄ test Ö®Í⣬Æäʵ£¬ÎÒÃÇ»¹¿ÉÒÔÀûÓÃÅжϷûºÅ¡º [ ] ¡»À´½øÐÐÊý¾ÝµÄÅжÏÄØ£¡ ¾ÙÀýÀ´Ëµ£¬Èç¹ûÎÒÏëÒªÖªµÀ $HOME Õâ¸ö±äÁ¿ÊÇ·ñΪ¿ÕµÄ£¬¿ÉÒÔÕâÑù×ö£º
    [root@linux ~]# [ -z "$HOME" ]
    µ«Ê¹Óà [] ÒªÌرð×¢ÒâµÄÊÇ£¬ÔÚÉÏÊöµÄÿ¸ö×é¼þÖм䶼ÐèÒªÓпոñ¼üÀ´·Ö¸ô£¬¼ÙÉèÎÒ¿Õ¸ñ¼üʹÓạõ¡»À´±íʾ£¬ ÄÇô£¬ÔÚÕâЩµØ·½Ä㶼ÐèÒªÓпոñ¼ü£º
    [ "$HOME" == "$MAIL" ]
    [¡õ"$HOME"¡õ==¡õ"$MAIL"¡õ]
    ¡ü ¡ü ¡ü ¡ü
    ÉÏÃæµÄÀý×ÓÔÚ˵Ã÷£¬Á½¸ö×Ö·û´® $HOME Óë $MAIL ÊÇ·ñÏàͬµÄÒâ˼£¬Ï൱ÓÚ test $HOME = $MAIL µÄÒâ˼À²£¡ ¶øÈç¹ûûÓпհ׷ָô£¬ÀýÈç [$HOME==$MAIL] ʱ£¬ÎÒÃÇµÄ bash ¾Í»áÏÔʾ´íÎóѶϢÁË£¡Õâ¿ÉÒªºÜ×¢Òâ°¡£¡ ËùÒÔ˵£¬Äú×îºÃҪעÒ⣺
    ÔÚÖÐÀ¨ºÅ [] ÄÚµÄÿ¸ö×é¼þ¶¼ÐèÒªÓпոñ¼üÀ´·Ö¸ô£»
    ÔÚÖÐÀ¨ºÅÄڵıäÁ¿£¬×îºÃ¶¼ÒÔË«ÒýºÅÀ´É趨£»
    ÔÚÖÐÀ¨ºÅÄڵij£Êý£¬×îºÃ¶¼ÒÔµ¥»òË«ÒýºÅÀ´É趨¡£
    ¾ÙÀýÀ´Ëµ£¬¼ÙÈçÎÒÉ趨ÁË name="VBird Tsai" £¬È»ºóÕâÑùÅж¨£º
    [root@linux ~]# name="VBird Tsai"
    [root@linux ~]# [ $name == "VBird" ]
    bash: [: too many arguments
    ΪʲôÄØ£¿ÒòΪ $name Èç¹ûûÓÐʹÓÃË«ÒýºÅ¹ÎÆðÀ´£¬ÄÇôÉÏÃæµÄÅж¨Ê½»á±ä³É£º
    [ VBird Tsai == "VBird" ]
    ¶ø²»ÊÇÎÒÃÇÒªµÄ£º
    [ "VBird Tsai" == "VBird" ]
    Õâ¿ÉÊDzîºÜ¶àµÄร¡ÁíÍ⣬ÖÐÀ¨ºÅµÄʹÓ÷½·¨Óë±êÖ¾Óë test ¼¸ºõһģһÑù°¡¡« Ö»ÊÇÖÐÀ¨ºÅ±È½Ï³£ÓÃÔÚÌõ¼þÅжÏʽ if ..... then ..... fi µÄÇé¿öÖоÍÊÇÁË¡£ ºÃ£¬ÄÇÎÒÃÇÒ²¼ÌÐøÀ´×öÒ»¸öС°¸ÀýºÃÁË£º
    µ±Ö´ÐÐÒ»¸ö³ÌÐòµÄʱºò£¬Õâ¸ö³ÌÐò»áÈÃʹÓÃÕßÑ¡Ôñ Y »ò N £¬
    Èç¹ûʹÓÃÕßÊäÈë Y »ò y ʱ£¬¾ÍÏÔʾ¡º OK, continue ¡»
    Èç¹ûʹÓÃÕßÊäÈë n »ò N ʱ£¬¾ÍÏÔʾ¡º Oh, interrupt £¡¡»
    Èç¹û²»ÊÇ Y/y/N/n Ö®ÄÚµÄÆäËü×Ö·û£¬¾ÍÏÔʾ¡ºI don't know what is your choise¡»
    ÀûÓÃÖÐÀ¨ºÅ¡¢ && Óë || À´¼ÌÐø°É£¡
    [root@linux scripts]# vi sh06.sh
    #!/bin/bash
    # Program:
    # This program will show the user's choice
    # History:
    # 2005/08/25 VBird First release
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    read -p "Please input (Y/N): " yn
    [ "$yn" == "Y" -o "$yn" == "y" ] && echo "OK, continue" && exit 0
    [ "$yn" == "N" -o "$yn" == "n" ] && echo "Oh, interrupt!" && exit 0
    echo "I don't know what is your choise" && exit 0
    ºÜÓÐȤ°É£¡ÀûÓÃÕâ¸ö×Ö·û´®ÅбðµÄ·½·¨£¬ÎÒÃǾͿÉÒÔºÜÇáËɵĽ«Ê¹ÓÃÕßÏëÒª½øÐеŤ×÷·ÖÃűðÀàÄØ£¡ ½ÓÏÂÀ´£¬ÎÒÃÇÔÙÀ´Ì¸Ò»Ð©ÆäËüÓеÄûÓеĶ«Î÷°É£¡
    Tips:
    ΪʲôÅжÏʽÀïÃæÏ´ïµÈÓÚÒªÓà == ¶ø²»ÊÇÒ»¸ö = ¾ÍºÃÁËÄØ£¿ÎÒÃÇÔÚÇ°Ò»ÕÂÕý¹æ±íʾ·¨ÀïÃæµÄ awk Ìáµ½£¬ Ö»ÓÐÒ»¸ö = ÓÃÀ´¸øÓèÒ»¸ö±äÁ¿É趨ÆäÄÚÈÝ£¬Âß¼­ÅжÏʱ£¬Ôò»á¸øÓèÁ½¸öµÈÓÚ£¬ Ò༴¡º±È½Ï¡»¶ø·Ç¡ºÉ趨¡»µÄÒâ˼¡«ÕâÀïÒªºÃºÃµÄ·Ö±æÒ»ÏÂร¡ ^_^
    --------------------------------------------------------------------------------
    Shell script µÄÔ¤Éè±äÊý($0, $1...)
    Æäʵ£¬µ±ÎÒÃÇÖ´ÐÐÒ»¸ö shell script ʱ£¬ÔÚÕâ¸ö shell script ÀïÃæ¾ÍÒѽ«°ïÎÒÃÇ×öºÃһЩ¿ÉÓõıäÁ¿ÁË¡£ ¾ÙÀýÀ´Ëµ£¬ÔÚ²»¾ÃµÄ½«À´£¬Äú¾Í»á·¢ÏÖ£¬µ±ÎÒÃÇÒªÆô¶¯Ò»¸öϵͳ·þÎñʱ£¬¿ÉÄÜ»áÏ´ïÀàËÆÕâÑùµÄÖ¸Á
    [root@linux ~]# /etc/init.d/crond restart
    ÄÇÊÇɶÍæÒâ¶ù£¿ºÇºÇ£¡¾ÍÊÇ¡ºÏò /etc/init.d/crond Õâ¸ö script Ï´ï restart µÄÖ¸Áî¡»£¬ ß×£¡ÎÒÃDz»ÊǶ¼Ê¹Óà read À´¶ÁȡʹÓÃÕßÊäÈëµÄ±äÁ¿ÄÚÈÝÂð£¿ÎªÉ¶ÎÒ¿ÉÒÔÖ±½ÓÔÚ script ºóÃæ½ÓÉÏÕâ¸ö²ÎÊý£¿ ÕâÊÇÒòΪ shell script °ïÎÒÃÇÉ趨ºÃһЩָ¶¨µÄ±äÁ¿ÁË£¡±äÁ¿µÄ¶ÔÓ¦ÊÇÕâÑùµÄ£º
    /path/to/scriptname opt1 opt2 opt3 opt4 ...
    $0 $1 $2 $3 $4 ...
    ÕâÑù¹»Çå³þÁË°É£¿£¡Ö´ÐеÄÎļþÃûΪ $0 Õâ¸ö±äÁ¿£¬µÚÒ»¸ö½ÓµÄ²ÎÊý¾ÍÊÇ $1 °¡¡« ËùÒÔ£¬Ö»ÒªÎÒÃÇÔÚ script ÀïÃæÉÆÓà $1 µÄ»°£¬¾Í¿ÉÒԺܼòµ¥µÄÁ¢¼´Ï´ïijЩָÁÄÜÁË£¡ ºÃÁË£¬À´×ö¸öÀý×Ó°É¡«¼ÙÉèÎÒÒªÖ´ÐÐÒ»¸ö script £¬Ö´Ðк󣬸à script »á×Ô¶¯Áгö×Ô¼ºµÄµµÃû£¬ »¹ÓкóÃæ½ÓµÄÇ°Èý¸ö²ÎÊý£¬¸ÃÈçºÎÊǺã¿
    [root@linux scripts]# vi sh07.sh
    #!/bin/bash
    # Program:
    # The program will show it's name and first 3 parameters.
    # History:
    # 2005/08/25 VBird First release
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    echo "The script naem is ==>; $0"
    [ -n "$1" ] && echo "The 1st paramter is ==>; $1" || exit 0
    [ -n "$2" ] && echo "The 2nd paramter is ==>; $2" || exit 0
    [ -n "$3" ] && echo "The 3th paramter is ==>; $3" || exit 0
    ÕâÖ§³ÌÐòÀïÃæÄñ¸ç¼ÓÉÏÁËһЩ¿ØÖÆʽ£¬Ò༴ÀûÓà && ¼° || À´¼ÓÒÔÅÐ¶Ï $1 ~ $3 ÊÇ·ñ´æÔÚ£¿ Èô´æÔÚ²ÅÏÔʾ£¬Èô²»´æÔÚ¾ÍÖжϡ«Ö´Ðнá¹ûÈçÏ£º
    [root@linux scripts]# sh sh07.sh theone haha quot
    The script naem is ==>; sh07.sh
    The 1st paramter is ==>; theone
    The 2nd paramter is ==>; haha
    The 3th paramter is ==>; quot
    ÉÏÃæÕâÆßµÄÀý×Ó¶¼ºÜ¼òµ¥°É£¿¼¸ºõ¶¼ÊÇÀûÓà bash µÄÏà¹Ø¹¦ÄܶøÒÑ¡« ²»ÄÑÀ²¡«µ×ÏÂÎÒÃǾÍҪʹÓÃÌõ¼þÅжÏʽÀ´½øÐÐһЩ·Ö±ð¹¦ÄܵÄÉ趨ÁË£¬ºÃºÃÇÆÒ»ÇÆÏÈ¡«
    --------------------------------------------------------------------------------
    Ìõ¼þÅжÏʽ£º
    Ö»Òª½²µ½¡º³ÌÐò¡»µÄ»°£¬ÄÇôÌõ¼þÅжÏʽ£¬Ò༴ÊÇ¡º if then ¡»ÕâÖÖÅбðʽ¿Ï¶¨Ò»¶¨ÒªÑ§Ï°µÄ£¡ ÒòΪºÜ¶àʱºò£¬ÎÒÃǶ¼±ØÐëÒªÒÀ¾ÝijЩÊý¾ÝÀ´ÅжϳÌÐò¸ÃÈçºÎ½øÐС£¾ÙÀýÀ´Ëµ£¬ÎÒÃÇÔÚÉÏÍ·²»ÊÇÓÐÁ·Ï°µ±Ê¹ÓÃÕßÊäÈë Y/N ʱ£¬±ØÐëÒªÖ´Ðв»Í¬µÄѶϢÊä³öÂ𣿼òµ¥µÄ·½Ê½¿ÉÒÔÀûÓà && Óë || £¬µ«Èç¹ûÎÒ»¹ÏëÒªÖ´ÐÐÒ»¶ÑÖ¸ÁîÄØ£¿ ÄÇÕæµÄµÃÒª if then À´°ïæ?¡«µ×ÏÂÎÒÃǾÍÀ´ÁÄÒ»ÁÄ£¡
    --------------------------------------------------------------------------------
    ÀûÓÃ if .... then
    Õâ¸ö if .... then ÊÇ×î³£¼ûµÄÌõ¼þÅжÏʽÁË¡«¼òµ¥µÄ˵£¬¾ÍÊǵ±·ûºÏij¸öÌõ¼þÅжϵÄʱºò£¬ ¾ÍÓèÒÔ½øÐÐijÏ×÷¾ÍÊÇÁË¡£ÎÒÃÇ¿ÉÒÔ¼òµ¥µÄÕâÑù¿´£º
    if [ Ìõ¼þÅжÏʽ ]; then
    µ±Ìõ¼þÅжÏʽ³ÉÁ¢Ê±£¬¿ÉÒÔ½øÐеÄÖ¸Á×÷ÄÚÈÝ£»
    fi
    ÖÁÓÚÌõ¼þÅжÏʽµÄÅжϷ½·¨£¬ÓëǰһС½ÚµÄ½éÉÜÏàͬ°¡£¡½ÏÌرðµÄÊÇ£¬Èç¹ûÎÒÓжà¸öÌõ¼þÒªÅбðʱ£¬ ³ýÁË sh06.sh ÄǸö°¸Àý£¬Ò²¾ÍÊǽ«¶à¸öÌõ¼þдÈëÒ»¸öÖÐÀ¨ºÅÄÚµÄÇé¿öÖ®Í⣬ ÎÒ»¹¿ÉÒÔÓжà¸öÖÐÀ¨ºÅÀ´¸ô¿ªà¸£¡¶øÀ¨ºÅÓëÀ¨ºÅÖ®¼ä£¬ÔòÒÔ && »ò || À´¸ô¿ª£¬ËûÃǵÄÒâÒåÊÇ£º
    && ´ú±í AND £»
    || ´ú±í or £»
    ËùÒÔ£¬ÔÚʹÓÃÖÐÀ¨ºÅµÄÅжÏʽÖУ¬ && ¼° || ¾ÍÓëÖ¸ÁîÏ´ïµÄ״̬²»Í¬ÁË¡£¾ÙÀýÀ´Ëµ£¬ sh06.sh ÄǸöÀý×ÓÎÒ¿ÉÒÔ¸Äд³ÉÕâÑù£º
    [root@linux scripts]# vi sh06-2.sh
    #!/bin/bash
    # Program:
    # This program will show the user's choice
    # History:
    # 2005/08/25 VBird First release
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    read -p "Please input (Y/N): " yn
    if [ "$yn" == "Y" ] || [ "$yn" == "y" ]; then
    echo "OK, continue"
    exit 0
    fi
    if [ "$yn" == "N" ] || [ "$yn" == "n" ]; then
    echo "Oh, interrupt!"
    exit 0
    fi
    echo "I don't know what is your choise" && exit 0
    ²»¹ý£¬ÓÉÕâ¸öÀý×Ó¿´ÆðÀ´£¬ËƺõҲûÓÐʲôÁ˲»Æð°É£¿ sh06.sh »¹±È½Ï¼òµ¥ÄØ¡« µ«ÊÇ£¬Èç¹ûÎÒÃÇ¿¼Âǵ×ϵÄ״̬£¬Äú¾Í»áÖªµÀ if then µÄºÃ´¦ÁË£º
    if [ Ìõ¼þÅжÏʽ ]; then
    µ±Ìõ¼þÅжÏʽ³ÉÁ¢Ê±£¬¿ÉÒÔ½øÐеÄÖ¸Á×÷ÄÚÈÝ£»
    else
    µ±Ìõ¼þÅжÏʽ²»³ÉÁ¢Ê±£¬¿ÉÒÔ½øÐеÄÖ¸Á×÷ÄÚÈÝ£»
    fi
    Èç¹û¿¼ÂǸü¸´ÔÓµÄÇé¿ö£¬Ôò¿ÉÒÔʹÓÃÕâ¸öÓï·¨£º
    if [ Ìõ¼þÅжÏʽһ ]; then
    µ±Ìõ¼þÅжÏʽһ³ÉÁ¢Ê±£¬¿ÉÒÔ½øÐеÄÖ¸Á×÷ÄÚÈÝ£»
    elif [ Ìõ¼þÅжÏʽ¶þ ]; then
    µ±Ìõ¼þÅжÏʽ¶þ³ÉÁ¢Ê±£¬¿ÉÒÔ½øÐеÄÖ¸Á×÷ÄÚÈÝ£»
    else
    µ±Ìõ¼þÅжÏʽһÓë¶þ¾ù²»³ÉÁ¢Ê±£¬¿ÉÒÔ½øÐеÄÖ¸Á×÷ÄÚÈÝ£»
    fi
    ÄÇÎҾͿÉÒÔ½« sh06-2.sh ¸Äд³ÉÕâÑù£º
    [root@linux scripts]# vi sh06-3.sh
    #!/bin/bash
    # Program:
    # This program will show the user's choice
    # History:
    # 2005/08/25 VBird First release
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    read -p "Please input (Y/N): " yn
    if [ "$yn" == "Y" ] || [ "$yn" == "y" ]; then
    echo "OK, continue"
    elif [ "$yn" == "N" ] || [ "$yn" == "n" ]; then
    echo "Oh, interrupt!"
    else
    echo "I don't know what is your choise"
    fi
    ÊÇ·ñ³ÌÐò±äµÃºÜ¼òµ¥£¬¶øÇÒÒÀÐòÅжϣ¬¿ÉÒÔ±ÜÃâµôÖظ´ÅжϵÄ×´¿ö£¬ÕâÑùÕæµÄºÜÈÝÒ×Éè¼Æ³ÌÐòµÄÀ²£¡ ^_^ ºÃÁË£¬ÄÇôÈç¹ûÎÒÒªÕì²âÄãËùÊäÈëµÄ²ÎÊýÊÇ·ñΪ hello ÄØ £¬ Ò²¾ÍÊÇ˵£¬Èç¹ûÎÒÏëÒªÖªµÀ£¬ÄãÔÚ³ÌÐòºóÃæËù½ÓµÄµÚÒ»¸ö²ÎÊý (¾ÍÊÇ $1 °¡£¡) ÊÇ·ñΪ hello £¬
    Èç¹ûÊǵĻ°£¬¾ÍÏÔʾ "Hello, how are you ?"£»
    Èç¹ûûÓмÓÈκβÎÊý£¬¾ÍÌáʾʹÓÃÕß±ØÐëҪʹÓõIJÎÊýÏ´﷨£»
    ¶øÈç¹û¼ÓÈëµÄ²ÎÊý²»ÊÇ hello £¬¾ÍÌáÐÑʹÓÃÕß½öÄÜʹÓà hello Ϊ²ÎÊý¡£
    Õû¸ö³ÌÐòµÄ׫д¿ÉÒÔÊÇÕâÑùµÄ£º
    [root@linux scripts]# vi sh08.sh
    #!/bin/bash
    # Program:
    # Show "Hello" from $1....
    # History:
    # 2005/08/28 VBird First release
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    if [ "$1" == "hello" ]; then
    echo "Hello, how are you ?"
    elif [ "$1" == "" ]; then
    echo "You MUST input parameters, ex>; $0 someword"
    else
    echo "The only parameter is 'hello'"
    fi
    È»ºóÄú¿ÉÒÔÖ´ÐÐÕâÖ§³ÌÐò£¬·Ö±ðÔÚ $1 µÄλÖÃÊäÈë hello, ûÓÐÊäÈëÓëËæÒâÊäÈ룬 ¾Í¿ÉÒÔ¿´µ½²»Í¬µÄÊä³ö?¡«ÊÇ·ñ»¹¾õµÃͦ¼òµ¥µÄ°¡£¡ ^_^¡£ÊÂʵÉÏ£¬ ѧµ½ÕâÀҲÕæµÄºÜÀ÷º¦ÁË¡«ºÃÁË£¬µ×ÏÂÎÒÃǼÌÐøÀ´ÍæһЩ±È½Ï´óÒ»µãµÄ?¡« ÎÒÃÇÔÚÇ°Ò»ÕÂÒѾ­Ñ§»áÁË grep Õâ¸öºÃÓõÄÍæÒâ¶ù£¬ÄÇô¶àѧһ¸ö½Ð×ö netstat µÄÖ¸Á Õâ¸öÖ¸Áî¿ÉÒÔ²éѯµ½Ä¿Ç°Ö÷»úÓпªÆôµÄÍøÂç·þÎñ¶Ë¿Ú¿Ú (service ports)£¬ Ïà¹ØµÄ¹¦ÄÜÎÒÃÇ»áÔÚ·þÎñÆ÷¼ÜÉèƪ¼ÌÐø½éÉÜ£¬ÕâÀïÄúÖ»ÒªÖªµÀ£¬ÎÒ¿ÉÒÔÀûÓẠnetstat -tuln ¡»À´È¡µÃÄ¿Ç°Ö÷»úÓÐÆô¶¯µÄ·þÎñ£¬ ¶øÇÒÈ¡µÃµÄÐÅÏ¢ÓеãÏñÕâÑù£º
    [root@linux ~]# netstat -tuln
    Active Internet connections (only servers)
    Proto Recv-Q Send-Q Local Address Foreign Address State
    tcp 0 0 0.0.0.0:199 0.0.0.0:* LISTEN
    tcp 0 0 :::80 :::* LISTEN
    tcp 0 0 :::22 :::* LISTEN
    tcp 0 0 :::25 :::* LISTEN
    ÉÏÃæµÄÖصãÊÇÌØÊâ×ÖÌåµÄÄǸö²¿·Ö£¬ÄÇЩÌØÊâ×ÖÌåµÄ²¿·Ö´ú±íµÄ¾ÍÊÇ port ?¡« ÄÇôÿ¸ö port ´ú±íµÄÒâÒåΪºÎÄØ£¿¼¸¸ö³£¼ûµÄ port ÓëÏà¹ØÍøÂç·þÎñµÄ¹ØϵÊÇ£º
    80: WWW
    22: ssh
    21: ftp
    25: mail
    ÄÇÎÒÈçºÎ͸¹ý netstat È¥Õì²âÎÒµÄÖ÷»úÊÇ·ñÓпªÆôÕâËĸöÖ÷ÒªµÄÍøÂç·þÎñ¶Ë¿Ú¿ÚÄØ£¿ ÎÒ¿ÉÒÔ¼òµ¥µÄÕâÑùȥдÕâ¸ö³ÌÐòรº
    [root@linux scripts]# vi sh09.sh
    #!/bin/bash
    # Program:
    # Using netstat and grep to detect WWW,SSH,FTP and Mail services.
    # History:
    # 2005/08/28 VBird First release
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    # 1. ÏÈ×÷һЩ¸æÖªµÄ¶¯×÷¶øÒÑ¡«
    echo "Now, the services of your Linux system will be detect!"
    echo -e "The www, ftp, ssh, and mail will be detect! "
    # 2. ¿ªÊ¼½øÐÐһЩ²âÊԵŤ×÷£¬²¢ÇÒÒ²Êä³öһЩÐÅÏ¢?£¡
    testing=`netstat -tuln | grep ":80 "`
    if [ "$testing" != "" ]; then
    echo "WWW is running in your system."
    fi
    testing=`netstat -tuln | grep ":22 "`
    if [ "$testing" != "" ]; then
    echo "SSH is running in your system."
    fi
    testing=`netstat -tuln | grep ":21 "`
    if [ "$testing" != "" ]; then
    echo "FTP is running in your system."
    fi
    testing=`netstat -tuln | grep ":25 "`
    if [ "$testing" != "" ]; then
    echo "Mail is running in your system."
    fi
    ÕâÑùÓÖÄܹ»Ò»¸öÒ»¸öµÄ¼ì²é?¡«ÊÇ·ñºÜÓÐȤ°¡£¡ ^_^¡£½ÓÏÂÀ´£¬ÎÒÃÇÔÙÀ´Íæ¸üÄÑÒ»µãµÄ¡£ ÎÒÃÇÖªµÀ¿ÉÒÔÀûÓà date À´ÏÔʾÈÕÆÚÓëʱ¼ä£¬Ò²¿ÉÒÔÀûÓà $((¼ÆËãʽ)) À´¼ÆËãÊýÖµÔËËã¡£ ÁíÍ⣬ date Ò²¿ÉÒÔÓÃÀ´ÏÔʾ×Ô 19710101 ÒÔÀ´µÄ¡º×ÜÃëÊý¡» (Çë×ÔÐвéÔÄ man date ¼° info date) ¡£ÄÇô£¬ÄúÊÇ·ñ¿ÉÒÔ׫дһ֧С³ÌÐò£¬ÓÃÀ´¡º¼ÆËãÍËÎéÈÕÆÚ»¹Ê£¼¸Ì죿¡»Ò²¾ÍÊÇ˵£º
    ÏÈÈÃʹÓÃÕßÊäÈëËûÃǵÄÍËÎéÈÕÆÚ£»
    ÔÙÓÉÏÖÔÚÈÕÆڱȶÔÍËÎéÈÕÆÚ£»
    ÓÉÁ½¸öÈÕÆڵıȽÏÀ´ÏÔʾ¡º»¹ÐèÒª¼¸Ìì¡»²ÅÄܹ»ÍËÎéµÄ×ÖÑù¡£
    ËƺõͦÄѵÄÑù×Ó£¿ÆäʵҲ²»»áÀ²£¬ÀûÓẠdate --date="YYYYMMDD" +%s ¡»¾ÍÄܹ»´ïµ½ÎÒÃÇËùÏëÒªµÄ?¡«Èç¹ûÄúÒѾ­Ð´ÍêÁ˳ÌÐò£¬¶ÔÕÕµ×ϵÄд·¨ÊÔ¿´¿´£º
    [root@linux scripts]# vi sh10.sh
    #!/bin/bash
    # Program:
    # Tring to calculate your demobilization date at how many days
    # later...
    # History:
    # 2005/08/29 VBird First release
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    # 1. ¸æ֪ʹÓÃÕßÕâÖ§³ÌÐòµÄÓÃ;£¬²¢ÇÒ¸æÖªÓ¦¸ÃÈçºÎÊäÈëÈÕÆÚ¸ñʽ£¿
    echo "This program will try to calculate :"
    echo "How many days about your demobilization date..."
    read -p "Please input your demobilization date (YYYYMMDD ex>;20050401): " date2
    # 2. ²âÊÔһϣ¬Õâ¸öÊäÈëµÄÄÚÈÝÊÇ·ñÕýÈ·£¿ÀûÓÃÕý¹æ±íʾ·¨?¡«
    date_d=`echo $date2 |grep '[0-9]{8}'`
    if [ "$date_d" == "" ]; then
    echo "You input the wrong format of date...."
    exit 1
    fi
    # 3. ¿ªÊ¼¼ÆËãÈÕÆÚ?¡«
    declare -i date_dem=`date --date="$date2" +%s`
    declare -i date_now=`date +%s`
    declare -i date_total_s=$(($date_dem-$date_now))
    declare -i date_d=$(($date_total_s/60/60/24))
    if [ "$date_total_s" -lt "0" ]; then
    echo "You had been demobilization before: " $((-1*$date_d)) " ago"
    else
    declare -i date_h=$(($(($date_total_s-$date_d*60*60*24))/60/60))
    echo "You will be demobilized after $date_d days and $date_h hours."
    fi
    ÇÆÒ»ÇÆ£¬ÕâÖ§³ÌÐò¿ÉÒÔ°ïÄú¼ÆËãÍËÎéÈÕÆÚÄØ¡«Èç¹ûÊÇÒѾ­ÍËÎéµÄÅóÓÑ£¬ »¹¿ÉÒÔÖªµÀÒѾ­ÍËÎé¶à¾ÃÁË¡«¹þ¹þ£¡ºÜ¿É°®°É¡«ÀûÓà date Ëã³ö×Ô 1971/01/01 ÒÔÀ´µÄ×ÜÃëÊý£¬ ÔÙÓëÄ¿Ç°µÄ×ÜÃëÊýÀ´±È¶Ô£¬È»ºóÒÔÒ»ÌìµÄ×ÜÃëÊý (60*60*24) Ϊ»ùÊýÈ¥¼ÆËã×ÜÈÕÊý£¬ ¾ÍÄܹ»µÃÖªÁ½ÕߵIJîÒìÁË¡«ÇÆ¡«È«²¿µÄ¶¯×÷¶¼Ã»Óг¬³öÎÒÃÇËùѧµÄ·¶Î§°É¡« ^_^ »¹Äܹ»±ÜÃâʹÓÃÕßÊäÈë´íÎóµÄÊý×Ö£¬ËùÒÔ¶àÁËÒ»¸öÕý¹æ±íʾ·¨µÄÅжÏʽÄØ¡« Õâ¸öÀý×ӱȽÏÄÑ£¬ÓÐÐËȤÏëҪһ̽¾¿¾¹µÄÅóÓÑ£¬¿ÉÒÔ×÷һϿκóÁ·Ï°Ìâ ¹ØÓÚ¼ÆËãÉúÈÕµÄÄÇÒ»Ìâร¡¡«¼ÓÓÍ£¡
    --------------------------------------------------------------------------------
    ÀûÓà case ..... esac ÅжÏ
    ÉϸöС½ÚÌáµ½µÄ¡º if .... then .... fi ¡»¶ÔÓÚ±äÁ¿µÄÅжÏÖУ¬ ÊÇÒԱȶԵķ½Ê½À´·Ö±æµÄ£¬Èç¹û·ûºÏ״̬¾Í½øÐÐijЩÐÐΪ£¬²¢ÇÒ͸¹ý½Ï¶à²ã´Î ( ¾ÍÊÇ elif ... ) µÄ·½Ê½À´½øÐжà¸ö±äÁ¿µÄ³ÌÐò´úÂë׫д£¬Æ©Èç sh08.sh ÄǸöС³ÌÐò£¬¾ÍÊÇÓÃÕâÑùµÄ·½Ê½À´µÄ?¡£ ºÃ£¬ÄÇôÍòÒ»ÎÒÓжà¸ö¼È¶¨µÄ±äÁ¿ÄÚÈÝ£¬ÀýÈç sh08.sh µ±ÖУ¬ÎÒËùÐèÒªµÄ±äÁ¿¾ÍÊÇ "hello" ¼°¿Õ×Ö·û´®Á½¸ö£¬ ÄÇôÎÒÖ»ÒªÕë¶ÔÕâÁ½¸ö±äÁ¿À´É趨״¿ö¾ÍºÃÁ˶԰ɣ¿£¡ÄÇô¿ÉÒÔʹÓÃʲô·½Ê½À´Éè¼ÆÄØ£¿ ºÇºÇ¡«¾ÍÓà case ... in .... esac °É¡«£¬ËûµÄÓï·¨ÈçÏ£º
    case $±äÁ¿Ãû³Æ in
    "µÚÒ»¸ö±äÁ¿ÄÚÈÝ")
    ³ÌÐò¶Î
    ;;
    "µÚ¶þ¸ö±äÁ¿ÄÚÈÝ")
    ³ÌÐò¶Î
    ;;
    *)
    ²»°üº¬µÚÒ»¸ö±äÁ¿ÄÚÈÝÓëµÚ¶þ¸ö±äÁ¿ÄÚÈݵÄÆäËü³ÌÐòÖ´ÐжÎ
    exit 1
    ;;
    esac
    ҪעÒâµÄÊÇ£¬Õâ¸öÓï·¨ÊÇÒÔ case Ϊ¿ªÍ·£¬¶øÒÔ esac Ϊ½áβ£¬É¶£¿ÎªºÎÊÇ esac ÄØ£¿ÏëÒ»Ï룬¼ÈÈ» if µÄ½áβÊÇ fi £¬ÄÇô case µÄ½áβµ±È»¾ÍÊǽ« case µ¹×Åд£¬×ÔÈ»¾ÍÊÇ esac ?¡« ^_^£¬ºÜºÃ¼Ç°É¡« ÁíÍ⣬ÿһ¸ö±äÁ¿ÄÚÈݵijÌÐò¶Î×îºó¶¼ÐèÒªÁ½¸ö·ÖºÅ (;;) À´´ú±í¸Ã³ÌÐò¶ÎÂäµÄ½áÊø£¬ÕâͦÖØÒªµÄร¡ ÖÁÓÚΪºÎÐèÒªÓÐ * Õâ¸ö±äÁ¿ÄÚÈÝÔÚ×îºóÄØ£¿ÕâÊÇÒòΪ£¬Èç¹ûʹÓÃÕß²»ÊÇÊäÈë±äÁ¿ÄÚÈÝÒ»»ò¶þʱ£¬ ÎÒÃÇ¿ÉÒÔ¸æ֪ʹÓÃÕßÏà¹ØµÄÐÅÏ¢°¡£¡¾ÙÀýÀ´Ëµ£¬ÎÒÃÇÈç¹û½« sh08.sh ¸ÄдµÄ»°£¬ ËûÓ¦¸Ã»á±ä³ÉÕâÑùร¡
    [root@linux scripts]# vi sh08-2.sh
    #!/bin/bash
    # Program:
    # Show "Hello" from $1.... by using case .... esac
    # History:
    # 2005/08/29 VBird First release
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    case $1 in
    "hello")
    echo "Hello, how are you ?"
    ;;
    "")
    echo "You MUST input parameters, ex>; $0 someword"
    ;;
    *)
    echo "Usage $0 {hello}"
    ;;
    esac
    ÔÚÉÏÃæÕâ¸ö sh08-2.sh µÄ°¸Àýµ±ÖУ¬Èç¹ûÄãÊäÈ롺 sh sh08-2.sh test ¡»À´Ö´ÐУ¬ ÄÇôÆÁÄ»ÉϾͻá³öÏÖ¡ºUsage sh08-2.sh {hello}¡»µÄ×ÖÑù£¬¸æÖªÖ´ÐÐÕß½öÄܹ»Ê¹Óà hello ม« ÕâÑùµÄ·½Ê½¶ÔÓÚÐèҪijЩ¹Ì¶¨×Ö·û´®À´Ö´ÐеıäÁ¿ÄÚÈݾÍÏԵĸü¼ÓµÄ·½±ãÄØ£¿ ÕâÖÖ·½Ê½ÄúÕæµÄÒªÊìϤร¡ÕâÊÇÒòΪϵͳµÄºÜ¶à·þÎñµÄÆô¶¯ scripts ¶¼ÊÇʹÓÃÕâÖÖд·¨µÄ£¬ ¾ÙÀýÀ´Ëµ£¬ÎÒÃÇ Linux µÄ·þÎñÆô¶¯·ÅÖÃĿ¼ÊÇÔÚ /etc/init.d/ µ±ÖУ¬ÎÒÒѾ­ÖªµÀÀïÍ·Óиö syslog µÄ·þÎñ£¬ÎÒÏëÒªÖØÐÂÆô¶¯Õâ¸ö·þÎñ£¬¿ÉÒÔÕâÑù×ö£º
    /etc/init.d/syslog restart
    ÖصãÊÇÄǸö restart À²¡«Èç¹ûÄú½øÈë /etc/init.d/syslog ¾Í»á¿´µ½ËûʹÓõÄÊÇ case Óï·¨£¬ ²¢ÇÒ»á¹æ¶¨Ä³Ð©¼È¶¨µÄ±äÁ¿ÄÚÈÝ£¬Äã¿ÉÒÔÖ±½ÓÏ´ï /etc/init.d/syslog £¬ ¸Ã script ¾Í»á¸æÖªÄãÓÐÄÄЩºóÐø½ÓµÄ±äÁ¿¿ÉÒÔʹÓÃ?¡«·½±ã°É£¡ ^_^
    Ò»°ãÀ´Ëµ£¬Ê¹ÓẠcase $±äÁ¿ in ¡»Õâ¸öÓï·¨ÖУ¬µ±ÖеÄÄǸö $±äÁ¿ ´óÖÂÓÐÁ½ÖÖÈ¡µÃµÄ·½Ê½£º
    Ö±½ÓÏ´ïʽ£ºÀýÈçÉÏÃæÌáµ½µÄ£¬ÀûÓẠscript.sh variable ¡» µÄ·½Ê½À´Ö±½Ó¸øÓè $1 Õâ¸ö±äÁ¿µÄÄÚÈÝ£¬ÕâÒ²ÊÇÔÚ /etc/init.d Ŀ¼Ï´ó¶àÊý³ÌÐòµÄÉè¼Æ·½Ê½¡£
    ½»»¥Ê½£ºÍ¸¹ý read Õâ¸öÖ¸ÁîÀ´ÈÃʹÓÃÕßÊäÈë±äÁ¿µÄÄÚÈÝ¡£
    Õâô˵»òÐíÄúµÄ¸ÐÊÜÐÔ»¹²»¸ß£¬ºÃ£¬ÎÒÃÇÖ±½Óд¸ö³ÌÐòÀ´ÍæÍ棺ÈÃʹÓÃÕßÄܹ»ÊäÈë one, two, three £¬ ²¢ÇÒ½«Ê¹ÓÃÕߵıäÁ¿ÏÔʾµ½ÆÁÄ»ÉÏ£¬Èç¹û²»ÊÇ one, two, three ʱ£¬¾Í¸æ֪ʹÓÃÕß½öÓÐÕâÈýÖÖÑ¡Ôñ¡£
    [root@linux scripts]# vi sh11.sh
    #!/bin/bash
    # Program:
    # Let user input one, two, three and show in screen.
    # History:
    # 2005/08/29 VBird First release
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    echo "This program will print your selection !"
    # read -p "Input your choice: " choice
    # case $choice in
    case $1 in
    "one")
    echo "Your choice is ONE"
    ;;
    "two")
    echo "Your choice is TWO"
    ;;
    "three")
    echo "Your choice is THREE"
    ;;
    *)
    echo "Usage {one|two|three}"
    ;;
    esac
    ´Ëʱ£¬Äú¿ÉÒÔʹÓẠsh sh11.sh two ¡»µÄ·½Ê½À´Ï´ïÖ¸Á¾Í¿ÉÒÔÊÕµ½Ïà¶ÔÓ¦µÄÏìÓ¦ÁË¡£ ÉÏÃæʹÓõÄÊÇÖ±½ÓÏ´ïµÄ·½Ê½£¬¶øÈç¹ûʹÓõÄÊǽ»»¥Ê½Ê±£¬ÄÇô½«ÉÏÃæµÚ 10, 11 ÐÐµÄ "#" Äõô£¬ ²¢½« 12 ÐмÓÉÏÅú×¢ (#)£¬¾Í¿ÉÒÔÈÃʹÓÃÕßÊäÈë²ÎÊý?¡«ÕâÑùÊÇ·ñºÜÓÐȤ°¡£¿£¡
    --------------------------------------------------------------------------------
    ÀûÓà function ¹¦ÄÜ
    ʲôÊÇ¡ºº¯Êý (function)¡»¹¦ÄÜ°¡£¿¼òµ¥µÄ˵£¬Æäʵ£¬ º¯Êý¿ÉÒÔÔÚ shell script µ±ÖÐ×ö³öÒ»¸öÀàËÆ×Ô¶©Ö´ÐÐÖ¸ÁîµÄ¶«Î÷£¬×î´óµÄ¹¦ÄÜÊÇ£¬ ¿ÉÒÔ¼ò»¯ÎÒÃǺܶàµÄ³ÌÐò´úÂë¡«¾ÙÀýÀ´Ëµ£¬ÉÏÃæµÄ sh11.sh µ±ÖУ¬Ã¿¸öÊäÈë½á¹û one, two, three ÆäʵÊä³öµÄÄÚÈݶ¼Ò»Ñù°¡¡«ÄÇôÎҾͿÉÒÔʹÓà function À´¼ò»¯ÁË£¡ function µÄÓï·¨ÊÇÕâÑùµÄ£º
    function fname() {
    ³ÌÐò¶Î
    }
    ÄǸö fname ¾ÍÊÇÎÒÃǵÄ×Ô¶©µÄÖ´ÐÐÖ¸ÁîÃû³Æ¡«¶ø³ÌÐò¶Î¾ÍÊÇÎÒÃÇÒªËûÖ´ÐеÄÄÚÈÝÁË¡£ ҪעÒâµÄÊÇ£¬ÔÚ shell script µ±ÖУ¬ function µÄÉ趨һ¶¨ÒªÔÚ³ÌÐòµÄ×îÇ°Ã棬 ÕâÑù²ÅÄܹ»ÔÚÖ´ÐÐʱ±»ÕÒµ½¿ÉÓõijÌÐò¶Îร¡ºÃ¡«ÎÒÃǽ« sh11.sh ¸ÄдһÏ£º
    [root@linux scripts]# vi sh11-2.sh
    #!/bin/bash
    # Program:
    # Let user input one, two, three and show in screen.
    # History:
    # 2005/08/29 VBird First release
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    function printit(){
    echo -n "Your choice is "
    }
    echo "This program will print your selection !"
    case $1 in
    "one")
    printit; echo $1 | tr 'a-z' 'A-Z'
    ;;
    "two")
    printit; echo $1 | tr 'a-z' 'A-Z'
    ;;
    "three")
    printit; echo $1 | tr 'a-z' 'A-Z'
    ;;
    *)
    echo "Usage {one|two|three}"
    ;;
    esac
    ÒÔÉÏÃæµÄÀý×ÓÀ´Ëµ£¬ÎÒ×öÁËÒ»¸öº¯ÊýÃû³ÆΪ printif £¬ËùÒÔ£¬µ±ÎÒÔÚºóÐøµÄ³ÌÐò¶ÎÀïÃ棬 Ö»ÒªÖ´ÐÐ printit µÄ»°£¬¾Í±íʾÎÒµÄ shell script ҪȥִÐСº function printit .... ¡» ÀïÃæµÄÄǼ¸¸ö³ÌÐò¶ÎÂä?£¡ µ±È»?£¬ÉÏÃæÕâ¸öÀý×Ó¾ÙµÃÌ«¼òµ¥ÁË£¬ËùÒÔÄú²»»á¾õµÃ function ÓÐʲôºÃÀ÷º¦µÄ£¬ ²»¹ý£¬Èç¹ûijЩ³ÌÐò´úÂëÒ»ÔÙµØÔÚ script µ±ÖÐÖظ´Ê±£¬Õâ¸ö function ¿É¾ÍÖØÒªµÄ¶à?¡« ²»µ«¿ÉÒÔ¼ò»¯³ÌÐò´úÂ룬¶øÇÒ¿ÉÒÔ×ö³ÉÀàËÆ¡ºÄ£¿é¡»µÄÍæÒâ¶ù£¬ÕæµÄºÜ°ôÀ²£¡
    ÁíÍ⣬ function Ò²ÊÇÓµÓÐÄÚ½¨±äÁ¿µÄ¡«ËûµÄÄÚ½¨±äÁ¿Óë shell script ºÜÀàËÆ£¬ º¯ÊýÃû³Æ´ú±íʾ $0 £¬¶øºóÐø½ÓµÄ±äÁ¿Ò²ÊÇÒÔ $1, $2... À´È¡´úµÄ¡« ÕâÀïºÜÈÝÒ׸ã´íม«ÒòΪ¡º function fname() { ³ÌÐò¶Î } ¡»ÄÚµÄ $0, $1... µÈµÈÓë shell script µÄ $0 ÊDz»Í¬µÄ¡£ÒÔÉÏÃæ sh11-2.sh À´Ëµ£¬¼ÙÈçÎÒÏ´¡º sh sh11-2.sh one ¡» Õâ±íʾÔÚ shell script ÄÚµÄ $1 Ϊ "one" Õâ¸ö×Ö·û´®¡£µ«ÊÇÔÚ printit() ÄÚµÄ $1 ÔòÓëÕâ¸ö one Î޹ء£ ÎÒÃǽ«ÉÏÃæµÄÀý×ÓÔٴεĸÄдһÏ£¬ÈÃÄú¸üÇå³þ£¡
    [root@linux scripts]# vi sh11-3.sh
    #!/bin/bash
    # Program:
    # Let user input one, two, three and show in screen.
    # History:
    # 2005/08/29 VBird First release
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    function printit(){
    echo "Your choice is $1"
    }
    echo "This program will print your selection !"
    case $1 in
    "one")
    printit 1
    ;;
    "two")
    printit 2
    ;;
    "three")
    printit 3
    ;;
    *)
    echo "Usage {one|two|three}"
    ;;
    esac
    ÔÚÉÏÃæµÄÀý×Óµ±ÖУ¬Èç¹ûÄúÊäÈ롺 sh sh11-3.sh one ¡»¾Í»á³öÏÖ¡º Your choice is 1 ¡»µÄ×ÖÑù¡« ΪʲôÊÇ 1 ÄØ£¿ÒòΪÔÚ³ÌÐò¶ÎÂäµ±ÖУ¬ÎÒÃÇÊÇдÁË¡º printit 1 ¡»ÄǸö 1 ¾Í»á³ÉΪ function µ±ÖÐµÄ $1 ม« ÕâÑùÊÇ·ñÀí½âÄØ£¿ function ±¾ÉíÆäʵ±È½ÏÀ§ÄÑÒ»µã£¬Èç¹ûÄú»¹ÏëÒª½øÐÐÆäËüµÄ׫дµÄ»°¡£ ²»¹ý£¬ÎÒÃǽöÊÇÏëÒª¸ü¼ÓÁ˽â shell script ¶øÒÑ£¬ËùÒÔ£¬ÕâÀï¿´¿´¼´¿É¡«Á˽âÔ­Àí¾ÍºÃ?¡« ^_^
    --------------------------------------------------------------------------------
    Ñ­»· (loop)
    ³ýÁË if...then...fi ÕâÖÖÌõ¼þÅжÏʽ֮Í⣬ѭ»·¿ÉÄÜÊdzÌÐòµ±ÖÐ×îÖØÒªµÄÒ»»·ÁË¡« Ñ­»·¿ÉÒÔ²»¶ÏµÄÖ´ÐÐij¸ö³ÌÐò¶ÎÂ䣬ֱµ½Ê¹ÓÃÕßÉ趨µÄÌõ¼þ´ï³ÉΪֹ¡£ ËùÒÔ£¬ÖصãÊÇÄǸö¡ºÌõ¼þµÄ´ï³É¡»ÊÇʲô¡£µ×ÏÂÎÒÃǾÍÀ´Ì¸Ò»Ì¸£º
    --------------------------------------------------------------------------------
    while do done, until do done
    Ò»°ãÀ´Ëµ£¬Ñ­»·×î³£¼ûµÄ¾ÍÊǵ×ÏÂÕâÁ½ÖÖ״̬ÁË£º
    while [ condition ]
    do
    ³ÌÐò¶ÎÂä
    done
    ÕâÖÖ·½Ê½ÖУ¬ while ÊÇ¡ºµ±....ʱ¡»£¬ËùÒÔ£¬ÕâÖÖ·½Ê½ËµµÄÊÇ¡ºµ± condition Ìõ¼þ³ÉÁ¢Ê±£¬¾Í½øÐÐÑ­»·£¬Ö±µ½ condition µÄÌõ¼þ²»³ÉÁ¢²ÅÍ£Ö¹¡»µÄÒâ˼¡£
    until [ condition ]
    do
    ³ÌÐò¶ÎÂä
    done
    ÕâÖÖ·½Ê½Ç¡Ç¡Óë while Ïà·´£¬Ëü˵µÄÊÇ¡ºµ± condition Ìõ¼þ³ÉÁ¢Ê±£¬¾ÍÖÕֹѭ»·£¬ ·ñÔò¾Í³ÖÐø½øÐÐÑ­»·µÄ³ÌÐò¶Î¡£¡»ÊÇ·ñ¸ÕºÃÏà·´°¡¡«ÎÒÃÇÒÔ while À´×ö¸ö¼òµ¥µÄÁ·Ï°ºÃÁË¡£ ¼ÙÉèÎÒÒªÈÃʹÓÃÕßÊäÈë yes »òÕßÊÇ YES ²Å½áÊø³ÌÐòµÄÖ´ÐУ¬·ñÔò¾ÍÒ»Ö±½øÐиæ֪ʹÓÃÕßÊäÈë×Ö·û´®¡£
    [root@linux scripts]# vi sh12.sh
    #!/bin/bash
    # Program:
    # Use loop to try find your input.
    # History:
    # 2005/08/29 VBird First release
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    while [ "$yn" != "yes" ] && [ "$yn" != "YES" ]
    do
    read -p "Please input yes/YES to stop this program: " yn
    done
    ÉÏÃæÕâ¸öÀýÌâµÄ˵Ã÷ÊÇ¡ºµ± $yn Õâ¸ö±äÁ¿²»ÊÇ "yes" ÇÒ $yn Ò²²»ÊÇ "YES" ʱ£¬²Å½øÐÐÑ­»·ÄڵijÌÐò¡£¡» ¶øÈç¹û $yn ÊÇ "yes" »ò "YES" ʱ£¬¾Í»áÀ뿪ѭ»·?¡«ÄÇÈç¹ûʹÓà until ÄØ£¿ºÇºÇÓÐȤ?¡« ËûµÄÌõ¼þ»á±ä³ÉÕâÑù£º
    [root@linux scripts]# vi sh12-2.sh
    #!/bin/bash
    # Program:
    # Use loop to try find your input.
    # History:
    # 2005/08/29 VBird First release
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    until [ "$yn" == "yes" ] || [ "$yn" == "YES" ]
    do
    read -p "Please input yes/YES to stop this program: " yn
    done
    ×Ðϸ±È¶ÔÒ»ÏÂÕâÁ½¸ö¶«Î÷ÓÐɶ²»Í¬à¸£¡ ^_^ÔÙÀ´£¬Èç¹ûÎÒÏëÒª¼ÆËã 1+2+3+....+100 Õâ¸öÊý¾ÝÄØ£¿ ÀûÓÃÑ­»·°¡¡«ËûÊÇÕâÑùµÄ£º
    [root@linux scripts]# vi sh13.sh
    #!/bin/bash
    # Program:
    # Try to use loop to calculate the result "1+2+3...+100"
    # History:
    # 2005/08/29 VBird First release
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    s=0
    i=0
    while [ "$i" != "100" ]
    do
    i=$(($i+1))
    s=$(($s+$i))
    done
    echo "The result of '1+2+3+...+100' is ==>; $s"
    ºÙºÙ£¡µ±ÄúÖ´ÐÐÁË¡º sh sh13.sh ¡»Ö®ºó£¬¾Í¿ÉÒԵõ½ 5050 Õâ¸öÊý¾Ý²Å¶Ô°¡£¡ÕâÑù?ºô¡« ÄÇôÈÃÄú×ÔÐÐ×öһϣ¬Èç¹ûÏëÒªÈÃʹÓÃÕß×ÔÐÐÊäÈëÒ»¸öÊý×Ö£¬ÈóÌÐòÓÉ 1+2+... Ö±µ½ÄúÊäÈëµÄÊý×ÖΪֹ£¬ ¸ÃÈçºÎ׫дÄØ£¿Ó¦¸ÃºÜ¼òµ¥°É£¿£¡´ð°¸¿ÉÒԲο¼Ò»ÏÂÏ°ÌâÁ·Ï°ÀïÃæµÄÒ»Ìâร¡
    --------------------------------------------------------------------------------
    for...do....done
    Ïà¶ÔÓÚ while, until µÄÑ­»··½Ê½ÊDZØÐëÒª¡º·ûºÏij¸öÌõ¼þ¡»µÄ״̬£¬ for ÕâÖÖÓï·¨£¬ÔòÊÇ¡º ÒѾ­ÖªµÀÒª½øÐм¸´ÎÑ­»·¡»µÄ״̬£¡ËûµÄÓï·¨ÊÇ£º
    for (( ³õʼֵ; ÏÞÖÆÖµ; Ö´Ðв½½× ))
    do
    ³ÌÐò¶Î
    done
    ÕâÖÖÓï·¨ÊʺÏÓÚÊýÖµ·½Ê½µÄÔËËãµ±ÖУ¬ÔÚ for ºóÃæµÄÀ¨ºÅÄÚµÄÈý´®ÄÚÈÝÒâÒåΪ£º
    ³õʼֵ£ºÄ³¸ö±äÁ¿ÔÚÑ­»·µ±ÖеÄÆðʼֵ£¬Ö±½ÓÒÔÀàËÆ i=1 É趨ºÃ£»
    ÏÞÖÆÖµ£ºµ±±äÁ¿µÄÖµÔÚÕâ¸öÏÞÖÆÖµµÄ·¶Î§ÄÚ£¬¾Í¼ÌÐø½øÐÐÑ­»·¡£ÀýÈç i<=100£»
    Ö´Ðв½½×£ºÃ¿×÷Ò»´ÎÑ­»·Ê±£¬±äÁ¿µÄ±ä»¯Á¿¡£ÀýÈç i=i+1¡£
    ÖµµÃ×¢ÒâµÄÊÇ£¬ÔÚ¡ºÖ´Ðв½½×¡»µÄÉ趨ÉÏ£¬Èç¹ûÿ´ÎÔö¼Ó 1 £¬Ôò¿ÉÒÔʹÓÃÀàËÆ¡ºi++¡»µÄ·½Ê½£¬Ò༴ÊÇ i ÿ´ÎÑ­»·¶¼»áÔö¼ÓÒ»µÄÒâ˼¡£ºÃ£¬ÎÒÃÇÒÔÕâÖÖ·½Ê½À´½øÐÐ 1 ÀÛ¼Óµ½ 100 µÄÑ­»·°É£¡
    [root@linux scripts]# vi sh14.sh
    #!/bin/bash
    # Program:
    # Try do calculate 1+2+....+100
    # History:
    # 2005/08/29 VBird First release
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    s=0
    for (( i=1; i<=100; i=i+1 ))
    do
    s=$(($s+$i))
    done
    echo "The result of '1+2+3+...+100' is ==>; $s"
    Ò»ÑùÒ²ÊǺܼòµ¥°É£¡ÀûÓÃÕâ¸ö for Ôò¿ÉÒÔÖ±½ÓÏÞÖÆÑ­»·Òª½øÐм¸´ÎÄØ£¡ÕâôºÃÓõĶ«Î÷ÄѵÀÖ»ÄÜÔÚÊýÖµ·½Ã涯×÷£¿ µ±È»²»ÊÇÀ²¡«ÎÒÃÇ»¹¿ÉÒÔÀûÓõ×ϵķ½Ê½À´½øÐзÇÊý×Ö·½ÃæµÄÑ­»·ÔË×÷ร¡
    for $var in con1 con2 con3 ...
    do
    ³ÌÐò¶Î
    done
    ÒÔÉÏÃæµÄÀý×ÓÀ´Ëµ£¬Õâ¸ö $var µÄ±äÁ¿ÄÚÈÝÔÚÑ­»·¹¤×÷ʱ£º
    µÚÒ»´ÎÑ­»·Ê±£¬ $var µÄÄÚÈÝΪ con1 £»
    µÚ¶þ´ÎÑ­»·Ê±£¬ $var µÄÄÚÈÝΪ con2 £»
    µÚÈý´ÎÑ­»·Ê±£¬ $var µÄÄÚÈÝΪ con3 £»
    ....
    ÎÒÃÇ¿ÉÒÔ×ö¸ö¼òµ¥µÄÁ·Ï°¡£¼ÙÉèÎÒÓÐÈýÖÖ¶¯Î·Ö±ðÊÇ dog, cat, elephant ÈýÖÖ£¬ ÎÒÏëÿһÐж¼Êä³öÕâÑù£º¡ºThere are dogs...¡»Ö®ÀàµÄ×ÖÑù£¬Ôò¿ÉÒÔ£º
    [root@linux scripts]# vi sh15.sh
    #!/bin/bash
    # Program:
    # Using for .... loop to print 3 animal
    # History:
    # 2005/08/29 VBird First release
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    for animal in dog cat elephant
    do
    echo "There are ""$animal""s.... "
    done
    ºÜ¼òµ¥ÊÇ°É£¡ ^_^¡£ºÃÁË£¬ÄÇôÈç¹ûÎÒÏëÒªÈÃʹÓÃÕßÊäÈëij¸öĿ¼£¬ È»ºóÎÒÕÒ³öijĿ¼ÄÚµÄÎļþÃûµÄȨÏÞÄØ£¿ÓÖ¸ÃÈçºÎÊǺ㿺Ǻǣ¡¿ÉÒÔÕâÑù×öÀ²¡«
    [root@linux scripts]# vi sh16.sh
    #!/bin/bash
    # Program:
    # let user input a directory and find the whole file's permission.
    # History:
    # 2005/08/29 VBird First release
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    # 1. ÏÈ¿´¿´Õâ¸öĿ¼ÊÇ·ñ´æÔÚ°¡£¿
    read -p "Please input a directory: " dir
    if [ "$dir" == "" ] || [ ! -d "$dir" ]; then
    echo "The $dir is NOT exist in your system."
    exit 1
    fi
    # 2. ¿ªÊ¼²âÊÔµµ°¸?¡«
    filelist=`ls $dir`
    for filename in $filelist
    do
    perm=""
    test -r "$dir/$filename" && perm="$perm readable"
    test -w "$dir/$filename" && perm="$perm writable"
    test -x "$dir/$filename" && perm="$perm executable"
    echo "The file $dir/$filename's permission is $perm "
    done
    ºÇºÇ£¡ºÜÓÐȤµÄÀý×Ó°É¡«ÀûÓÃÕâÖÖ·½Ê½£¬Äú¿ÉÒÔºÜÇáÒ×µÄÀ´´¦ÀíһЩµµ°¸µÄÌØÐÔÄØ¡« ÎÒÃÇÑ­»·¾Í½éÉܵ½ÕâÀïÁË¡«ÆäËü¸ü¶àµÄÓ¦Ó㬾͵ÃÊÓÄúµÄÐèÇóÀ´Íæ?¡«¡£
    --------------------------------------------------------------------------------
    shell script µÄ×·×ÙÓë debug
    scripts ÔÚÖ´ÐÐ֮ǰ£¬×îŵľÍÊdzöÏÖÎÊÌâÁË£¡ÄÇôÎÒÃÇÈçºÎ debug ÄØ£¿ÓÐûÓа취²»ÐèҪ͸¹ýÖ±½ÓÖ´Ðиà scripts ¾Í¿ÉÒÔÀ´ÅжÏÊÇ·ñÓÐÎÊÌâÄØ£¡£¿ºÇºÇ£¡ µ±È»ÊÇÓеģ¡ÎÒÃǾÍÖ±½ÓÒÔ bash µÄÏà¹Ø²ÎÊýÀ´½øÐÐÅжϰɣ¡
    [root@linux ~]# sh [-nvx] scripts.sh
    ²ÎÊý£º
    -n £º²»ÒªÖ´ÐÐ script£¬½ö²éѯÓï·¨µÄÎÊÌ⣻
    -v £ºÔÙÖ´ÐÐ sccript Ç°£¬ÏȽ« scripts µÄÄÚÈÝÊä³öµ½ÆÁÄ»ÉÏ£»
    -x £º½«Ê¹Óõ½µÄ script ÄÚÈÝÏÔʾµ½ÆÁÄ»ÉÏ£¬ÕâÊǺÜÓÐÓõIJÎÊý£¡
    ·¶Àý£º
    ·¶ÀýÒ»£º²âÊÔ sh16.sh ÓÐÎÞÓï·¨µÄÎÊÌ⣿
    [root@linux ~]# sh -n sh16.sh
    # ÈôÓ﷨ûÓÐÎÊÌ⣬Ôò²»»áÏÔʾÈκÎÐÅÏ¢£¡
    ·¶Àý¶þ£º½« sh15.sh µÄÖ´Ðйý³ÌÈ«²¿ÁгöÀ´¡«
    [root@linux ~]# sh -x sh15.sh
    + PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/home/vbird/bin
    + export PATH
    + for animal in dog cat elephant
    + echo 'There are dogs.... '
    There are dogs....
    + for animal in dog cat elephant
    + echo 'There are cats.... '
    There are cats....
    + for animal in dog cat elephant
    + echo 'There are elephants.... '
    There are elephants....
    # ʹÓà -x ÕæµÄÊÇ×·×Ù script µÄºÃ·½·¨£¬Ëû¿ÉÒÔ½«ËùÓÐÓÐÖ´ÐеijÌÐò¶ÎÔÚÖ´ÐÐÇ°ÁгöÀ´£¬
    # Èç¹ûÊdzÌÐò¶ÎÂ䣬ÔòÊä³öʱ£¬×îÇ°Ãæ»á¼ÓÉÏ + ×ֺţ¬±íʾËûÊdzÌÐò´úÂë¶øÒÑ£¬
    # ʵ¼ÊµÄÊä³öÔòÓë standard output Óйذ¡¡«ÈçÉÏËùʾ¡£
    ÔÚÉÏÃæµÄ·¶Àý¶þµ±ÖУ¬ÎÒÃÇ¿ÉÒÔ͸¹ýÕâ¸ö¼òµ¥µÄ²ÎÊý -x À´´ï³É debug µÄÄ¿µÄ£¬Õâ¿ÉÊÇÒ»¸ö²»¿É¶àµÃµÄ²ÎÊý£¬ ͨ³£Èç¹ûÄúÖ´ÐÐ script È´·¢ÉúÎÊÌâʱ£¬ÀûÓÃÕâ¸ö -x ²ÎÊý£¬¾Í¿ÉÒÔÖªµÀÎÊÌâÊÇ·¢ÉúÔÚÄÄÒ»ÐÐÉÏÃæÁË£¡
    ÊìϤ sh µÄÓ÷¨£¬½«¿ÉÒÔʹÄúÔÚ¹ÜÀí Linux µÄ¹ý³ÌÖеÃÐÄÓ¦ÊÖ£¡ÖÁÓÚÔÚ Shell scripts µÄѧϰ·½·¨ÉÏÃ棬ÐèÒª¡º¶à¿´¡¢¶àÄ£·Â¡¢²¢¼ÓÒÔÐ޸ijÉ×Ô¼ºµÄÑùʽ£¡¡» ÊÇ×î¿ìµÄѧϰÊÖ¶ÎÁË£¡ÍøÂçÉÏÓÐÏ൱¶àµÄÅóÓÑÔÚ¿ª·¢Ò»Ð©Ï൱ÓÐÓÃµÄ scripts £¬ÈôÊÇÄú¿ÉÒÔ½«¶Ô·½µÄ scripts ÄÃÀ´£¬²¢ÇҸijÉÊʺÏ×Ô¼ºÖ÷»úµÄÑù×Ó£¡ÄÇôѧϰµÄЧ¹û»áÊÇ×î¿ìµÄÄØ£¡
    ÁíÍ⣬ÎÒÃÇ Linux ϵͳ±¾À´¾ÍÓкܶàµÄÆô¶¯ script £¬Èç¹ûÄúÏëÒªÖªµÀÿ¸ö script Ëù´ú±íµÄ¹¦ÄÜÊÇʲô£¿ ¿ÉÒÔÖ±½ÓÒÔ vi ½øÈë¸Ã script È¥²éÔÄһϣ¬Í¨³£Á¢¿Ì¾ÍÖªµÀ¸Ã script µÄÄ¿µÄÁË¡£ ¾ÙÀýÀ´Ëµ£¬ÎÒÃÇµÄ Linux ÀïÍ·ÓиöÎļþÃû³ÆΪ£º /etc/init.d/portmap £¬Õâ¸ö script ÊǸÉÂïÓõģ¿ ÀûÓà vi È¥²éÔÄ×îÇ°ÃæµÄ¼¸ÐÐ×Ö£¬Ëû³öÏÖÈçÏÂÐÅÏ¢£º
    # description: The portmapper manages RPC connections, which are used by
    # protocols such as NFS and NIS. The portmap server must be
    # running on machines which act as servers for protocols which
    # make use of the RPC mechanism.
    # processname: portmap
    --------------------------------------------------------------------------------
    ±¾ÕÂÏ°ÌâÁ·Ï°
    ( Òª¿´´ð°¸Ç뽫Êó±êÒƶ¯µ½¡º´ð£º¡»µ×ϵĿհ״¦£¬°´ÏÂ×ó¼üȦѡ¿Õ°×´¦¼´¿É²ì¿´ )
    Ç뽨Á¢Ò»Ö§ script £¬µ±ÄãÖ´Ðиà script µÄʱºò£¬¸Ã script ¿ÉÒÔÏÔʾ£º 1. ÄãÄ¿Ç°µÄÉí·Ý (Óà whoami ) 2. ÄãÄ¿Ç°ËùÔÚµÄĿ¼ (Óà pwd)
    #!/bin/bash
    echo -e "Your name is ==>; `whoami`"
    echo -e "The current directory is ==>; `pwd`"
    Çë×ÔÐн¨Á¢Ò»Ö§³ÌÐò£¬¸Ã³ÌÐò¿ÉÒÔÓÃÀ´¼ÆË㡺Äú»¹Óм¸Ìì¿ÉÒÔ¹ýÉúÈÕ¡»°¡£¿£¿
    #!/bin/bash
    read -p "Pleas input your birthday (MMDD, ex>; 0709): " bir
    now=`date +%m%d`
    if [ "$bir" == "$now" ]; then
    echo "Happy Birthday to you!!!"
    elif [ "$bir" -gt "$now" ]; then
    year=`date +%Y`
    total_d=$(($((`date --date="$year$bir" +%s`-`date +%s`))/60/60/24))
    echo "Your birthday will be $total_d later"
    else
    year=$((`date +%Y`+1))
    total_d=$(($((`date --date="$year$bir" +%s`-`date +%s`))/60/60/24))
    echo "Your birthday will be $total_d later"
    fi
    ÈÃʹÓÃÕßÊäÈëÒ»¸öÊý×Ö£¬³ÌÐò¿ÉÒÔÓÉ 1+2+3... Ò»Ö±ÀÛ¼Óµ½Ê¹ÓÃÕßÊäÈëµÄÊý×ÖΪֹ¡£
    #!/bin/bash
    read -p "Please input an integer number: " number
    i=0
    s=0
    while [ "$i" != "$number" ]
    do
    i=$(($i+1))
    s=$(($s+$i))
    done
    echo "the result of '1+2+3+...$number' is ==>; $s"
    ׫дһ֧³ÌÐò£¬ËûµÄ×÷ÓÃÊÇ: 1.) ÏȲ鿴һÏ /root/test/logical Õâ¸öÃû³ÆÊÇ·ñ´æÔÚ£» 2.) Èô²»´æÔÚ£¬Ôò½¨Á¢Ò»¸öµµ°¸£¬Ê¹Óà touch À´½¨Á¢£¬½¨Á¢Íê³ÉºóÀ뿪£» 3.) Èç¹û´æÔڵĻ°£¬ÅжϸÃÃû³ÆÊÇ·ñΪµµ°¸£¬ÈôΪµµ°¸Ôò½«Ö®É¾³ýºó½¨Á¢Ò»¸öµµ°¸£¬µµÃûΪ logical £¬Ö®ºóÀ뿪£» 4.) Èç¹û´æÔڵĻ°£¬¶øÇÒ¸ÃÃû³ÆΪĿ¼£¬ÔòÒƳý´ËĿ¼£¡
    #!/bin/bash
    if [ ! -e logical ]; then
    touch logical
    echo "Just make a file logical"
    exit 1
    elif [ -e logical ] && [ -f logical ]; then
    rm logical
    mkdir logical
    echo "remove file ==>; logical"
    echo "and make directory logical"
    exit 1
    elif [ -e logical ] && [ -d logical ]; then
    rm -rf logical
    echo "remove directory ==>; logical"
    exit 1
    else
    echo "Does here have anything?"
    fi
    ÎÒÃÇÖªµÀ /etc/passwd ÀïÃæÒÔ : À´·Ö¸ô£¬µÚÒ»À¸ÎªÕ˺ÅÃû³Æ¡£Çëдһֻ³ÌÐò£¬¿ÉÒÔ½« /etc/passwd µÄµÚÒ»À¸È¡³ö£¬¶øÇÒÿһÀ¸¶¼ÒÔÒ»ÐÐ×Ö·û´®¡ºThe 1 account is "root" ¡»À´ÏÔʾ£¬ÄǸö 1 ±íʾÐÐÊý¡£
    #!/bin/bash
    accounts=`cat /etc/passwd | cut -d':' -f1`
    for account in $accounts
    do
    declare -i i=$i+1
    echo "The $i account is "$account" "
    done

  2. MOCEL ÓÚ 2006-07-30 11:56:52·¢±í:

    Â¥Ö÷ÐÁ¿àÁË