{"id":588,"date":"2019-08-06T21:00:15","date_gmt":"2019-08-06T13:00:15","guid":{"rendered":"http:\/\/prayerlaputa.com\/?p=588"},"modified":"2019-08-11T15:12:58","modified_gmt":"2019-08-11T07:12:58","slug":"shell%e4%b8%ad%e6%a0%b9%e6%8d%ae%e6%8c%87%e5%ae%9a%e5%88%86%e9%9a%94%e7%ac%a6%e5%b0%86%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%8b%86%e5%88%86%e6%88%90%e6%95%b0%e7%bb%84%ef%bc%8c%e7%84%b6%e5%90%8e%e9%81%8d","status":"publish","type":"post","link":"http:\/\/prayerlaputa.com\/?p=588","title":{"rendered":"shell\u4e2d\u6839\u636e\u6307\u5b9a\u5206\u9694\u7b26\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u6210\u6570\u7ec4\uff0c\u7136\u540e\u904d\u5386\u8be5\u6570\u7ec4"},"content":{"rendered":"<h1>\u76ee\u6807<\/h1>\n<div class=\"mume markdown-preview \">\n<p>shell\u4e2d\u6839\u636e\u6307\u5b9a\u5206\u9694\u7b26\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u6210\u6570\u7ec4\uff0c\u7136\u540e\u904d\u5386\u8be5\u6570\u7ec4<\/p>\n<h1 id=\"%E7%9F%A5%E8%AF%86%E7%82%B9\" class=\"mume-header\">\u77e5\u8bc6\u70b9<\/h1>\n<h2 id=\"shell%E4%B8%AD%E7%9A%84ifs\" class=\"mume-header\">shell\u4e2d\u7684$IFS<\/h2>\n<p><!--more--><\/p>\n<p>Shell \u811a\u672c\u4e2d\u6709\u4e2a\u53d8\u91cf\u53ebIFS(Internal Field Seprator) \uff0c\u5185\u90e8\u57df\u5206\u9694\u7b26\u3002\u5b8c\u6574\u5b9a\u4e49\u662fThe shell uses the value stored in IFS, which is the space, tab, and newline characters by default, to delimit words for the read and set commands, when parsing output from command substitution, and when performing variable substitution.<br \/>\nShell \u7684\u73af\u5883\u53d8\u91cf\u5206\u4e3aset, env \u4e24\u79cd\uff0c\u5176\u4e2d set \u53d8\u91cf\u53ef\u4ee5\u901a\u8fc7 export \u5de5\u5177\u5bfc\u5165\u5230 env \u53d8\u91cf\u4e2d\u3002\u5176\u4e2d\uff0cset \u662f\u663e\u793a\u8bbe\u7f6eshell\u53d8\u91cf\uff0c\u4ec5\u5728\u672c shell \u4e2d\u6709\u6548\uff1benv \u662f\u663e\u793a\u8bbe\u7f6e\u7528\u6237\u73af\u5883\u53d8\u91cf \uff0c\u4ec5\u5728\u5f53\u524d\u4f1a\u8bdd\u4e2d\u6709\u6548\u3002\u6362\u53e5\u8bdd\u8bf4\uff0cset \u53d8\u91cf\u91cc\u5305\u542b\u4e86env \u53d8\u91cf\uff0c\u4f46set\u53d8\u91cf\u4e0d\u4e00\u5b9a\u90fd\u662fenv \u53d8\u91cf\u3002\u8fd9\u4e24\u79cd\u53d8\u91cf\u4e0d\u540c\u4e4b\u5904\u5728\u4e8e\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u4e0d\u540c\u3002\u663e\u7136\uff0cenv \u53d8\u91cf\u7684\u4f5c\u7528\u57df\u8981\u5927\u4e9b\uff0c\u5b83\u53ef\u4ee5\u5728 subshell \u4e2d\u4f7f\u7528\u3002<br \/>\nIFS \u662f\u4e00\u79cd set \u53d8\u91cf\uff0c\u5f53 shell \u5904\u7406&#8221;\u547d\u4ee4\u66ff\u6362&#8221;\u548c&#8221;\u53c2\u6570\u66ff\u6362&#8221;\u65f6\uff0cshell \u6839\u636e IFS \u7684\u503c\uff0c\u9ed8\u8ba4\u662f space, tab, newline \u6765\u62c6\u89e3\u8bfb\u5165\u7684\u53d8\u91cf\uff0c\u7136\u540e\u5bf9\u7279\u6b8a\u5b57\u7b26\u8fdb\u884c\u5904\u7406\uff0c\u6700\u540e\u91cd\u65b0\u7ec4\u5408\u8d4b\u503c\u7ed9\u8be5\u53d8\u91cf\u3002<\/p>\n<h3 id=\"ifs%E7%9A%84%E4%BD%BF%E7%94%A8\" class=\"mume-header\">$IFS\u7684\u4f7f\u7528<\/h3>\n<ol>\n<li>\u67e5\u770b$IFS\u7684\u503c<\/li>\n<\/ol>\n<ul>\n<li>echo &#8220;$IFS&#8221;<br \/>\n\u4e0d\u4f1a\u663e\u793a\u4efb\u4f55\u5185\u5bb9<\/li>\n<li>echo &#8220;$IFS&#8221;|od -b<br \/>\n\u5c55\u793a\u5185\u5bb9\u5982\u4e0b<\/p>\n<pre class=\"language-\" data-role=\"codeBlock\" data-info=\"\"><code>0000000 040 011 012 012\r\n0000004\r\n<\/code><\/pre>\n<p>\u76f4\u63a5\u8f93\u51faIFS\u662f\u770b\u4e0d\u5230\u503c\u7684\uff0c\u8f6c\u5316\u4e3a\u4e8c\u8fdb\u5236\u5c31\u53ef\u4ee5\u770b\u5230\u4e86\uff0c&#8221;040&#8243;\u662f\u7a7a\u683c\uff0c&#8221;011&#8243;\u662fTab\uff0c&#8221;012&#8243;\u662f\u6362\u884c\u7b26&#8221;\\n&#8221; \u3002\u6700\u540e\u4e00\u4e2a 012 \u662f\u56e0\u4e3a echo \u9ed8\u8ba4\u662f\u4f1a\u6362\u884c\u7684\u3002<\/li>\n<\/ul>\n<ol start=\"2\">\n<li>\u5b9e\u9645\u5e94\u7528<\/li>\n<\/ol>\n<pre class=\"language-\" data-role=\"codeBlock\" data-info=\"\"><code>#!\/bin\/bash\r\nOLD_IFS=$IFS #\u4fdd\u5b58\u539f\u59cb\u503c\r\nIFS=\"\" #\u6539\u53d8IFS\u7684\u503c\r\n...\r\n...\r\nIFS=$OLD_IFS #\u8fd8\u539fIFS\u7684\u539f\u59cb\u503c\r\n<\/code><\/pre>\n<h2 id=\"shell%E5%BE%AA%E7%8E%AF%E5%A4%84%E7%90%86\" class=\"mume-header\">shell\u5faa\u73af\u5904\u7406<\/h2>\n<pre class=\"language-\" data-role=\"codeBlock\" data-info=\"\"><code>#!\/bin\/bash\r\na=\"hello,world,nice,to,meet,you\"\r\n#\u8981\u5c06$a\u5206\u5272\u5f00\uff0c\u5148\u5b58\u50a8\u65e7\u7684\u5206\u9694\u7b26\r\nOLD_IFS=\"$IFS\"\r\n\r\n#\u8bbe\u7f6e\u5206\u9694\u7b26\r\nIFS=\",\" \r\n\r\n#\u5982\u4e0b\u4f1a\u81ea\u52a8\u5206\u9694\r\narr=($a)\r\n\r\n#\u6062\u590d\u539f\u6765\u7684\u5206\u9694\u7b26\r\nIFS=\"$OLD_IFS\"\r\n\r\n#\u904d\u5386\u6570\u7ec4\r\nfor s in ${arr[@]}\r\ndo\r\necho \"$s\"\r\ndone\r\n<\/code><\/pre>\n<p>\u53d8\u91cf<\/p>\n<p>IFS\u5b58\u50a8\u7740\u5206\u9694\u7b26\uff0c\u8fd9\u91cc\u6211\u4eec\u5c06\u5176\u8bbe\u4e3a\u9017\u53f7&#8221;,&#8221;OLDIFS\u7528\u4e8e\u5907\u4efd\u9ed8\u8ba4\u7684\u5206\u9694\u7b26\uff0c\u4f7f\u7528\u5b8c\u540e\u5c06\u4e4b\u6062\u590d\u9ed8\u8ba4\u3002arr=(IFS\u5b58\u50a8\u7740\u5206\u9694\u7b26\uff0c\u8fd9\u91cc\u6211\u4eec\u5c06\u5176\u8bbe\u4e3a\u9017\u53f7 &#8220;,&#8221; OLD_IFS\u7528\u4e8e\u5907\u4efd\u9ed8\u8ba4\u7684\u5206\u9694\u7b26\uff0c\u4f7f\u7528\u5b8c\u540e\u5c06\u4e4b\u6062\u590d\u9ed8\u8ba4\u3002<br \/>\narr=(<span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"mord mathdefault\" style=\"margin-right: 0.07847em;\">I<\/span><span class=\"mord mathdefault\" style=\"margin-right: 0.13889em;\">F<\/span><span class=\"mord mathdefault\" style=\"margin-right: 0.05764em;\">S<\/span><span class=\"mord cjk_fallback\">\u5b58<\/span><span class=\"mord cjk_fallback\">\u50a8<\/span><span class=\"mord cjk_fallback\">\u7740<\/span><span class=\"mord cjk_fallback\">\u5206<\/span><span class=\"mord cjk_fallback\">\u9694<\/span><span class=\"mord cjk_fallback\">\u7b26<\/span><span class=\"mord cjk_fallback\">\uff0c<\/span><span class=\"mord cjk_fallback\">\u8fd9<\/span><span class=\"mord cjk_fallback\">\u91cc<\/span><span class=\"mord cjk_fallback\">\u6211<\/span><span class=\"mord cjk_fallback\">\u4eec<\/span><span class=\"mord cjk_fallback\">\u5c06<\/span><span class=\"mord cjk_fallback\">\u5176<\/span><span class=\"mord cjk_fallback\">\u8bbe<\/span><span class=\"mord cjk_fallback\">\u4e3a<\/span><span class=\"mord cjk_fallback\">\u9017<\/span><span class=\"mord cjk_fallback\">\u53f7<\/span><span class=\"mord\">&#8220;<\/span><span class=\"mpunct\">,<\/span><span class=\"mord\">&#8220;<\/span><span class=\"mord mathdefault\" style=\"margin-right: 0.02778em;\">O<\/span><span class=\"mord mathdefault\">L<\/span><span class=\"mord\"><span class=\"mord mathdefault\" style=\"margin-right: 0.02778em;\">D<\/span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height: 0.32833099999999993em;\"><span style=\"top: -2.5500000000000003em; margin-left: -0.02778em; margin-right: 0.05em;\"><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mathdefault mtight\" style=\"margin-right: 0.07847em;\">I<\/span><\/span><\/span><\/span><span class=\"vlist-s\">\u200b<\/span><\/span><\/span><\/span><\/span><span class=\"mord mathdefault\" style=\"margin-right: 0.13889em;\">F<\/span><span class=\"mord mathdefault\" style=\"margin-right: 0.05764em;\">S<\/span><span class=\"mord cjk_fallback\">\u7528<\/span><span class=\"mord cjk_fallback\">\u4e8e<\/span><span class=\"mord cjk_fallback\">\u5907<\/span><span class=\"mord cjk_fallback\">\u4efd<\/span><span class=\"mord cjk_fallback\">\u9ed8<\/span><span class=\"mord cjk_fallback\">\u8ba4<\/span><span class=\"mord cjk_fallback\">\u7684<\/span><span class=\"mord cjk_fallback\">\u5206<\/span><span class=\"mord cjk_fallback\">\u9694<\/span><span class=\"mord cjk_fallback\">\u7b26<\/span><span class=\"mord cjk_fallback\">\uff0c<\/span><span class=\"mord cjk_fallback\">\u4f7f<\/span><span class=\"mord cjk_fallback\">\u7528<\/span><span class=\"mord cjk_fallback\">\u5b8c<\/span><span class=\"mord cjk_fallback\">\u540e<\/span><span class=\"mord cjk_fallback\">\u5c06<\/span><span class=\"mord cjk_fallback\">\u4e4b<\/span><span class=\"mord cjk_fallback\">\u6062<\/span><span class=\"mord cjk_fallback\">\u590d<\/span><span class=\"mord cjk_fallback\">\u9ed8<\/span><span class=\"mord cjk_fallback\">\u8ba4<\/span><span class=\"mord cjk_fallback\">\u3002<\/span><span class=\"mord mathdefault\">a<\/span><span class=\"mord mathdefault\" style=\"margin-right: 0.02778em;\">r<\/span><span class=\"mord mathdefault\" style=\"margin-right: 0.02778em;\">r<\/span><span class=\"mrel\">=<\/span><\/span><span class=\"base\"><span class=\"mopen\">(<\/span><\/span><\/span>a)\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32<\/p>\n<p>a\u6309IFS\u5206\u9694\u7b26\u5206\u5272\u5230\u6570\u7ec4a\u6309IFS\u5206\u9694\u7b26\u5206\u5272\u5230\u6570\u7ec4<span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"mord mathdefault\">a<\/span><span class=\"mord cjk_fallback\">\u6309<\/span><span class=\"mord mathdefault\" style=\"margin-right: 0.07847em;\">I<\/span><span class=\"mord mathdefault\" style=\"margin-right: 0.13889em;\">F<\/span><span class=\"mord mathdefault\" style=\"margin-right: 0.05764em;\">S<\/span><span class=\"mord cjk_fallback\">\u5206<\/span><span class=\"mord cjk_fallback\">\u9694<\/span><span class=\"mord cjk_fallback\">\u7b26<\/span><span class=\"mord cjk_fallback\">\u5206<\/span><span class=\"mord cjk_fallback\">\u5272<\/span><span class=\"mord cjk_fallback\">\u5230<\/span><span class=\"mord cjk_fallback\">\u6570<\/span><span class=\"mord cjk_fallback\">\u7ec4<\/span><\/span><\/span>arr<\/p>\n<p>arr[0]{arr[0]}<span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"mord\"><span class=\"mord mathdefault\">a<\/span><span class=\"mord mathdefault\" style=\"margin-right: 0.02778em;\">r<\/span><span class=\"mord mathdefault\" style=\"margin-right: 0.02778em;\">r<\/span><span class=\"mopen\">[<\/span><span class=\"mord\">0<\/span><span class=\"mclose\">]<\/span><\/span><\/span><\/span>{arr[1]} &#8230; \u5206\u522b\u5b58\u50a8\u5206\u5272\u540e\u7684\u6570\u7ec4\u7b2c1 2 &#8230; \u9879<\/p>\n<p>arr[@]\u5b58\u50a8\u6574\u4e2a\u6570\u7ec4\u3002{arr[@]}\u5b58\u50a8\u6574\u4e2a\u6570\u7ec4\u3002<span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"mord\"><span class=\"mord mathdefault\">a<\/span><span class=\"mord mathdefault\" style=\"margin-right: 0.02778em;\">r<\/span><span class=\"mord mathdefault\" style=\"margin-right: 0.02778em;\">r<\/span><span class=\"mopen\">[<\/span><span class=\"mord\">@<\/span><span class=\"mclose\">]<\/span><\/span><span class=\"mord cjk_fallback\">\u5b58<\/span><span class=\"mord cjk_fallback\">\u50a8<\/span><span class=\"mord cjk_fallback\">\u6574<\/span><span class=\"mord cjk_fallback\">\u4e2a<\/span><span class=\"mord cjk_fallback\">\u6570<\/span><span class=\"mord cjk_fallback\">\u7ec4<\/span><span class=\"mord cjk_fallback\">\u3002<\/span><\/span><\/span>{!arr[@]}\u5b58\u50a8\u6574\u4e2a\u7d22\u5f15\u503c\uff1a1 2 3 4 &#8230;<br \/>\n${#arr[@]} \u83b7\u53d6\u6570\u7ec4\u7684\u957f\u5ea6\u3002<\/p>\n<h1 id=\"%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99\" class=\"mume-header\">\u53c2\u8003\u8d44\u6599<\/h1>\n<ul>\n<li><a href=\"https:\/\/www.cnblogs.com\/zejin2008\/p\/7683357.html\">shell\u5c06\u5b57\u7b26\u4e32\u5206\u9694\u6210\u6570\u7ec4<\/a><\/li>\n<li><a href=\"https:\/\/www.cnblogs.com\/fjping0606\/p\/4573536.html\">Shell\u4e2dIFS\u7528\u6cd5<\/a><\/li>\n<\/ul>\n<\/div>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u76ee\u6807 shell\u4e2d\u6839\u636e\u6307\u5b9a\u5206\u9694\u7b26\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u6210\u6570\u7ec4\uff0c\u7136\u540e\u904d\u5386\u8be5\u6570\u7ec4 \u77e5\u8bc6\u70b9 shell\u4e2d\u7684$IFS<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[7,68,67],"tags":[34,70,69],"_links":{"self":[{"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/posts\/588"}],"collection":[{"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=588"}],"version-history":[{"count":9,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/posts\/588\/revisions"}],"predecessor-version":[{"id":602,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/posts\/588\/revisions\/602"}],"wp:attachment":[{"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=588"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=588"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}