ÄãÓ¦¸ÃÖªµÀµÄShell ¾ç±¾µÄ¾µäÊ®ÈýÎÊ
¾µäµÄShellÊ®ÈýÎÊ
1. ΪºÎ½Ð×öshell?
2. Shell prompt(PS1)ÓëCarriage Return(CR)µÄ¹Øϵ?
3. ±ðÈËecho, ÄãÒ²echo, ÊÇÎÊechoÖª¼¸¶à?
4. “”(Ë«ÒýºÅ)Óë(µ¥ÒýºÅ)ÓÐʲôÇø±ð?
5. var=value? exportÇ°ºó²îÔÚÄÄ£¿
6. exec¸úsource²îÔÚÄÄ£¿
7. ( ) Óë { } ²îÔÚÄÄ£¿
8. Óë () ÉÐÓÐ ${} ²îÔÚÄÄ£¿
9. Óë* Çø±ðÔÚÄÄ£¿
10. && Óë || ²îÔÚÄÄ£¿
11. > Óë
12. ÄãÒªifÕÕ¾ÉcaseÄØ£¿
13. for what? whileÓëuntil²îÔÚÄÄ£¿
1. ΪºÎ½Ð×öshell?
ÎÒÃÇÖªµÀÅÌËã»úµÄÔË×÷²»¿ÉÍÑÀëÓ²¼þ£¬µ«Ê¹ÓÃÕßÈ´ÎÞ·¨Ö±½Ó²Ù×÷Ó²¼þ£¬Ó²¼þµÄÇý¶¯Ö»ÄÜͨ¹ýÒ»ÖÖ³ÆΪ²Ù×÷ϵͳ(OS£¬Opertating System)µÄÈí¼þÀ´¹Ü¿Ø¡£linuxÑÏ¿áÀ´ËµÖ»ÊÇÒ»¸ö²Ù×÷ϵͳ(OS)£¬ÎÒÃdzÆ֮ΪÄÚºË(kernel)¡£
ʹÓÃÕßûÓв½·¥Ö±½Ó²Ù×÷Ò»¸ö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Öն˺ó£¬Óαêcursor×ó±ß²¿·Ö£¬³Æ֮ΪÌáÐÑ·ûprompt
ͨ³£Ò»Ñùƽ³£Óû§Ê¹ÓÃ$£¬ÖÎÀíÔ±Óû§rootʹÓÃ#
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¾Í¸ÃÕâÑùѧºǫ́»Ø¸´¡°gitÊé¼®¡±£¬»ñÈ¡Ò»·Ý¾ªÏ²Àñ°ü¡£
# ½¨Éè×ÓshellÖ´Ðо籾 ./sh # Ä¿½ñshellÖ´ÐÐ source sh # Ä¿½ñshellÖ´ÐÐÍËÈ´³ö exec sh
µÇ¼ºó¸´ÖÆ
7. ( ) Óë { } ²îÔÚÄÄ£¿
( ) ½«command groupÖÃÓÚsub-shellÖ´ÐÐ
{ } ÔòÊÇÔÚͳһ¸öshellÄÚÍê³É
8. Óë () ÉÐÓÐ ${} ²îÔÚÄÄ£¿
$()Óë “(·´ÒýºÅ) ¶¼ÊÇÓÃÀ´×öÏÂÁîÌæ»»Óõġ£
${var}Óë $var ¶¼ÊÇÓÃÀ´×ö±äÁ¿Ìæ»»Óõġ£
# ¼ÙÉèÎÒÃǽç˵ÁËÒ»¸ö±äÁ¿Îª£º file=/dir1/dir2/dir3/my.file.txt # ÎÒÃÇ¿ÉÒÔÓà ${ } »®·ÖÌæ»»»ñµÃ²î±ðµÄÖµ£º # shell×Ö·û´®µÄ·ÇÌ°ÐÄ(×îСƥÅä)×óɾ³ý ${file#*/} # ÄõôµÚÒ»Ìõ / ¼°Æä×ó±ßµÄ×Ö·û´®£ºdir1/dir2/dir3/my.file.txt ${file#*.} #ÄõôµÚÒ»¸ö . ¼°Æä×ó±ßµÄ×Ö·û´®£ºfile.txt # shell×Ö·û´®µÄÌ°ÐÄ(×î´óÆ¥Åä)×óɾ³ý ${file##*/} # Äõô×îºóÒ»Ìõ / ¼°Æä×ó±ßµÄ×Ö·û´®£ºmy.file.txt ${file##*.} # Äõô×îºóÒ»¸ö . ¼°Æä×ó±ßµÄ×Ö·û´®£ºtxt # shell×Ö·û´®µÄ·ÇÌ°ÐÄ(×îСƥÅä)ÓÒɾ³ý£º ${file%/*} # Äõô×îºóÌõ / ¼°ÆäÓұߵÄ×Ö·û´®£º/dir1/dir2/dir3 ${file%.*} # Äõô×îºóÒ»¸ö . ¼°ÆäÓұߵÄ×Ö·û´®£º/dir1/dir2/dir3/my.file # shell×Ö·û´®µÄÌ°ÐÄ(×î´óÆ¥Åä)ÓÒɾ³ý£º ${file%%/*} # ÄõôµÚÒ»Ìõ / ¼°ÆäÓұߵÄ×Ö·û´®£º(¿ÕÖµ) ${file%%.*} # ÄõôµÚÒ»¸ö . ¼°ÆäÓұߵÄ×Ö·û´®£º/dir1/dir2/dir3/my Ó°ÏóµÄÒªÁìΪ£º # ÊÇÈ¥µô×ó±ß(ÔÚ¼üÅÌÉÏ # ÔÚ $ Ö®×ó±ß) % ÊÇÈ¥µôÓÒ±ß(ÔÚ¼üÅÌÉÏ % ÔÚ $ Ö®ÓÒ±ß) ¼òµ¥·ûºÅÊÇ×îСƥÅä©rÁ½¸ö·ûºÅÊÇ×î´óÆ¥Åä¡£
µÇ¼ºó¸´ÖÆ
# shell×Ö·û´®È¡×Ó´®£º ${file:0:5}£ºÌáÈ¡×î×ó±ßµÄ 5 ¸ö×Ö½Ú£º/dir1 ${file:5:5}£ºÌáÈ¡µÚ 5 ¸ö×Ö½ÚÓұߵÄÒ»Á¬ 5 ¸ö×Ö½Ú£º/dir2 # shell×Ö·û´®±äÁ¿ÖµµÄÌæ»»£º ${file/dir/path}£º½«µÚÒ»¸ö dir ÌỻΪ path£º/path1/dir2/dir3/my.file.txt ${file//dir/path}£º½«ËùÓÐ dir ÌỻΪ path£º/path1/path2/path3/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} £ºÈô $file ûÉ趨£¬Ôò½« my.file.txt Êä³öÖÁ STDERR¡£ (¿ÕÖµ¼°·Ç¿Õֵʱ²»×÷´¦Öóͷ£) ${file:?my.file.txt} £ºÈô $file ûÉ趨»òΪ¿ÕÖµ£¬Ôò½« my.file.txt Êä³öÖÁ STDERR¡£ (·Ç¿Õֵʱ²»×÷´¦Öóͷ£) tips: ÒÔÉϵÄÃ÷È·ÔÚÓÚ, ÄãÒ»¶¨Òª·ÖÇåÎú unset Óë null ¼° non-null ÕâÈýÖÖ¸³Öµ×´Ì¬. Ò»Ñùƽ³£¶øÑÔ, : Óë null ÓйØ, Èô²»´ø : µÄ»°, null ²»ÊÜÓ°Ïì, Èô´ø : ÔòÁ¬ null Ò²ÊÜÓ°Ïì.
µÇ¼ºó¸´ÖÆ
# ÅÌËãshell×Ö·û´®±äÁ¿µÄ³¤¶È£º${#var} ${#var} ¿ÉÅÌËã³ö±äÁ¿ÖµµÄ³¤¶È£º ${#file} ¿É»ñµÃ 27 £¬ÓÉÓÚ /dir1/dir2/dir3/my.file.txt Ç¡Ç¡ÊÇ 27 ¸ö×Ö½Ú... # bashÊý×é(array)µÄ´¦Öóͷ£ÒªÁì Êý×é: A=(a b c d) ÒýÓÃÊý×é: ${A[@]} ${A[*]} »á¼ûÊý×é³ÉÔ± ${A[0]} ÅÌËãÊý×鳤¶È ${#A[@]} ${#A[*]} Êý×éÖØи³Öµ A[2]=xyz # $(( ))ÊÇÓÃÀ´×öÕûÊýÔËËãµÄ a=5;b=7;c=2; echo $(( a + b * c))
µÇ¼ºó¸´ÖÆ
9. Óë* Çø±ðÔÚÄÄ£¿
“$@” Ôò¿É»ñµÃ “p1” “p2 p3” “p4” ÕâÈý¸ö²î±ðµÄ´Ê¶Î
“$*” Ôò¿É»ñµÃ “p1 p2 p3 p4” ÕâÒ»Õû´®¼òµ¥µÄ´Ê¶Î
10. && Óë || ²îÔÚÄÄ£¿
testÏÂÁîÓÐÁ½ÖÖÐÎʽ
test expression
[ expression ]
bashµÄtestÏÖÔÚÖ§³ÖÈýÖÖ²âÊÔ¹¤¾ß
string£º×Ö·û´®
integer£ºÕûÊý
file£ºÎļþ
µ±expressionΪÕæÊÇ·µ»Ø 0(true) £¬²»È»·µ»Ø ·Ç0(false)
command1 && command2 # command2Ö»ÓÐÔÚcommand1µÄRVΪ0(true)µÄÌõ¼þÏÂÖ´ÐС£ ¹Ø×¢LinuxÖÐÎÄÉçÇø
command1 || command2 # command2Ö»ÓÐÔÚcommand1µÄRVΪ·Ç0(false)µÄÌõ¼þÏÂÖ´ÐС£
ÏÈÌæ»»±äÁ¿ÔÙ½ÏÁ¿
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 ¾ç±¾µÄ¾µäÊ®ÈýÎʵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡