{"id":142,"date":"2026-08-05T15:58:52","date_gmt":"2026-08-05T07:58:52","guid":{"rendered":"https:\/\/www.wangguihui.com\/?p=142"},"modified":"2026-08-05T15:58:52","modified_gmt":"2026-08-05T07:58:52","slug":"shift%e5%91%bd%e4%bb%a4","status":"publish","type":"post","link":"https:\/\/www.wangguihui.com\/?p=142","title":{"rendered":"shift\u547d\u4ee4"},"content":{"rendered":"<p>\u5728 Linux Shell \u811a\u672c\u4e2d\uff0c<code>shift<\/code> \u662f\u4e00\u4e2a\u975e\u5e38\u5b9e\u7528\u4e14\u5185\u7f6e\u7684\u547d\u4ee4\u3002\u5b83\u7684\u4e3b\u8981\u4f5c\u7528\u662f<strong>\u5c06\u811a\u672c\u7684\u6240\u6709\u53c2\u6570\u5411\u5de6\u79fb\u52a8\u4e00\u4e2a\u4f4d\u7f6e<\/strong>\uff08\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff09\uff0c\u5e38\u7528\u4e8e\u5904\u7406\u672a\u77e5\u6570\u91cf\u6216\u53ef\u53d8\u6570\u91cf\u7684\u547d\u4ee4\u884c\u53c2\u6570\u3002<\/p>\n<h2 id=\"\u4e00\u3001-shift-\u547d\u4ee4\u7684\u6838\u5fc3\u539f\u7406\">\u4e00\u3001 <code>shift<\/code> \u547d\u4ee4\u7684\u6838\u5fc3\u539f\u7406<\/h2>\n<p>\u5f53\u6211\u4eec\u5728\u7ec8\u7aef\u8fd0\u884c\u4e00\u4e2a Shell \u811a\u672c\u5e76\u4f20\u5165\u53c2\u6570\u65f6\uff0c\u7cfb\u7edf\u4f1a\u5c06\u8fd9\u4e9b\u53c2\u6570\u4f9d\u6b21\u5b58\u50a8\u5728\u4f4d\u7f6e\u53d8\u91cf\u4e2d\uff1a<\/p>\n<ul>\n<li><code>$0<\/code>\uff1a\u4ee3\u8868\u811a\u672c\u672c\u8eab\u7684\u540d\u79f0\u3002<\/li>\n<li><code>$1<\/code>\uff1a\u4ee3\u8868\u7b2c\u4e00\u4e2a\u53c2\u6570\u3002<\/li>\n<li><code>$2<\/code>\uff1a\u4ee3\u8868\u7b2c\u4e8c\u4e2a\u53c2\u6570\u3002<\/li>\n<li>\u2026\u2026\u4ee5\u6b64\u7c7b\u63a8\u3002<\/li>\n<li><code>$#<\/code>\uff1a\u4ee3\u8868\u53c2\u6570\u7684\u603b\u4e2a\u6570\u3002<\/li>\n<li><code>$*<\/code> \u6216 <code>$@<\/code>\uff1a\u4ee3\u8868\u6240\u6709\u7684\u53c2\u6570\u3002<\/li>\n<\/ul>\n<p><strong>\u6267\u884c\u4e00\u6b21 <code>shift<\/code> \u540e\uff1a<\/strong><\/p>\n<ul>\n<li>\u539f\u6765\u7684 <code>$1<\/code> \u4f1a\u88ab\u4e22\u5f03\uff08\u9500\u6bc1\uff09\u3002<\/li>\n<li>\u539f\u6765\u7684 <code>$2<\/code> \u53d8\u6210 <code>$1<\/code>\u3002<\/li>\n<li>\u539f\u6765\u7684 <code>$3<\/code> \u53d8\u6210 <code>$2<\/code>\u3002<\/li>\n<li>\u2026\u2026\u4ee5\u6b64\u7c7b\u63a8\u3002<\/li>\n<li>\u53c2\u6570\u603b\u6570 <code>$#<\/code> \u7684\u503c\u4f1a\u81ea\u52a8\u51cf 1\u3002<\/li>\n<\/ul>\n<h2 id=\"\u4e8c\u3001-\u8bed\u6cd5\u683c\u5f0f\">\u4e8c\u3001 \u8bed\u6cd5\u683c\u5f0f<\/h2>\n<pre><code class=\"language-Bash\">shift [n]\r\n<\/code><\/pre>\n<ul>\n<li><strong><code>n<\/code>\uff08\u53ef\u9009\uff09<\/strong>\uff1a\u6307\u5b9a\u5411\u5de6\u79fb\u52a8\u7684\u4f4d\u6570\u3002\u5982\u679c\u4e0d\u586b\uff0c\u9ed8\u8ba4\u79fb\u52a8 <code>1<\/code> \u4f4d\u3002\u5982\u679c\u6307\u5b9a\u4e86 <code>n<\/code>\uff08\u4f8b\u5982 <code>shift 3<\/code>\uff09\uff0c\u5219\u53c2\u6570\u4f1a\u6574\u4f53\u5411\u5de6\u79fb\u52a8 3 \u4f4d\uff0c<code>$4<\/code> \u53d8\u6210 <code>$1<\/code>\uff0c\u524d 3 \u4e2a\u53c2\u6570\u88ab\u4e22\u5f03\u3002<\/li>\n<\/ul>\n<h2 id=\"\u4e09\u3001-\u5178\u578b\u5e94\u7528\u573a\u666f\">\u4e09\u3001 \u5178\u578b\u5e94\u7528\u573a\u666f<\/h2>\n<h3 id=\"\u573a\u666f-1\uff1a\u914d\u5408-while-\u5faa\u73af\u904d\u5386\u6240\u6709\u53c2\u6570\">\u573a\u666f 1\uff1a\u914d\u5408 <code>while<\/code> \u5faa\u73af\u904d\u5386\u6240\u6709\u53c2\u6570<\/h3>\n<p>\u5f53\u811a\u672c\u9700\u8981\u5904\u7406\u4e0d\u5b9a\u6570\u91cf\u7684\u53c2\u6570\u65f6\uff0c<code>shift<\/code> \u662f\u6700\u7ecf\u5178\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<pre><code class=\"language-bash\">#!\/bin\/bash\r\n\r\necho &quot;\u603b\u5171\u6709 $# \u4e2a\u53c2\u6570&quot;\r\n\r\nwhile [ $# -gt 0 ]; do\r\n    echo &quot;\u5f53\u524d\u5904\u7406\u7684\u53c2\u6570\u662f: $1&quot;\r\n    shift # \u5c06\u53c2\u6570\u5411\u5de6\u79fb\u4e00\u4f4d\uff0c\u4f7f\u4e0b\u4e00\u4e2a\u53c2\u6570\u53d8\u6210 $1\r\ndone\r\n<\/code><\/pre>\n<p><strong>\u8fd0\u884c\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre><code class=\"language-Bash\">$ .\/test.sh apple banana orange\r\n\u603b\u5171\u6709 3 \u4e2a\u53c2\u6570\r\n\u5f53\u524d\u5904\u7406\u7684\u53c2\u6570\u662f: apple\r\n\u5f53\u524d\u5904\u7406\u7684\u53c2\u6570\u662f: banana\r\n\u5f53\u524d\u5904\u7406\u7684\u53c2\u6570\u662f: orange\r\n<\/code><\/pre>\n<h3 id=\"\u573a\u666f-2\uff1a\u89e3\u6790\u5e26\u9009\u9879\uff08flagsoptions\uff09\u7684\u547d\u4ee4\u884c\u53c2\u6570\">\u573a\u666f 2\uff1a\u89e3\u6790\u5e26\u9009\u9879\uff08Flags\/Options\uff09\u7684\u547d\u4ee4\u884c\u53c2\u6570<\/h3>\n<p>\u5728\u7f16\u5199\u590d\u6742\u7684 Shell \u811a\u672c\u65f6\uff0c\u7ecf\u5e38\u9700\u8981\u5904\u7406\u7c7b\u4f3c <code>-f<\/code>\u3001<code>-v<\/code> \u8fd9\u6837\u7684\u9009\u9879\u3002<code>shift<\/code> \u53ef\u4ee5\u7528\u6765\u9010\u4e2a\u5265\u79bb\u9009\u9879\u548c\u53c2\u6570\u3002<\/p>\n<pre><code class=\"language-bash\">#!\/bin\/bash\r\n\r\n# \u521d\u59cb\u5316\u53d8\u91cf\r\nVERBOSE=0\r\nFILENAME=&quot;&quot;\r\n\r\nwhile [ &quot;$1&quot; != &quot;&quot; ]; do\r\n    case &quot;$1&quot; in\r\n        -v | --verbose)\r\n            VERBOSE=1\r\n            shift\r\n            ;;\r\n        -f | --file)\r\n            FILENAME=&quot;$2&quot;\r\n            shift 2 # \u9009\u9879\u540d\u548c\u6587\u4ef6\u540d\u5404\u5360\u4e00\u4e2a\u53c2\u6570\uff0c\u9700\u8981\u8fde\u7eed shift \u4e24\u6b21\r\n            ;;\r\n        *)\r\n            echo &quot;\u672a\u77e5\u53c2\u6570: $1&quot;\r\n            exit 1\r\n            ;;\r\n    esac\r\ndone\r\n\r\necho &quot;Verbose \u6a21\u5f0f: $VERBOSE&quot;\r\necho &quot;\u6587\u4ef6\u540d: $FILENAME&quot;\r\n<\/code><\/pre>\n<p><strong>\u8fd0\u884c\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre><code class=\"language-bash\">$ .\/script.sh -v -f data.txt\r\nVerbose \u6a21\u5f0f: 1\r\n\u6587\u4ef6\u540d: data.txt\r\n<\/code><\/pre>\n<blockquote>\n<p><strong>\u89e3\u6790\uff1a<\/strong><\/p>\n<ol>\n<li>\u9047\u5230 <code>-v<\/code> \u65f6\uff0c\u5339\u914d\u6210\u529f\uff0c\u6267\u884c <code>shift<\/code>\uff0c\u628a <code>-v<\/code> \u79fb\u51fa\uff0c\u5269\u4e0b <code>-f data.txt<\/code>\u3002<\/li>\n<li>\u9047\u5230 <code>-f<\/code> \u65f6\uff0c\u5339\u914d\u6210\u529f\uff0c\u7528 <code>$2<\/code> \u83b7\u53d6\u5230\u5b83\u7684\u503c <code>data.txt<\/code>\uff0c\u7136\u540e\u6267\u884c <code>shift 2<\/code>\uff0c\u628a <code>-f<\/code> \u548c <code>data.txt<\/code> \u540c\u65f6\u79fb\u51fa\uff0c\u5faa\u73af\u7ed3\u675f\u3002<\/li>\n<\/ol>\n<\/blockquote>\n<h2 id=\"\u56db\u3001-\u6ce8\u610f\u4e8b\u9879\">\u56db\u3001 \u6ce8\u610f\u4e8b\u9879<\/h2>\n<ol>\n<li><strong>\u65e0\u6cd5\u5411\u53f3\u79fb\u52a8<\/strong>\uff1a<code>shift<\/code> \u7684\u53c2\u6570\u53ea\u80fd\u662f\u6b63\u6574\u6570\uff0c\u4e0d\u652f\u6301\u8d1f\u6570\uff0c\u56e0\u6b64\u65e0\u6cd5\u5b9e\u73b0\u5411\u53f3\u79fb\u56de\u5df2\u88ab\u4e22\u5f03\u7684\u53c2\u6570\u3002<\/li>\n<li><strong>\u8d85\u51fa\u8303\u56f4\u4f1a\u62a5\u9519<\/strong>\uff1a\u5982\u679c\u6307\u5b9a\u7684 <code>n<\/code> \u5927\u4e8e\u5f53\u524d\u5269\u4f59\u7684\u53c2\u6570\u603b\u6570\uff08\u4f8b\u5982\u53ea\u5269 1 \u4e2a\u53c2\u6570\uff0c\u5374\u6267\u884c\u4e86 <code>shift 2<\/code>\uff09\uff0c\u811a\u672c\u4f1a\u62a5\u9519\uff08\u5982 <code>shift: 2: can&#39;t shift that many<\/code>\uff09\uff0c\u5e76\u53ef\u80fd\u5bfc\u81f4\u975e\u9884\u671f\u9000\u51fa\u3002<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Linux Shell \u811a\u672c\u4e2d\uff0cshift \u662f\u4e00\u4e2a\u975e\u5e38\u5b9e\u7528\u4e14\u5185\u7f6e\u7684\u547d\u4ee4\u3002\u5b83\u7684\u4e3b\u8981\u4f5c\u7528\u662f\u5c06\u811a\u672c\u7684\u6240\u6709\u53c2\u6570\u5411\u5de6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[33,26],"tags":[3,9],"class_list":["post-142","post","type-post","status-publish","format-standard","hentry","category-linux","category-26","tag-linux","tag-shell"],"_links":{"self":[{"href":"https:\/\/www.wangguihui.com\/index.php?rest_route=\/wp\/v2\/posts\/142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wangguihui.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wangguihui.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wangguihui.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wangguihui.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=142"}],"version-history":[{"count":1,"href":"https:\/\/www.wangguihui.com\/index.php?rest_route=\/wp\/v2\/posts\/142\/revisions"}],"predecessor-version":[{"id":143,"href":"https:\/\/www.wangguihui.com\/index.php?rest_route=\/wp\/v2\/posts\/142\/revisions\/143"}],"wp:attachment":[{"href":"https:\/\/www.wangguihui.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wangguihui.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wangguihui.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}