µ±Ç°Î»ÖãºLinux½Ì³Ì - Linux×ÊѶ - ¸ü¶àµÄ bash »ù±¾±à³Ì

¸ü¶àµÄ bash »ù±¾±à³Ì

    ÔÚǰһƪ bash µÄ½éÉÜÐÔÎÄÕÂÖУ¬Daniel Robbins ΪÄú½²½âÁ˽ű¾ÓïÑÔµÄһЩ»ù±¾ÔªËغÍʹÓà bash µÄÔ­Òò¡£ÔÚ±¾ÎÄ£¨¼´µÚ¶þ²¿·Ö£©ÖУ¬Daniel ¼ÌÐøǰһƪµÄÄÚÈÝ£¬²¢½²½âÌõ¼þ (if-then) Óï¾ä¡¢Ñ­»·ºÍ¸ü¶àµÄ bash »ù±¾½á¹¹¡£

    ÎÒÃÇÏÈ¿´Ò»Ï´¦ÀíÃüÁîÐÐ×Ô±äÁ¿µÄ¼òµ¥¼¼ÇÉ£¬È»ºóÔÙ¿´¿´ bash »ù±¾±à³Ì½á¹¹¡£

½ÓÊÕ×Ô±äÁ¿

    ÔÚ ½éÉÜÐÔÎÄÕ ÖеÄÑù±¾³ÌÐòÖУ¬ÎÒÃÇʹÓû·¾³±äÁ¿ "$1" À´ÒýÓõÚÒ»¸öÃüÁîÐÐ×Ô±äÁ¿¡£ÀàËƵأ¬¿ÉÒÔʹÓà "$2"¡¢"$3" µÈÀ´ÒýÓô«µÝ¸ø½Å±¾µÄµÚ¶þºÍµÚÈý¸ö×Ô±äÁ¿¡£ÕâÀïÓÐÒ»¸öÀý×Ó£º

#!/usr/bin/env bash echo name of script is $0 echo first argument is $1 echo second argument is $2 echo seventeenth argument is $17 echo number of arguments is $#

    ³ýÒÔÏÂÁ½¸öϸ½ÚÖ®Í⣬´ËÀýÎÞÐè˵Ã÷¡£µÚÒ»£¬"$0" ½«À©Õ¹³É´ÓÃüÁîÐе÷ÓõĽű¾Ãû³Æ£¬"$#" ½«À©Õ¹³É´«µÝ¸ø½Å±¾µÄ×Ô±äÁ¿ÊýÄ¿¡£ÊÔÑéÒÔÉϽű¾£¬Í¨¹ý´«µÝ²»Í¬ÀàÐ͵ÄÃüÁîÐÐ×Ô±äÁ¿À´Á˽âÆ乤×÷Ô­Àí¡£

    ÓÐʱÐèÒªÒ»´ÎÒýÓà ËùÓÐ ÃüÁîÐÐ×Ô±äÁ¿¡£Õë¶ÔÕâÖÖÓÃ;£¬bash ʵÏÖÁ˱äÁ¿ "$@"£¬ËüÀ©Õ¹³ÉËùÓÐÓÿոñ·Ö¿ªµÄÃüÁîÐвÎÊý¡£ÔÚ±¾ÎÄÉÔºóµÄ "for" Ñ­»·²¿·ÖÖУ¬Äú½«¿´µ½Ê¹ÓøñäÁ¿µÄÀý×Ó¡£

Bash ±à³Ì½á¹¹

    Èç¹ûÄúÔøÓùýÈç C¡¢Pascal¡¢Python »ò Perl ÄÇÑùµÄ¹ý³ÌÓïÑÔ±à³Ì£¬ÔòÒ»¶¨ÊìϤ "if" Óï¾äºÍ "for" Ñ­»·ÄÇÑùµÄ±ê×¼±à³Ì½á¹¹¡£¶ÔÓÚÕâЩ±ê×¼½á¹¹µÄ´ó¶àÊý£¬Bash ÓÐ×Ô¼ºµÄ°æ±¾¡£ÔÚϼ¸½ÚÖУ¬½«½éÉܼ¸ÖÖ bash ½á¹¹£¬²¢ÑÝʾÕâЩ½á¹¹ºÍÄúÒѾ­ÊìϤµÄÆäËü±à³ÌÓïÑÔÖнṹµÄ²îÒì¡£Èç¹ûÒÔÇ°±à³Ì²»¶à£¬Ò²²»±Øµ£ÐÄ¡£ÎÒÌṩÁË×ã¹»µÄÐÅÏ¢ºÍʾÀý£¬Ê¹Äú¿ÉÒÔ¸úÉϱ¾ÎĵĽø¶È¡£

·½±ãµÄÌõ¼þÓï¾ä

    Èç¹ûÄúÔøÓà C ±àд¹ýÓëÎļþÏà¹ØµÄ´úÂ룬ÔòÓ¦¸ÃÖªµÀ£ºÒª±È½ÏÌض¨ÎļþÊÇ·ñ±ÈÁíÒ»¸öÎļþÐÂÐèÒª´óÁ¿¹¤×÷¡£ÄÇÊÇÒòΪ C ûÓÐÈκÎÄÚÖÃÓï·¨À´½øÐÐÕâÖֱȽϣ¬±ØÐëʹÓÃÁ½¸ö stat() µ÷ÓúÍÁ½¸ö stat ½á¹¹À´½øÐÐÊÖ¹¤±È½Ï¡£Ïà·´£¬bash ÄÚÖÃÁ˱ê×¼Îļþ±È½ÏÔËËã·û£¬Òò´Ë£¬È·¶¨¡°/tmp/myfile ÊÇ·ñ¿É¶Á¡±Óë²é¿´¡°$myvar ÊÇ·ñ´óÓÚ 4¡±Ò»ÑùÈÝÒס£

    ϱíÁгö×î³£ÓÃµÄ bash ±È½ÏÔËËã·û¡£Í¬Ê±»¹ÓÐÈçºÎÕýȷʹÓÃÿһѡÏîµÄʾÀý¡£Ê¾ÀýÒª¸úÔÚ "if" Ö®ºó¡£ÀýÈ磺

if [ -z "$myvar" ] then echo "myvar is not defined" fi

ÔËËã·û ÃèÊö ʾÀý Îļþ±È½ÏÔËËã·û -e filename Èç¹û filename´æÔÚ£¬ÔòΪÕæ [ -e /var/log/syslog ] -d filename Èç¹û filenameΪĿ¼£¬ÔòΪÕæ [ -d /tmp/mydir ] -f filename Èç¹û filenameΪ³£¹æÎļþ£¬ÔòΪÕæ [ -f /usr/bin/grep ] -L filename Èç¹û filenameΪ·ûºÅÁ´½Ó£¬ÔòΪÕæ [ -L /usr/bin/grep ] -r filename Èç¹û filename¿É¶Á£¬ÔòΪÕæ [ -r /var/log/syslog ] -w filename Èç¹û filename¿Éд£¬ÔòΪÕæ [ -w /var/mytmp.txt ] -x filename Èç¹û filename¿ÉÖ´ÐУ¬ÔòΪÕæ [ -L /usr/bin/grep ] filename1-nt filename2 Èç¹û filename1±È filename2У¬ÔòΪÕæ [ /tmp/install/etc/services -nt /etc/services ] filename1-ot filename2 Èç¹û filename1±È filename2¾É£¬ÔòΪÕæ [ /boot/bzImage -ot arch/i386/boot/bzImage ] ×Ö·û´®±È½ÏÔËËã·û £¨Çë×¢ÒâÒýºÅµÄʹÓã¬ÕâÊÇ·ÀÖ¹¿Õ¸ñÈÅÂÒ´úÂëµÄºÃ·½·¨£© -z string Èç¹û string³¤¶ÈΪÁ㣬ÔòΪÕæ [ -z "$myvar" ] -n string Èç¹û string³¤¶È·ÇÁ㣬ÔòΪÕæ [ -n "$myvar" ] string1= string2 Èç¹û string1Óë string2Ïàͬ£¬ÔòΪÕæ [ "$myvar" = "one two three" ] string1!= string2 Èç¹û string1Óë string2²»Í¬£¬ÔòΪÕæ [ "$myvar" != "one two three" ] ËãÊõ±È½ÏÔËËã·û num1-eq num2 µÈÓÚ [ 3 -eq $mynum ] num1-ne num2 ²»µÈÓÚ [ 3 -ne $mynum ] num1-lt num2 СÓÚ [ 3 -lt $mynum ] num1-le num2 СÓÚ»òµÈÓÚ [ 3 -le $mynum ] num1-gt num2 ´óÓÚ [ 3 -gt $mynum ] num1-ge num2 ´óÓÚ»òµÈÓÚ [ 3 -ge $mynum ]

ÓÐʱ£¬Óм¸ÖÖ²»Í¬·½·¨À´½øÐÐÌض¨±È½Ï¡£ÀýÈ磬ÒÔÏÂÁ½¸ö´úÂë¶ÎµÄ¹¦ÄÜÏàͬ£º

if [ "$myvar" -eq 3 ] then echo "myvar equals 3" fi if [ "$myvar" = "3" ] then echo "myvar equals 3" fi

    ÉÏÃæÁ½¸ö±È½ÏÖ´ÐÐÏàͬµÄ¹¦ÄÜ£¬µ«ÊǵÚÒ»¸öʹÓÃËãÊõ±È½ÏÔËËã·û£¬¶øµÚ¶þ¸öʹÓÃ×Ö·û´®±È½ÏÔËËã·û¡£

×Ö·û´®±È½Ï˵Ã÷

    ´ó¶àÊýʱºò£¬ËäÈ»¿ÉÒÔ²»Ê¹ÓÃÀ¨Æð×Ö·û´®ºÍ×Ö·û´®±äÁ¿µÄË«ÒýºÅ£¬µ«Õâ²¢²»ÊǺÃÖ÷Ò⡣ΪʲôÄØ£¿ÒòΪÈç¹û»·¾³±äÁ¿ÖÐÇ¡ÇÉÓÐÒ»¸ö¿Õ¸ñ»òÖƱí¼ü£¬bash ½«ÎÞ·¨·Ö±æ£¬´Ó¶øÎÞ·¨Õý³£¹¤×÷¡£ÕâÀïÓÐÒ»¸ö´íÎóµÄ±È½ÏʾÀý£º

if [ $myvar = "foo bar oni" ] then echo "yes" fi

    ÔÚÉÏÀýÖУ¬Èç¹û myvar µÈÓÚ "foo"£¬Ôò´úÂ뽫°´Ô¤Ï빤×÷£¬²»½øÐдòÓ¡¡£µ«ÊÇ£¬Èç¹û myvar µÈÓÚ "foo bar oni"£¬Ôò´úÂ뽫ÒòÒÔÏ´íÎóʧ°Ü£º

[: too many arguments

    ÔÚÕâÖÖÇé¿öÏ£¬"$myvar"£¨µÈÓÚ "foo bar oni"£©ÖеĿոñÃÔ»óÁË bash¡£bash À©Õ¹ "$myvar" Ö®ºó£¬´úÂëÈçÏ£º

[ foo bar oni = "foo bar oni" ]

    ÒòΪ»·¾³±äÁ¿Ã»·ÅÔÚË«ÒýºÅÖУ¬ËùÒÔ bash ÈÏΪ·½À¨ºÅÖеÄ×Ô±äÁ¿¹ý¶à¡£¿ÉÒÔÓÃË«ÒýºÅ½«×Ö·û´®×Ô±äÁ¿À¨ÆðÀ´Ïû³ý¸ÃÎÊÌâ¡£Çë¼Çס£¬Èç¹ûÑø³É½«ËùÓÐ×Ö·û´®×Ô±äÁ¿ÓÃË«ÒýºÅÀ¨ÆðµÄÏ°¹ß£¬½«³ýÈ¥ºÜ¶àÀàËƵıà³Ì´íÎó¡£"foo bar oni" ±È½Ï Ó¦¸Ãд³É£º

if [ "$myvar" = "foo bar oni" ] then echo "yes" fi ¸ü¶àÒýÓÃϸ½Ú

Èç¹ûÒªÀ©Õ¹»·¾³±äÁ¿£¬Ôò±ØÐ뽫ËüÃÇÓà ˫ÒýºÅ¡¢¶ø²»Êǵ¥ÒýºÅÀ¨Æð¡£µ¥ÒýºÅ ½ûÓà ±äÁ¿£¨ºÍÀúÊ·£©À©Õ¹¡£

    ÒÔÉÏ´úÂ뽫°´Ô¤Ï빤×÷£¬¶ø²»»áÓÐÈκÎÁîÈ˲»¿ìµÄÒâÍâ³öÏÖ¡£

Ñ­»·½á¹¹£º"for"

    ºÃÁË£¬ÒѾ­½²ÁËÌõ¼þÓï¾ä£¬ÏÂÃæ¸Ã̽Ë÷ bash Ñ­»·½á¹¹ÁË¡£ÎÒÃǽ«´Ó±ê×¼µÄ "for" Ñ­»·¿ªÊ¼¡£ÕâÀïÓÐÒ»¸ö¼òµ¥µÄÀý×Ó£º

#!/usr/bin/env bash for x in one two three four do echo number $x done Êä³ö£º number one number two number three number four

    ·¢ÉúÁËʲô£¿"for" Ñ­»·ÖÐµÄ "for x" ²¿·Ö¶¨ÒåÁËÒ»¸öÃûΪ "$x" µÄл·¾³±äÁ¿£¨Ò²³ÆΪѭ»·¿ØÖƱäÁ¿£©£¬ËüµÄÖµ±»ÒÀ´ÎÉèÖÃΪ "one"¡¢"two"¡¢"three" ºÍ "four"¡£Ã¿Ò»´Î¸³ÖµÖ®ºó£¬Ö´ÐÐÒ»´ÎÑ­»·Ì壨"do" ºÍ "done" Ö®¼äµÄ´úÂ룩¡£ÔÚÑ­»·ÌåÄÚ£¬ÏóÆäËü»·¾³±äÁ¿Ò»Ñù£¬Ê¹Óñê×¼µÄ±äÁ¿À©Õ¹Óï·¨À´ÒýÓÃÑ­»·¿ØÖƱäÁ¿ "$x"¡£»¹Òª×¢Ò⣬"for" Ñ­»·×ÜÊǽÓÊÕ "in" Óï¾äÖ®ºóµÄijÖÖÀàÐ͵Ä×ÖÁÐ±í¡£ÔÚ±¾ÀýÖУ¬Ö¸¶¨ÁËËĸöÓ¢Óïµ¥´Ê£¬µ«ÊÇ×ÖÁбíÒ²¿ÉÒÔÒýÓôÅÅÌÉϵÄÎļþ£¬ÉõÖÁÎļþͨÅä·û¡£¿´¿´ÏÂÃæµÄÀý×Ó£¬¸ÃÀýÑÝʾÈçºÎʹÓñê×¼ shell ͨÅä·û£º

#!/usr/bin/env bash for myfile in /etc/r* do if [ -d "$myfile" ] then echo "$myfile (dir)" else echo "$myfile" fi done Êä³ö£º /etc/rc.d (dir) /etc/resolv.conf /etc/resolv.conf~ /etc/rpc

    ÒÔÉÏ´úÂëÁгöÔÚ /etc ÖÐÿ¸öÒÔ "r" ¿ªÍ·µÄÎļþ¡£Òª×öµ½Õâµã£¬bash ÔÚÖ´ÐÐÑ­»·Ö®Ç°Ê×ÏÈÈ¡µÃͨÅä·û /etc/r*£¬È»ºóÀ©Õ¹Ëü£¬ÓÃ×Ö·û´® /etc/rc.d /etc/resolv.conf /etc/resolv.conf~ /etc/rpc Ìæ»»¡£Ò»µ©½øÈëÑ­»·£¬¸ù¾Ý myfile ÊÇ·ñΪĿ¼£¬"-d" Ìõ¼þÔËËã·ûÓÃÀ´Ö´ÐÐÁ½¸ö²»Í¬²Ù×÷¡£Èç¹ûÊÇĿ¼£¬Ôò½« "(dir)" ¸½¼Óµ½Êä³öÐС£

»¹¿ÉÒÔÔÚ×ÖÁбíÖÐʹÓöà¸öͨÅä·û¡¢ÉõÖÁÊÇ»·¾³±äÁ¿£º

for x in /etc/r--? /var/lo* /home/drobbins/mystuff/* /tmp/${MYPATH}/* do cp $x /mnt/mydir done

Bash ½«ÔÚËùÓÐÕýȷλÖÃÉÏÖ´ÐÐͨÅä·ûºÍ»·¾³±äÁ¿À©Õ¹£¬²¢¿ÉÄÜ´´½¨Ò»¸ö·Ç³£³¤µÄ×ÖÁÐ±í¡£

ËäÈ»ËùÓÐͨÅä·ûÀ©Õ¹Ê¾ÀýʹÓÃÁË ¾ø¶Ô·¾¶£¬µ«Ò²¿ÉÒÔʹÓÃÏà¶Ô·¾¶£¬ÈçÏÂËùʾ£º

for x in ../* mystuff/* do echo $x is a silly file done

    ÔÚÉÏÀýÖУ¬bash Ïà¶ÔÓÚµ±Ç°¹¤×÷Ŀ¼ִÐÐͨÅä·ûÀ©Õ¹£¬¾ÍÏóÔÚÃüÁîÐÐÖÐʹÓÃÏà¶Ô·¾¶Ò»Ñù¡£Ñо¿Ò»ÏÂͨÅä·ûÀ©Õ¹¡£Äú½«×¢Òâµ½£¬Èç¹ûÔÚͨÅä·ûÖÐʹÓþø¶Ô·¾¶£¬bash ½«Í¨Åä·ûÀ©Õ¹³ÉÒ»¸ö¾ø¶Ô·¾¶ÁÐ±í¡£·ñÔò£¬bash ½«ÔÚºóÃæµÄ×ÖÁбíÖÐʹÓÃÏà¶Ô·¾¶¡£Èç¹ûÖ»ÒýÓõ±Ç°¹¤×÷Ŀ¼ÖеÄÎļþ£¨ÀýÈ磬Èç¹ûÊäÈë "for x in *"£©£¬Ôò²úÉúµÄÎļþÁÐ±í½«Ã»Óз¾¶ÐÅÏ¢µÄǰ׺¡£Çë¼Çס£¬¿ÉÒÔʹÓà "basename" ¿ÉÖ´ÐгÌÐòÀ´³ýȥǰÃæµÄ·¾¶ÐÅÏ¢£¬ÈçÏÂËùʾ£º

for x in /var/log/* do echo `basename $x` is a file living in /var/log done

    µ±È»£¬Ôڽű¾µÄÃüÁîÐÐ×Ô±äÁ¿ÉÏÖ´ÐÐÑ­»·Í¨³£ºÜ·½±ã¡£ÕâÀïÓÐÒ»¸öÈçºÎʹÓñ¾ÎÄ¿ªÊ¼Ìáµ½µÄ "$@" ±äÁ¿µÄÀý×Ó£º

#!/usr/bin/env bash for thing in "$@" do echo you typed ${thing}. done Êä³ö£º $ allargs hello there you silly you typed hello. you typed there. you typed you. you typed silly.

Shell ËãÊõ

    ÔÚѧϰÁíÒ»ÀàÐ͵ÄÑ­»·½á¹¹Ö®Ç°£¬×îºÃÏÈÊìϤÈçºÎÖ´ÐÐ shell ËãÊõ¡£Êǵģ¬È·ÊµÈç´Ë£º¿ÉÒÔʹÓà shell ½á¹¹À´Ö´Ðмòµ¥µÄÕûÊýÔËËã¡£Ö»Ð轫Ìض¨µÄËãÊõ±í´ïʽÓà "$((" ºÍ "))" À¨Æð£¬bash ¾Í¿ÉÒÔ¼ÆËã±í´ïʽ¡£ÕâÀïÓÐһЩÀý×Ó£º

$ echo $(( 100 / 3 )) 33 $ myvar="56" $ echo $(( $myvar + 12 )) 68 $ echo $(( $myvar - $myvar )) 0 $ myvar=$(( $myvar + 1 )) $ echo $myvar 57

¸ü¶àµÄÑ­»·½á¹¹£º"while" ºÍ "until"

Ö»ÒªÌض¨Ìõ¼þΪÕ棬"while" Óï¾ä¾Í»áÖ´ÐУ¬Æä¸ñʽÈçÏ£º

while [ condition ] do statements done

ͨ³£Ê¹Óà "While" Óï¾äÀ´Ñ­»·Ò»¶¨´ÎÊý£¬±ÈÈ磬ÏÂÀý½«Ñ­»· 10 ´Î£º

myvar=0 while [ $myvar -ne 10 ] do echo $myvar myvar=$(( $myvar + 1 )) done

¿ÉÒÔ¿´µ½£¬ÉÏÀýʹÓÃÁËËãÊõ±í´ïʽÀ´Ê¹Ìõ¼þ×îÖÕΪ¼Ù£¬²¢µ¼ÖÂÑ­»·ÖÕÖ¹¡£

    "Until" Óï¾äÌṩÁËÓë "while" Óï¾äÏà·´µÄ¹¦ÄÜ£ºÖ»ÒªÌض¨Ìõ¼þΪ ¼Ù £¬ËüÃǾÍÖظ´¡£ÏÂÃæÊÇÒ»¸öÓëÇ°ÃæµÄ "while" Ñ­»·¾ßÓÐͬµÈ¹¦ÄÜµÄ "until" Ñ­»·£º

myvar=0 until [ $myvar -eq 10 ] do echo $myvar myvar=$(( $myvar + 1 )) done

Case Óï¾ä

Case Óï¾äÊÇÁíÒ»ÖÖ±ãÀûµÄÌõ¼þ½á¹¹¡£ÕâÀïÓÐÒ»¸öʾÀýƬ¶Î£º

case "${x##*.}" in gz) gzunpack ${SROOT}/${x} ;; bz2) bz2unpack ${SROOT}/${x} ;; *) echo "Archive format not recognized." exit ;; esac

    ÔÚÉÏÀýÖУ¬bash Ê×ÏÈÀ©Õ¹ "${x##*.}"¡£ÔÚ´úÂëÖУ¬"$x" ÊÇÎļþµÄÃû³Æ£¬"${x##.*}" ³ýÈ¥ÎļþÖÐ×îºó¾äµãºóÎı¾Ö®ÍâµÄËùÓÐÎı¾¡£È»ºó£¬bash ½«²úÉúµÄ×Ö·û´®Óë ")" ×ó±ßÁгöµÄÖµ×ö±È½Ï¡£ÔÚ±¾ÀýÖУ¬"${x##.*}" ÏÈÓë "gz" ±È½Ï£¬È»ºóÊÇ "bz2"£¬×îºóÊÇ "*"¡£Èç¹û "${x##.*}" ÓëÕâЩ×Ö·û´®»òģʽÖеÄÈκÎÒ»¸öÆ¥Å䣬ÔòÖ´Ðнô½Ó ")" Ö®ºóµÄÐУ¬Ö±µ½ ";;" Ϊֹ£¬È»ºó bash ¼ÌÐøÖ´ÐнáÊø·û "esac" Ö®ºóµÄÐС£Èç¹û²»Æ¥ÅäÈκÎģʽ»ò×Ö·û´®£¬Ôò²»Ö´ÐÐÈκδúÂëÐУ¬ÔÚÕâ¸öÌØÊâµÄ´úÂëƬ¶ÎÖУ¬ÖÁÉÙÒªÖ´ÐÐÒ»¸ö´úÂë¿é£¬ÒòΪÈκβ»Óë "gz" »ò "bz2" Æ¥ÅäµÄ×Ö·û´®¶¼½«Óë "*" ģʽƥÅä¡£

º¯ÊýÓëÃû³Æ¿Õ¼ä

    ÔÚ bash ÖУ¬ÉõÖÁ¿ÉÒÔ¶¨ÒåÓëÆäËü¹ý³ÌÓïÑÔ£¨Èç Pascal ºÍ C£©ÀàËƵĺ¯Êý¡£ÔÚ bash ÖУ¬º¯ÊýÉõÖÁ¿ÉÒÔʹÓÃÓë½Å±¾½ÓÊÕÃüÁîÐÐ×Ô±äÁ¿ÀàËƵķ½Ê½À´½ÓÊÕ×Ô±äÁ¿¡£ÈÃÎÒÃÇ¿´Ò»ÏÂÑù±¾º¯Êý¶¨Ò壬ȻºóÔÙ´ÓÄÇÀï¼ÌÐø£º

tarview() { echo -n "Displaying contents of $1 " if [ ${1##*.} = tar ] then echo "(uncompressed tar)" tar tvf $1 elif [ ${1##*.} = gz ] then echo "(gzip-compressed tar)" tar tzvf $1 elif [ ${1##*.} = bz2 ] then echo "(bzip2-compressed tar)" cat $1 bzip2 -d tar tvf - fi } ÁíÒ»ÖÖÇé¿ö

¿ÉÒÔʹÓà "case" Óï¾äÀ´±àдÉÏÃæµÄ´úÂë¡£ÄúÖªµÀÈçºÎ±àдÂð£¿

    ÎÒÃÇÔÚÉÏÃ涨ÒåÁËÒ»¸öÃûΪ "tarview" µÄº¯Êý£¬Ëü½ÓÊÕÒ»¸ö×Ô±äÁ¿£¬¼´Ä³ÖÖÀàÐ굀 tar Îļþ¡£ÔÚÖ´Ðиú¯Êýʱ£¬ËüÈ·¶¨×Ô±äÁ¿ÊÇÄÄÖÖ tar ÎļþÀàÐÍ£¨Î´Ñ¹ËõµÄ¡¢gzip ѹËõµÄ»ò bzip2 ѹËõµÄ£©£¬´òÓ¡Ò»ÐÐÐÅÏ¢ÐÔÏûÏ¢£¬È»ºóÏÔʾ tar ÎļþµÄÄÚÈÝ¡£Ó¦¸ÃÈçϵ÷ÓÃÉÏÃæµÄº¯Êý£¨ÔÚÊäÈë¡¢Õ³Ìù»òÕÒµ½¸Ãº¯Êýºó£¬´Ó½Å±¾»òÃüÁîÐе÷ÓÃËü£©£º

$ tarview shorten.tar.gz Displaying contents of shorten.tar.gz (gzip-compressed tar) drwxr-xr-x ajr/abbot 0 1999-02-27 16:17 shorten-2.3a/ -rw-r--r-- ajr/abbot 1143 1997-09-04 04:06 shorten-2.3a/Makefile -rw-r--r-- ajr/abbot 1199 1996-02-04 12:24 shorten-2.3a/INSTALL -rw-r--r-- ajr/abbot 839 1996-05-29 00:19 shorten-2.3a/LICENSE .... ½»»¥µØʹÓÃËüÃÇ

±ðÍüÁË£¬¿ÉÒÔ½«º¯Êý£¨ÈçÉÏÃæµÄº¯Êý£©·ÅÔÚ ~/.bashrc »ò ~/.bash_profile ÖУ¬ÒÔ±ãÔÚ bash ÖÐËæʱʹÓÃËüÃÇ¡£

    ÈçÄúËù¼û£¬¿ÉÒÔʹÓÃÓëÒýÓÃÃüÁîÐÐ×Ô±äÁ¿Í¬ÑùµÄ»úÖÆÀ´ÔÚº¯Êý¶¨ÒåÄÚ²¿ÒýÓÃ×Ô±äÁ¿¡£ÁíÍ⣬½«°Ñ "$#" ºêÀ©Õ¹³É°üº¬×Ô±äÁ¿µÄÊýÄ¿¡£Î¨Ò»¿ÉÄܲ»ÍêÈ«ÏàͬµÄÊDZäÁ¿ "$0"£¬Ëü½«À©Õ¹³É×Ö·û´® "bash"£¨Èç¹û´Ó shell ½»»¥ÔËÐк¯Êý£©»òµ÷Óú¯ÊýµÄ½Å±¾Ãû³Æ¡£

Ãû³Æ¿Õ¼ä

    ¾­³£ÐèÒªÔÚº¯ÊýÖд´½¨»·¾³±äÁ¿¡£ËäÈ»ÓпÉÄÜ£¬µ«ÊÇ»¹ÓÐÒ»¸ö¼¼Êõϸ½ÚÓ¦¸ÃÁ˽⡣ÔÚ´ó¶àÊý±àÒëÓïÑÔ£¨Èç C£©ÖУ¬µ±ÔÚº¯ÊýÄÚ²¿´´½¨±äÁ¿Ê±£¬±äÁ¿±»·ÅÖÃÔÚµ¥¶ÀµÄ¾Ö²¿Ãû³Æ¿Õ¼äÖС£Òò´Ë£¬Èç¹ûÔÚ C Öж¨ÒåÒ»¸öÃûΪ myfunction µÄº¯Êý£¬²¢Ôڸú¯ÊýÖж¨ÒåÒ»¸öÃûΪ "x" µÄ×Ô±äÁ¿£¬ÔòÈκÎÃûΪ "x" µÄÈ«¾Ö±äÁ¿£¨º¯ÊýÖ®ÍâµÄ±äÁ¿£©½«²»ÊÜËüµÄÓ¡Ï󣬴ӶøÏû³ýÁ˸º×÷Óá£

    ÔÚ C ÖÐÊÇÕâÑù£¬µ«ÔÚ bash ÖÐÈ´²»ÊÇ¡£ÔÚ bash ÖУ¬Ã¿µ±ÔÚº¯ÊýÄÚ²¿´´½¨»·¾³±äÁ¿£¬¾Í½«ÆäÌí¼Óµ½ È«¾ÖÃû³Æ¿Õ¼ä¡£ÕâÒâζ×Å£¬¸Ã±äÁ¿½«ÖØдº¯ÊýÖ®ÍâµÄÈ«¾Ö±äÁ¿£¬²¢ÔÚº¯ÊýÍ˳öÖ®ºó¼ÌÐø´æÔÚ£º

#!/usr/bin/env bash myvar="hello" myfunc() { myvar="one two three" for x in $myvar do echo $x done } myfunc echo $myvar $x

    ÔËÐд˽ű¾Ê±£¬Ëü½«Êä³ö "one two three three"£¬ÕâÏÔʾÁËÔÚº¯ÊýÖж¨ÒåµÄ "$myvar" ÈçºÎÓ°ÏìÈ«¾Ö±äÁ¿ "$myvar"£¬ÒÔ¼°Ñ­»·¿ØÖƱäÁ¿ "$x" ÈçºÎÔÚº¯ÊýÍ˳öÖ®ºó¼ÌÐø´æÔÚ£¨Èç¹û "$x" È«¾Ö±äÁ¿´æÔÚ£¬Ò²½«Êܵ½Ó°Ï죩¡£

    ÔÚÕâ¸ö¼òµ¥µÄÀý×ÓÖУ¬ºÜÈÝÒ×ÕÒµ½¸Ã´íÎ󣬲¢Í¨¹ýʹÓÃÆäËü±äÁ¿ÃûÀ´¸ÄÕý´íÎó¡£µ«Õâ²»ÊÇÕýÈ·µÄ·½·¨£¬½â¾ö´ËÎÊÌâµÄ×îºÃ·½·¨ÊÇͨ¹ýʹÓà "local" ÃüÁÔÚÒ»¿ªÊ¼¾ÍÔ¤·ÀÓ°ÏìÈ«¾Ö±äÁ¿µÄ¿ÉÄÜÐÔ¡£µ±Ê¹Óà "local" ÔÚº¯ÊýÄÚ²¿´´½¨±äÁ¿Ê±£¬½«°ÑËüÃÇ·ÅÔÚ ¾Ö²¿Ãû³Æ¿Õ¼äÖУ¬²¢ÇÒ²»»áÓ°ÏìÈκÎÈ«¾Ö±äÁ¿¡£ÕâÀïÑÝʾÁËÈçºÎʵÏÖÉÏÊö´úÂ룬ÒԱ㲻ÖØдȫ¾Ö±äÁ¿£º

#!/usr/bin/env bash myvar="hello" myfunc() { local x local myvar="one two three" for x in $myvar do echo $x done } myfunc echo $myvar $x

    ´Ëº¯Êý½«Êä³ö "hello" -- ²»ÖØдȫ¾Ö±äÁ¿ "$myvar"£¬"$x" ÔÚ myfunc Ö®Íâ²»¼ÌÐø´æÔÚ¡£ÔÚº¯ÊýµÄµÚÒ»ÐУ¬ÎÒÃÇ´´½¨ÁËÒÔºóҪʹÓõľֲ¿±äÁ¿ x£¬¶øÔÚµÚ¶þ¸öÀý×Ó (local myvar="one two three"") ÖУ¬ÎÒÃÇ´´½¨Á˾ֲ¿±äÁ¿ myvar£¬ ͬʱ ΪÆ丳ֵ¡£ÔÚ½«Ñ­»·¿ØÖƱäÁ¿¶¨ÒåΪ¾Ö²¿±äÁ¿Ê±£¬Ê¹ÓõÚÒ»ÖÖÐÎʽºÜ·½±ã£¬ÒòΪ²»ÔÊÐí˵£º"for local x in $myvar"¡£´Ëº¯Êý²»Ó°ÏìÈκÎÈ«¾Ö±äÁ¿£¬¹ÄÀøÄúÓÃÕâÖÖ·½Ê½Éè¼ÆËùÓеĺ¯Êý¡£Ö»ÓÐÔÚÃ÷È·Ï£ÍûÒªÐÞ¸ÄÈ«¾Ö±äÁ¿Ê±£¬²Å ²»Ó¦¸ÃʹÓà "local"¡£

½áÊøÓï

    ÎÒÃÇÒѾ­Ñ§Ï°ÁË×î»ù±¾µÄ bash ¹¦ÄÜ£¬ÏÖÔÚÒª¿´Ò»ÏÂÈçºÎ»ùÓÚ bash ¿ª·¢Õû¸öÓ¦ÓóÌÐò¡£ÏÂÒ»²¿·ÖÕýÒª½²µ½¡£ÔÙ¼û£¡

£¨³ö´¦£ºhttp://www.sheup.com£©