¹ØÓÚ Shell ¾ç±¾µÄ¾µäÊ®ÈýÎÊ
1¡¢ÎªºÎ½Ð×ö Shell£¿
ÎÒÃÇÖªµÀÅÌËã»úµÄÔË×÷Àë²»¿ªÓ²¼þ£¬µ«È´ÎÞ·¨Ö±½Ó²Ù×÷Ó²¼þ£¬Ó²¼þµÄÇý¶¯Ö»ÄÜͨ¹ýÒ»ÖÖ³Æ֮Ϊ¡°²Ù×÷ϵͳ£¨OS£¬Opertating System£©µÄÈí¼þÀ´¹Ü¿Ø¡£Linux ÑÏ¿áÀ´ËµÊÇÒ»¸ö²Ù×÷ϵͳ£¨OS£©¡£
ʹÓÃÕßûÓв½·¥Ö±½Ó²Ù×÷ Kernel£¬¶øÊÇͨ¹ý Kernel µÄ¡°Íâ¿Ç¡±³ÌÐò£¬Ò²¾ÍÊÇËùνµÄ Shell À´Óë Kernel Ïàͬ¡£Shell ÊÇÒ»¸öʹÓÃÕßÓëϵͳµÄ½»»¥½çÃ棨Interface£©£¬Ö»ÄÜͨ¹ýÏÂÁîÐУ¨Command line£©À´Ê¹ÓÃϵͳÀ´Íê³ÉÊÂÇé¡£Òò´Ë Shell ×î¼òÆӵĽç˵¾ÍÊÇ£ºÏÂÁî½âÒëÆ÷£¨Command Interpreter£©
½«Ê¹ÓÃÕßµÄÏÂÁî·Òë¸øÄں˴¦Àí£»
ͬʱ£¬½«Äں˴¦ÀíЧ¹û·Òë¸øʹÓÃÕß¡£
²î±ðµÄ OS ʹÓòî±ðµÄ Kernel£»Í³Ò»¸ö kernel Ö®ÉÏ£¬Ò²¿ÉÒÔʹÓòî±ðµÄ Shell£¬³£¼ûµÄ Shell ÓÐsh¡¢bash¡¢csh¡¢ksh µÈ
2¡¢Shell prompt£¨PS1£©Óë Carriage Return£¨CR£©µÄ¹Øϵ£¿
ÀֳɵǼһ¸ö Shell Öն˺󣬹â±ê×ó±ß²¿·Ö³Æ֮ΪÌáÐÑ·û Prompt£¬Í¨³£Ò»Ñùƽ³£Óû§Ê¹ÓÃ$£¬ÖÎÀíÔ±Óû§Ê¹ÓÃ#
Shell Prompt£º¿ÉÒÔÊäÈëÏÂÁîÁË£¬¼üÈëÏÂÁîºó£¬Ö±µ½¶Á½ø CR£¨Carriage Return£©×Ö·ûΪֹ
Carriage Return£º¿ÉÒÔÖ´ÐÐÏÂÁîÁË
Èô´ÓÊÖÒÕµÄϸ½ÚÀ´¿´£¬Shell »áÒÀ¾Ý IFS£¨Internal Field Seperator£©½« Command line ËùÊäÈëµÄÎÄ×Ö²ð½âΪ¡±×ֶΡ±£¨word/field£©¡£È»ºóÔÙÕë¶ÔÌØÊâ×Ö·û£¨meta£©ÏÈ×÷´¦Àí£¬×îºóÖØ×éÕûÐÐcommand line¡£
3¡¢±ðÈËecho£¬ÄãÒ²echo£¬ÊÔÎÊ echo Öª¼¸¶à?
echo ½« argumentË͵½±ê×¼Êä³ö£¨stdout£©£¬Í¨³£ÏÔʾÔÚÆÁÄ»
stdin ±ê×¼ÊäÈë
stdout ±ê×¼Êä³ö
stderr ±ê×¼¹ýʧÊä³ö
echo -n # ×÷·Ï»»Ðзû echo -e # ÆôÓ÷´Ð±¸ÜתÒë
µÇ¼ºó¸´ÖÆ
4¡¢Ë«ÒýºÅ””Óëµ¥ÒýºÅ”ÓÐʲôÇø±ð?
hard quote£º”£¨µ¥ÒýºÅ£©£¬¹Ø±ÕËùÓÐÒýÓÃ
soft quote£º””£¨Ë«ÒýºÅ£©£¬±£´æ$ÒýÓÃ
5¡¢var=value? export Ç°ºó²îÔÚÄÄ£¿
±äÁ¿½ç˵£ºname=value£¬µÈºÅ×óÓÒË«·½²»¿ÉʹÓÃÍÑÀë·û¡£
±äÁ¿Ìæ»»£ºecho ${name}
export±äÁ¿£ºexport name=value£¬Ê¹±äÁ¿³ÉΪÇéÐαäÁ¿
# ÍâµØ±äÁ¿ A=B # ×÷·Ï±äÁ¿ unset A # ÇéÐαäÁ¿export A=B
µÇ¼ºó¸´ÖÆ
6¡¢exec ¸ú source ²îÔÚÄÄ£¿
ÇéÐαäÁ¿Ö»ÄÜ´Ó¸¸Àú³Ìµ½×ÓÀú³Ìµ¥Ïòת´ï¡£»»¾ä»°Ëµ£ºÔÚ×ÓÀú³ÌÖÐÇéÐÎÔõÑù±ä»»£¬¾ù²»»áÓ°Ï츸Àú³ÌµÄÇéÐΡ£
µ±ÎÒÃÇÖ´ÐÐÒ»¸öshell scriptʱ£¬×ÅʵÊÇÏȱ¬·¢Ò»¸ösub-shellµÄ×ÓÀú³Ì£¬ È»ºósub-shellÔÙÈ¥±¬·¢ÏÂÁîÐеÄ×ÓÀú³Ì¡£¹Ø×¢LinuxÖÐÎÄÉçÇø
# ½¨Éè×ÓshellÖ´Ðо籾 ./1.sh # Ä¿½ñshellÖ´ÐÐ source 1.sh # Ä¿½ñshellÖ´ÐÐÍËÈ´³ö exec 1.sh
µÇ¼ºó¸´ÖÆ
7¡¢( ) Óë { } ²îÔÚÄÄ£¿
( )½« command group ÖÃÓÚ sub-shell Ö´ÐÐ
{ }ÔòÊÇÔÚͳһ¸öshellÄÚÍê³É
8¡¢$(()) Óë $() ÉÐÓÐ ${} ²îÔÚÄÄ£¿
# ¼ÙÉèÎÒÃǽç˵ÁËÒ»¸ö±äÁ¿Îª£º file=/dir1/dir2/dir3/my.file.txt # ÎÒÃÇ¿ÉÒÔÓà ${ } »®·ÖÌæ»»»ñµÃ²î±ðµÄÖµ£º # 1. shell×Ö·û´®µÄ·ÇÌ°ÐÄ(×îСƥÅä)×óɾ³ý ${file#*/} # ÄõôµÚÒ»Ìõ / ¼°Æä×ó±ßµÄ×Ö·û´®£ºdir1/dir2/dir3/my.file.txt # 2. shell×Ö·û´®µÄÌ°ÐÄ(×î´óÆ¥Åä)×óɾ³ý ${file##*/} # Äõô×îºóÒ»Ìõ / ¼°Æä×ó±ßµÄ×Ö·û´®£ºmy.file.txt ${file##*.} # Äõô×îºóÒ»¸ö . ¼°Æä×ó±ßµÄ×Ö·û´®£ºtxt # 3. shell×Ö·û´®µÄ·ÇÌ°ÐÄ(×îСƥÅä)ÓÒɾ³ý£º ${file%/*} # Äõô×îºóÌõ / ¼°ÆäÓұߵÄ×Ö·û´®£º/dir1/dir2/dir3 ${file%.*} # Äõô×îºóÒ»¸ö . ¼°ÆäÓұߵÄ×Ö·û´®£º/dir1/dir2/dir3/my.file # 4. shell×Ö·û´®µÄÌ°ÐÄ(×î´óÆ¥Åä)ÓÒɾ³ý£º ${file%%/*} # ÄõôµÚÒ»Ìõ / ¼°ÆäÓұߵÄ×Ö·û´®£º(¿ÕÖµ) ${file%%.*} # ÄõôµÚÒ»¸ö . ¼°ÆäÓұߵÄ×Ö·û´®£º/dir1/dir2/dir3/my Ó°ÏóµÄÒªÁìΪ£º # ÊÇÈ¥µô×ó±ß(ÔÚ¼üÅÌÉÏ # ÔÚ $ Ö®×ó±ß) % ÊÇÈ¥µôÓÒ±ß(ÔÚ¼üÅÌÉÏ % ÔÚ $ Ö®ÓÒ±ß) ¼òµ¥·ûºÅÊÇ×îСƥÅä©rÁ½¸ö·ûºÅÊÇ×î´óÆ¥Åä¡£
µÇ¼ºó¸´ÖÆ
# 5. shell×Ö·û´®È¡×Ó´®£º ${file:0:5}£ºÌáÈ¡×î×ó±ßµÄ 5 ¸ö×Ö½Ú£º/dir1 ${file:5:5}£ºÌáÈ¡µÚ 5 ¸ö×Ö½ÚÓұߵÄÒ»Á¬ 5 ¸ö×Ö½Ú£º/dir2 # 6. shell×Ö·û´®±äÁ¿ÖµµÄÌæ»»£º ${file/dir/path}£º½«µÚÒ»¸ö dir ÌỻΪ path£º/path1/dir2/dir3/my.file.txt ${file//dir/path}£º½«ËùÓÐ dir ÌỻΪ path£º/path1/path2/path3/my.file.txt # 7. ${}»¹¿ÉÕë¶Ô±äÁ¿µÄ²î±ð״̬(ûÉ趨¡¢¿ÕÖµ¡¢·Ç¿ÕÖµ)¾ÙÐи³Öµ£º ${file-my.file.txt} £ºÈôÊÇ $file ûÓÐÉ趨£¬ÔòʹÓà my.file.txt ×÷´«»ØÖµ¡£(¿ÕÖµ¼°·Ç¿Õֵʱ²»×÷´¦Àí) ${file:-my.file.txt} £ºÈôÊÇ $file ûÓÐÉ趨»òΪ¿ÕÖµ£¬ÔòʹÓà my.file.txt ×÷´«»ØÖµ¡£(·Ç¿Õֵʱ²»×÷´¦Àí) ${file+my.file.txt} £ºÈôÊÇ $file ÉèΪ¿ÕÖµ»ò·Ç¿ÕÖµ£¬¾ùʹÓà my.file.txt ×÷´«»ØÖµ¡£(ûÉè׼ʱ²»×÷´¦Àí) ${file:+my.file.txt} £ºÈô $file Ϊ·Ç¿ÕÖµ£¬ÔòʹÓà my.file.txt ×÷´«»ØÖµ¡£(ûÉ趨¼°¿Õֵʱ²»×÷´¦Àí) ${file=my.file.txt} £ºÈô $file ûÉ趨£¬ÔòʹÓà my.file.txt ×÷´«»ØÖµ£¬Í¬Ê±½« $file ¸³ÖµÎª my.file.txt ¡£(¿ÕÖµ¼°·Ç¿Õֵʱ²»×÷´¦Àí) ${file:=my.file.txt} £ºÈô $file ûÉ趨»òΪ¿ÕÖµ£¬ÔòʹÓà my.file.txt ×÷´«»ØÖµ£¬Í¬Ê±½« $file ¸³ÖµÎª my.file.txt ¡£(·Ç¿Õֵʱ²»×÷´¦Àí) ${file?my.file.txt} £ºÈô $file ûÉ趨£¬Ôò½« my.file.txt Êä³öÖÁ STDERR¡£(¿ÕÖµ¼°·Ç¿Õֵʱ²»×÷´¦Àí) ${file:?my.file.txt} £ºÈô $file ûÉ趨»òΪ¿ÕÖµ£¬Ôò½« my.file.txt Êä³öÖÁ STDERR¡£(·Ç¿Õֵʱ²»×÷´¦Àí) tips: ÒÔÉϵÄÃ÷È·ÔÚÓÚ, ÄãÒ»¶¨Òª·ÖÇåÎú unset Óë null ¼° non-null ÕâÈýÖÖ¸³Öµ×´Ì¬. Ò»Ñùƽ³£¶øÑÔ, : Óë null ÓйØ, Èô²»´ø : µÄ»°, null ²»ÊÜÓ°Ïì, Èô´ø : ÔòÁ¬ null Ò²ÊÜÓ°Ïì.
µÇ¼ºó¸´ÖÆ
# 8. ÅÌËãshell×Ö·û´®±äÁ¿µÄ³¤¶È£º${#var} ${#var} ¿ÉÅÌËã³ö±äÁ¿ÖµµÄ³¤¶È£º ${#file} ¿É»ñµÃ 27 £¬ÓÉÓÚ /dir1/dir2/dir3/my.file.txt Ç¡Ç¡ÊÇ 27 ¸ö×Ö½Ú... # 9. bashÊý×é(array)µÄ´¦ÀíÒªÁì Êý×é: A=(a b c d) ÒýÓÃÊý×é: ${A[@]} ${A[*]} »á¼ûÊý×é³ÉÔ± ${A[0]} ÅÌËãÊý×鳤¶È ${#A[@]} ${#A[*]} Êý×éÖØи³Öµ A[2]=xyz # 10.$(( ))ÊÇÓÃÀ´×öÕûÊýÔËËãµÄ a=5;b=7;c=2; echo $(( a + b * c))
µÇ¼ºó¸´ÖÆ
9¡¢$@ Óë $* Çø±ðÔÚÄÄ£¿
“$@”Ôò¿É»ñµÃ ¡°p1¡± ¡°p2 p3¡± ¡°p4¡± ÕâÈý¸ö²î±ðµÄ´Ê¶Î
“$*”Ôò¿É»ñµÃ ¡°p1 p2 p3 p4¡± ÕâÒ»Õû´®¼òµ¥µÄ´Ê¶Î
ÁíÍ⣬ËÑË÷ÃñÖÚºÅLinux¾Í¸ÃÕâÑùѧºǫ́»Ø¸´¡°gitÊé¼®¡±£¬»ñÈ¡Ò»·Ý¾ªÏ²Àñ°ü¡£
10¡¢&& Óë || ²îÔÚÄÄ£¿
1¡¢testÏÂÁîÓÐÁ½ÖÖÐÎʽ
test expression
[ expression ]
2¡¢bashµÄtestÏÖÔÚÖ§³ÖÈýÖÖ²âÊÔ¹¤¾ß
string£º×Ö·û´®
integer£ºÕûÊý
file£ºÎļþ
3¡¢µ± expression ΪÕæÊÇ·µ»Ø 0£¨true£© £¬²»È»·µ»Ø ·Ç0£¨false£©
command1 && command2 command2 Ö»ÓÐÔÚ command1 µÄRVΪ0£¨True£©µÄÌõ¼þÏÂÖ´ÐС£
command1 || command2 command2Ö»ÓÐÔÚcommand1µÄRVΪ·Ç0(False)µÄÌõ¼þÏÂÖ´ÐС£
4¡¢ÏÈÌæ»»±äÁ¿ÔÙ½ÏÁ¿
A=123[ -n "$A" ] && ([ "$A" -lt 100 ] || echo "too big")unset A
µÇ¼ºó¸´ÖÆ
11¡¢> Óë < ²îÔÚÄÄ£¿
0: Standard Input£¨STDIN£©
1: Standard Output £¨STDOUT£©
2: Standard Error Output£¨STDERR£©
ÎÒÃÇ¿ÉÓà < À´¸Ä±ä¶Á½øµÄÊý¾ÝÐŵÀ£¨stdin£©£¬Ê¹Ö®´ÓÖ¸¶¨µÄµµ°¸¶Á½ø¡£
ÎÒÃÇ¿ÉÓà > À´¸Ä±äËͳöµÄÊý¾ÝÐŵÀ£¨stdout, stderr£©£¬Ê¹Ö®Êä³öµ½Ö¸¶¨µÄµµ°¸¡£
ls my.file no.such.file 1> file.out 2>file.err # 2>&1 ¾ÍÊǽ«stderr²¢½østdout×öÊä³ö ls my.file no.such.file 1> file.out 2>&1 # /dev/null ¿Õ ls my.file no.such.file >/dev/null 2>&1 cat < file > file # ÔÚ IO Redirection ÖУ¬stdout Óë stderr µÄ¹ÜµÀ»áÏÈ×¼±¸ºÃ£¬²Å»á´Ó stdin ¶Á½ø×ÊÁÏ¡£ # Ò²¾ÍÊÇ˵£¬ÔÚÉÏÀýÖУ¬> file »áÏȽ« file Çå¿Õ£¬È»ºó²Å¶Á½ø < file £¬ # µ«Õâʱ¼äµµ°¸ÒѾ±»Çå¿ÕÁË£¬Òò´Ë¾ÍÄð³É¶Á²»½øÈκÎÊý¾ÝÁË
µÇ¼ºó¸´ÖÆ
12¡¢ÄãÒªifÕÕ¾ÉcaseÄØ£¿
# if echo -n "Do you want to continue?(Yes/No):" read YN if [ "$YN"=Y -o "$YN"=y -o "$YN"="Yes" -o "$YN"="yes" -o "$YN"="YES"];then echo "continue" else exit 0 fi # case echo -n "Do you want to continue?(Yes/No):" read YN case "$YN" in [Yy]|[Yy][Ee][Ss]) echo "continue" ;; *) exit 0 esac
µÇ¼ºó¸´ÖÆ
13¡¢for what? whileÓëuntil²îÔÚÄÄ£¿
# for for ((i=1;i<=10;i++)) do echo "num is $i" done # while num=1 while [ "$num" -le 10 ]; do echo "num is $num" num=$(($num + 1)) done # until num=1 until [ "$num" -gt 10 ]; do echo "num is $num" num=$(($nu + 1)) done
µÇ¼ºó¸´ÖÆ
break ÊÇ¿¢Ê loop
return ÊÇ¿¢Ê function
exit ÊÇ¿¢Ê script/shell
ÒÔÉϾÍÊǹØÓÚ Shell ¾ç±¾µÄ¾µäÊ®ÈýÎʵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡