{"id":112008,"date":"2024-12-16T19:19:10","date_gmt":"2024-12-16T11:19:10","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=112008"},"modified":"2024-12-16T19:19:10","modified_gmt":"2024-12-16T11:19:10","slug":"bash-programming-4","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/bash-programming-4.html","title":{"rendered":"\u7b2c\u56db\u8282\uff1aBash\u7f16\u7a0b\u6613\u72af\u7684\u9519\u8bef"},"content":{"rendered":"
\u4e0a\u4e00\u7bc7\u6587\u7ae0\u53c2\u89c1 \u7b2c\u4e09\u8282\uff1aBash\u7f16\u7a0b\u6613\u72af\u7684\u9519\u8bef\u3002\u8fd9\u4e00\u7bc7\u7ffb\u8bd1\u5f97\u4e0d\u662f\u975e\u5e38\u6ee1\u610f\uff0c\u65f6\u95f4\u6bd4\u8f83\u8d76\uff0c\u8bf7\u89c1\u8c05\uff0c\u5982\u679c\u6709\u95ee\u9898\u53ef\u4ee5\u5728\u672c\u6587\u540e\u65b9\u7559\u8a00\uff0c\u5927\u5bb6\u4e00\u8d77\u6df1\u5165\u63a2\u8ba8\u3002<\/p>\n
36. [ -n $foo ] or [ -z $foo ]<\/p>\n
\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c$foo \u6ca1\u6709\u7528\u5f15\u53f7\u5f15\u8d77\u6765\uff0c\u5f53$foo\u5305\u542b\u7a7a\u683c\u6216\u8005$foo\u4e3a\u7a7a\u65f6\u90fd\u4f1a\u51fa\u95ee\u9898\uff1a<\/p>\n
\r\n$ foo=\"some word\" && [ -n $foo ] && echo yes\r\n-bash: [: some: binary operator expected\r\n\r\n$ foo=\"\" && [ -n $foo ] && echo yes\r\nyes\r\n\r\n\u6b63\u786e\u7684\u5199\u6cd5\u662f\uff1a\r\n\r\n[ -n \"$foo\" ]\r\n[ -z \"$foo\" ]\r\n[ -n \"$(some command with a \"$file\" in it)\" ]\r\n\r\n[[ -n $foo ]]\r\n[[ -z $foo ]]\r\n<\/pre>\n37. [[ -e \"$broken_symlink\" ]] returns 1 even though $broken_symlink exists<\/p>\n
\u8fd9\u91cc-e \u9009\u9879\u662f\u770b\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff0c\u5f53\u7d27\u8ddf\u7684\u6587\u4ef6\u662f\u4e00\u4e2a\u8f6f\u94fe\u63a5\u65f6\uff0c\u5b83\u4e0d\u770b\u8f6f\u94fe\u63a5\u662f\u5426\u5b58\u5728\uff0c\u800c\u662f\u770b\u5b9e\u9645\u6307\u5411\u7684\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002\u6240\u4ee5\u5f53\u8f6f\u94fe\u63a5\u635f\u574f\u65f6\uff0c\u5373\u5b9e\u9645\u6307\u5411\u7684\u6587\u4ef6\u88ab\u5220\u9664\u540e\uff0c-e \u7684\u7ed3\u679c\u8fd4\u56de1\u3002<\/p>\n
\u6240\u4ee5\u5982\u679c\u4f60\u786e\u5b9e\u8981\u5224\u65ad\u540e\u9762\u7684\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff0c\u6b63\u786e\u7684\u5199\u6cd5\u662f\uff1a<\/p>\n
\r\n[[ -e \"$broken_symlink\" || -L \"$broken_symlink\" ]]\r\n<\/pre>\n38. ed file < <<\"g\/d\\{0,3\\}\/s\/\/e\/g\" fails\n\ned \u547d\u4ee4\u4f7f\u7528\u7684\u6b63\u5219\u8bed\u6cd5\uff0c\u4e0d\u652f\u63010\u6b21\u51fa\u73b0\u6b21\u6570\uff0c\u4e0b\u9762\u7684\u5c31\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\uff1a\n\n\n\n
\r\ned file < <<\"g\/d\\{1,3\\}\/s\/\/e\/g\"\r\n<\/pre>\n\u7565\u8fc7\uff0c\u73b0\u5728\u5f88\u5c11\u4f1a\u6709\u4eba\u7528 ed \u547d\u4ee4\u5427\u3002<\/p>\n
39. expr sub-string fails for \"match\"<\/p>\n
\u4e0b\u9762\u7684\u4f8b\u5b50\u591a\u6570\u60c5\u51b5\u4e0b\u8fd0\u884c\u4e0d\u4f1a\u6709\u95ee\u9898\uff1a<\/p>\n
\r\nword=abcde\r\nexpr \"$word\" : \".\\(.*\\)\"\r\nbcde\r\n<\/pre>\n\u4f46\u662f\u5f53 $work \u4e0d\u5de7\u521a\u597d\u662f match \u65f6\uff0c\u5c31\u6709\u53ef\u80fd\u51fa\u9519\u4e86\uff08MAC OSX \u4e0b\u7684 expr \u547d\u4ee4\u4e0d\u652f\u6301 match\uff0c\u6240\u4ee5\u4f9d\u7136\u80fd\u6b63\u5e38\u5de5\u4f5c\uff09\uff1a<\/p>\n
\r\nword=match\r\nexpr \"$word\" : \".\\(.*\\)\"\r\n<\/pre>\n\u539f\u56e0\u662f match \u662f expr \u547d\u4ee4\u91cc\u9762\u7684\u4e00\u4e2a\u7279\u6b8a\u5173\u952e\u5b57\uff0c\u9488\u5bf9 GNU\u7cfb\u7edf\uff0c\u89e3\u51b3\u65b9\u6cd5\u662f\u5728\u524d\u9762\u52a0\u4e00\u4e2a'+'\uff1a<\/p>\n
\r\nword=match\r\nexpr + \"$word\" : \".\\(.*\\)\"\r\natch\r\n<\/pre>\n'+'\u53f7\u53ef\u4ee5\u8ba9 expr \u547d\u4ee4\u5ffd\u7565\u540e\u7eed token \u7684\u7279\u6b8a\u542b\u4e49\u3002<\/p>\n
\u53e6\u5916\u4e00\u4e2a\u5efa\u8bae\u662f\uff0c\u4e0d\u8981\u518d\u4f7f\u7528 expr \u547d\u4ee4\u4e86\uff0cexpr \u80fd\u505a\u7684\u4e8b\u60c5\u90fd\u53ef\u4ee5\u7528 Bash \u539f\u751f\u652f\u6301\u7684\u53c2\u6570\u5c55\u5f00\uff08Parameter Expansion\uff09\u6216\u8005\u5b57\u7b26\u4e32\u5c55\u5f00\uff08Substring Expansion\uff09\u6765\u5b8c\u6210\u3002\u5e76\u4e14\u76f8\u540c\u60c5\u51b5\u4e0b\uff0c\u5185\u7f6e\u7684\u529f\u80fd\u80af\u5b9a\u6bd4\u5916\u90e8\u547d\u4ee4\u7684\u6548\u7387\u8981\u9ad8\u3002<\/p>\n
\u4e0a\u9762\u7684\u4f8b\u5b50\uff0c\u76ee\u7684\u662f\u4e3a\u4e86\u5220\u9664\u5355\u8bcd\u4e2d\u7684\u9996\u5b57\u7b26\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n
\r\n$ word=match\r\n$ echo \"${word#?}\" # PE\r\natch\r\n$ echo \"${word:1}\" # SE\r\natch\r\n<\/pre>\n40. On UTF-8 and Byte-Order Marks (BOM)<\/p>\n
\u591a\u6570\u60c5\u51b5\u4e0b\uff0cUNIX \u4e0b UTF-8 \u7c7b\u578b\u7684\u6587\u672c\u4e0d\u9700\u8981\u4f7f\u7528 BOM\uff0c\u6587\u672c\u7684\u7f16\u7801\u662f\u6839\u636e\u5f53\u524d\u8bed\u8a00\u73af\u5883\uff0cMIME\u7c7b\u578b\u6216\u8005\u5176\u5b83\u6587\u4ef6\u5143\u6570\u636e\u4fe1\u606f\u786e\u5b9a\u7684\u3002\u4eba\u4e3a\u9605\u8bfb\u65f6\uff0c\u4e0d\u4f1a\u56e0\u4e3a\u5728\u6587\u4ef6\u5f00\u59cb\u5904\u52a0 BOM \u6807\u8bb0\u800c\u815a\u5f71\u54cd\uff0c\u4f46\u662f\u5f53\u6587\u4ef6\u8981\u88ab\u811a\u672c\u89e3\u91ca\u6267\u884c\u65f6\uff0cBOM \u6807\u8bb0\u4f1a\u50cf MS-DOS \u4e0b\u7684\u6362\u884c\u7b26\uff08^M\uff09\u4e00\u6837\u5947\u602a\u3002<\/p>\n
41. content=$(
&1 >>logfile<\/p>\n \u8fd9\u662f\u4e00\u4e2a\u5f88\u5e38\u89c1\u7684\u9519\u8bef\uff0c\u663e\u7136\u4f60\u672c\u6765\u662f\u60f3\u5c06\u6807\u51c6\u8f93\u51fa\u4e0e\u6807\u51c6\u9519\u8bef\u8f93\u51fa\u90fd\u91cd\u5b9a\u5411\u5230\u6587\u4ef6logfile \u4e2d\uff0c\u4f46\u662f\u4f60\u4f1a\u60ca\u8bb6\u5730\u53d1\u73b0\uff0c\u6807\u51c6\u9519\u8bef\u4f9d\u7136\u8f93\u51fa\u5230\u5c4f\u5e55\u4e2d\u3002<\/p>\n
\u8fd9\u79cd\u884c\u4e3a\u7684\u539f\u56e0\u662f\uff0c\u91cd\u5b9a\u5411\u5728\u547d\u4ee4\u6267\u884c\u4e4b\u524d\u89e3\u6790\uff0c\u5e76\u4e14\u662f\u4ece\u5de6\u5f80\u53f3\u89e3\u6790\u3002\u4e0a\u9762\u7684\u547d\u4ee4\u53ef\u4ee5\u7ffb\u8bd1\u6210\uff0c\u5c06\u6807\u51c6\u9519\u8bef\u8f93\u51fa\u91cd\u5b9a\u5411\u5230\u6807\u51c6\u8f93\u51fa\uff08\u6b64\u523b\u662f\u7ec8\u7aef\uff09\uff0c\u7136\u540e\u5c06\u6807\u51c6\u8f93\u51fa\u91cd\u5b9a\u5411\u5230\u6587\u4ef6 logfile \u4e2d\u3002\u6240\u4ee5\uff0c\u5230\u6700\u540e\uff0c\u6807\u51c6\u9519\u8bef\u5e76\u6ca1\u6709\u91cd\u5b9a\u5411\u5230\u6587\u4ef6\u4e2d\uff0c\u800c\u662f\u4f9d\u7136\u8f93\u51fa\u5230\u7ec8\u7aef\uff1a<\/p>\n
\r\nsomecmd >>logfile 2>&1\r\n<\/pre>\n\u66f4\u52a0\u8be6\u7ec6\u7684\u8bf4\u660e\u89c1BashFAQ\u3002<\/p>\n
43. cmd; (( ! $? )) || die<\/p>\n
\u53ea\u6709\u9700\u8981\u6355\u83b7\u4e0a\u4e00\u4e2a\u547d\u4ee4\u7684\u6267\u884c\u7ed3\u679c\u8fdb\uff0c\u624d\u9700\u8981\u8bb0\u5f55$?\u7684\u503c\uff0c\u5426\u5219\u5982\u679c\u4f60\u53ea\u9700\u8981\u68c0\u67e5\u4e0a\u4e00\u4e2a\u547d\u4ee4\u662f\u5426\u6267\u884c\u6210\u529f\uff0c\u76f4\u63a5\u68c0\u6d4b\u547d\u4ee4\uff1a<\/p>\n
\r\nif cmd; then\r\n ...\r\nfi\r\n\r\n\u6216\u8005\u4f7f\u7528 case \u8bed\u53e5\u6765\u68c0\u6d4b\u591a\u4e2a\u6216\u80fd\u7684\u8fd4\u56de\u7801\uff1a\r\n\r\ncmd\r\nstatus=$?\r\ncase $status in\r\n 0)\r\n echo success >&2\r\n ;;\r\n 1)\r\n echo 'Must supply a parameter, exiting.' >&2\r\n exit 1\r\n ;;\r\n *)\r\n echo 'Unknown error, exiting.' >&2\r\n exit $status\r\nesac\r\n<\/pre>\n\n\u539f\u6587\u6765\u81ea\uff1ahttp:\/\/www.linuxde.net\/2013\/08\/15075.html<\/a><\/p>\n