{"id":154678,"date":"2019-09-19T08:00:18","date_gmt":"2019-09-19T00:00:18","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=154678"},"modified":"2019-08-29T15:51:06","modified_gmt":"2019-08-29T07:51:06","slug":"linux-bash-add","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/linux-bash-add.html","title":{"rendered":"\u5982\u4f55\u5728 Bash \u4e2d\u5bf9\u53d8\u91cf\u9012\u589e\u6216\u8005\u9012\u51cf"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u7f16\u5199 Bash \u811a\u672c\u65f6\u6700\u5e38\u89c1\u7684\u7b97\u672f\u8fd0\u7b97\u4e4b\u4e00\u662f\u9012\u589e\u548c\u9012\u51cf\u53d8\u91cf\u3002\u8fd9\u901a\u5e38\u5728\u5faa\u73af\u4e2d\u7528\u4f5c\u8ba1\u6570\u5668\uff0c\u4f46\u5b83\u4e5f\u53ef\u4ee5\u5728\u811a\u672c\u7684\u5176\u4ed6\u5730\u65b9\u51fa\u73b0\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u9012\u589e\u548c\u9012\u51cf\u610f\u5473\u7740\u5206\u522b\u4ece\u6570\u503c\u53d8\u91cf\u7684\u503c\u4e2d\u6dfb\u52a0\u6216\u51cf\u53bb\u503c(\u901a\u5e38\u4e3a 1 )\u3002\u53ef\u4ee5\u4f7f\u7528\u53cc\u62ec\u53f7 ((...)) \u548c $((...)) \u6216\u4f7f\u7528\u5185\u7f6e\u7684 let \u547d\u4ee4\u6267\u884c\u7b97\u672f\u6269\u5c55\u3002<\/p>\n

\u5728 Bash \u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u52a0\/\u51cf\u53d8\u91cf\u3002\u672c\u6587\u89e3\u91ca\u4e86\u5176\u4e2d\u7684\u4e00\u4e9b\u5185\u5bb9\u3002<\/p>\n

\u4f7f\u7528 + \u548c - \u64cd\u4f5c\u5458<\/strong><\/div>\n

\u52a0\/\u51cf\u53d8\u91cf\u7684\u6700\u7b80\u5355\u65b9\u6cd5\u662f\u4f7f\u7528 + \u548c - \u8fd0\u7b97\u7b26\u3002<\/p>\n

\r\ni=$((i+1))\r\n((i=i+1))\r\nlet \"i=i+1\"\r\n\r\ni=$((i-1))\r\n((i=i-1))\r\nlet \"i=i-1\"\r\n<\/pre>\n

\u6b64\u65b9\u6cd5\u5141\u8bb8\u60a8\u5c06\u53d8\u91cf\u9012\u589e\/\u9012\u51cf\u6240\u9700\u7684\u4efb\u4f55\u503c\u3002<\/p>\n

\u4ee5\u4e0b\u662f\u5728 until \u5faa\u73af\u4e2d\u9012\u589e\u53d8\u91cf\u7684\u793a\u4f8b\uff1a<\/p>\n

\r\ni=0\r\n\r\nuntil [ $i -gt 3 ]\r\ndo\r\n  echo i: $i\r\n  ((i=i+1))\r\ndone\r\n\r\ni: 0\r\ni: 1\r\ni: 2\r\ni: 3\r\n<\/pre>\n
\u4f7f\u7528 += \u4e0e -= \u8fd0\u7b97\u7b26<\/strong><\/div>\n

\u9664\u4e86\u4e0a\u9762\u89e3\u91ca\u7684\u57fa\u672c\u8fd0\u7b97\u7b26\u4e4b\u5916\uff0c bash \u8fd8\u63d0\u4f9b\u8d4b\u503c\u8fd0\u7b97\u7b26 += \u548c -= \u3002\u8fd9\u4e9b\u8fd0\u7b97\u7b26\u7528\u4e8e\u4f7f\u7528\u8fd0\u7b97\u7b26\u540e\u6307\u5b9a\u7684\u503c\u9012\u589e\/\u9012\u51cf\u5de6\u64cd\u4f5c\u6570\u7684\u503c\u3002<\/p>\n

\r\n((i+=1))\r\nlet \"i+=1\" \r\n\r\n((i-=1))\r\nlet \"i-=1\" \r\n<\/pre>\n

\u5728\u63a5\u4e0b\u6765\u7684 while \u5faa\u73af\u4e2d\uff0c\u6211\u4eec\u5c06 i \u53d8\u91cf\u7684\u503c\u9012\u51cf 5 \u3002<\/p>\n

\r\ni=20\r\n\r\nwhile [ $i -ge 5 ]\r\ndo\r\n  echo Number: $i\r\n  let \"i-=5\" \r\ndone\r\n\r\nNumber: 20\r\nNumber: 15\r\nNumber: 10\r\nNumber: 5\r\n<\/pre>\n
\u4f7f\u7528 ++ \u548c -- \u8fd0\u7b97\u7b26<\/strong><\/div>\n

\u5728 ++ \u548c -- \u5206\u522b\u8fd0\u8425\u5546\u589e\u91cf\u548c\u51cf\u91cf\uff0c\u5176\u64cd\u4f5c 1 \u548c\u8fd4\u56de\u503c\u3002<\/p>\n

\r\n((i++))\r\n((++i))\r\nlet \"i++\"\r\nlet \"++i\"\r\n\r\n((i--))\r\n((--i))\r\nlet \"i--\"\r\nlet \"--i\"\r\n<\/pre>\n

\u53ef\u4ee5\u5728\u64cd\u4f5c\u6570\u4e4b\u524d\u6216\u4e4b\u540e\u4f7f\u7528\u8fd0\u7b97\u7b26\u3002\u5b83\u4eec\u4e5f\u88ab\u79f0\u4e3a\uff1a
\n\u2022\u524d\u7f00\u589e\u91cf\uff1a ++i
\n\u2022\u524d\u7f00\u51cf\u91cf\uff1a --i
\n\u2022\u540e\u7f00\u589e\u91cf\uff1a i++
\n\u2022\u540e\u7f00\u51cf\u91cf\uff1a i--<\/p>\n

\u524d\u7f00\u8fd0\u7b97\u7b26\u9996\u5148\u8ba9\u53d8\u91cf\u52a0\/\u51cf 1 \uff0c\u7136\u540e\u8fd4\u56de\u53d8\u91cf\u7684\u65b0\u503c\u3002\u540e\u7f00\u8fd0\u7b97\u7b26\u5728\u53d8\u91cf\u52a0\/\u51cf\u4e4b\u524d\u8fd4\u56de\u8fd0\u7b97\u7b26\u7684\u503c\u3002<\/p>\n

\u5982\u679c\u60a8\u53ea\u60f3\u589e\u52a0\/\u51cf\u5c11\u53d8\u91cf\uff0c\u90a3\u4e48\u5982\u679c\u60a8\u4f7f\u7528\u524d\u7f00\u6216\u540e\u7f00\u8fd0\u7b97\u7b26\u5219\u6ca1\u6709\u533a\u522b\u3002\u53ea\u6709\u5728\u67d0\u4e9b\u5176\u4ed6\u64cd\u4f5c\u4e2d\u4f7f\u7528\u8fd0\u7b97\u7b26\u7684\u7ed3\u679c\u6216\u5c06\u5176\u5206\u914d\u7ed9\u53e6\u4e00\u4e2a\u53d8\u91cf\u65f6\uff0c\u5b83\u624d\u4f1a\u6709\u6240\u4e0d\u540c\u3002<\/p>\n

\u4ee5\u4e0b\u793a\u4f8b\u6f14\u793a\u4e86 ++ \u64cd\u4f5c\u7b26\u5728\u5176\u64cd\u4f5c\u7b26\u4e4b\u524d\u548c\u4e4b\u540e\u4f7f\u7528\u65f6\u7684\u5de5\u4f5c\u65b9\u5f0f\uff1a<\/p>\n

\r\nx=5\r\ny=$((x++))\r\necho x: $x\r\necho y: $y\r\n\r\nx: 6\r\ny: 5\r\n\r\nx=5\r\ny=$((++x))\r\necho x: $x\r\necho y: $y\r\n\r\nx: 6\r\ny: 6\r\n<\/pre>\n

\u4e0b\u9762\u662f\u5728 bash \u811a\u672c\u4e2d\u4f7f\u7528\u7684\u793a\u4f8b\uff1a<\/p>\n

\r\n#!\/bin\/bash\r\ni=0\r\nwhile true; do\r\n  if [[ \"$i\" -gt 3 ]]; then\r\n       exit 1\r\n  fi\r\n  echo i: $i\r\n  ((i++))\r\ndone\r\n<\/pre>\n

\u4f7f\u7528\u8fd9\u4e9b\u8fd0\u7b97\u7b26\u7684\u7f3a\u70b9\u662f\u53d8\u91cf\u53ea\u80fd\u9012\u589e\u6216\u9012\u51cf 1 \u3002<\/p>\n

\u7ed3\u8bba<\/strong><\/div>\n

Bash \u4e2d\u7684\u53d8\u91cf\u7684\u9012\u589e\u548c\u9012\u51cf\u53ef\u4ee5\u4ee5\u8bb8\u591a\u4e0d\u540c\u7684\u65b9\u5f0f\u6267\u884c\u3002\u65e0\u8bba\u4f7f\u7528\u4f55\u79cd\u65b9\u6cd5\uff0c\u7ed3\u679c\u90fd\u662f\u4e00\u6837\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u9012\u589e\u548c\u9012\u51cf\u610f\u5473\u7740\u5206\u522b\u4ece\u6570\u503c\u53d8\u91cf\u7684\u503c\u4e2d\u6dfb\u52a0\u6216\u51cf\u53bb\u503c(\u901a\u5e38\u4e3a 1 )\u3002\u53ef\u4ee5\u4f7f\u7528\u53cc\u62ec\u53f7 ((…)) \u548c $((. […]<\/p>\n","protected":false},"author":63,"featured_media":154695,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-154678","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/154678","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=154678"}],"version-history":[{"count":7,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/154678\/revisions"}],"predecessor-version":[{"id":157516,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/154678\/revisions\/157516"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/154695"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=154678"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=154678"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=154678"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}