{"id":290843,"date":"2024-04-13T21:25:37","date_gmt":"2024-04-13T13:25:37","guid":{"rendered":"https:\/\/lrxjmw.cn\/lsjjmwyiyjdj.html"},"modified":"2024-04-13T21:25:37","modified_gmt":"2024-04-13T13:25:37","slug":"lsjjmwyiyjdj","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/lsjjmwyiyjdj.html","title":{"rendered":"Linux\u795e\u6280\u89e3\u5bc6\uff1awhile\u4e0eif\u8bed\u53e5\u5927\u63ed\u79d8"},"content":{"rendered":"

1.Linux<\/a>\u57fa\u7840\u6982\u5ff5<\/b><\/p>\n

Linux\u4f5c\u4e3a\u5f00\u6e90\u64cd\u4f5c\u7cfb\u7edf<\/a>\uff0c\u56e0\u5176\u7a33\u5b9a\u3001\u5b89\u5168\u4e14\u81ea\u7531\u5ea6\u9ad8\u7b49\u7279\u6027\uff0c\u88ab\u5e7f\u6cdb\u8fd0\u7528\u4e8e\u670d\u52a1\u5668\u53ca\u5d4c\u5165\u5f0f\u8bbe\u5907\u7b49\u9886\u57df\u3002\u5176\u4e2d\uff0cwhile\u4e0eif\u8bed\u53e5\u4f5c\u4e3a\u5176\u7f16\u7a0b\u4e2d\u7684\u6838\u5fc3\u63a7\u5236\u6d41\u8bed\u53e5\uff0c\u53ef\u5b9e\u73b0\u5faa\u73af\u53ca\u6761\u4ef6\u5224\u65ad\uff0c\u4e3a\u7a0b\u5e8f\u8bbe\u8ba1\u63d0\u4f9b\u6709\u529b\u652f\u6491\u3002<\/p>\n

2.while\u5faa\u73af\u8bed\u53e5<\/b><\/p>\n

While\u4f5c\u4e3a\u8fed\u4ee3\u63a7\u5236\u7ed3\u6784\uff0c\u65e8\u5728\u6301\u7eed\u8fd0\u884c\u67d0\u6bb5\u4ee3\u7801\uff0c\u76f4\u81f3\u9884\u8bbe\u6761\u4ef6\u4e0d\u518d\u6210\u7acb\u3002\u5728Linux\u64cd\u4f5c\u7cfb\u7edf\u4e2dlinux while if<\/strong>\uff0c\u5e38\u89c1\u7684while\u5faa\u73af\u5f62\u6001\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

\n

bash\nwhile[condition]\ndo\ncommand1\ncommand2\n...\ndone\n<\/pre>\n<\/p>\n

\u5728\u6b64\u4f8b\u4e2d\uff0c\"condition\"\u4f5c\u4e3a\u4e00\u4e2a\u8868\u8fbe\u5f0f\u6216\u547d\u4ee4<\/a>\uff0c\u82e5\u5176\u8fd4\u56de\u503c\u4e3a\u771f\uff08\u5373\u7b49\u4e8e0\uff09\uff0c\u5219\u5faa\u73af\u4f53\u5185\u7684\u6307\u4ee4\u5c06\u6301\u7eed\u6267\u884c\uff1b\u501f\u52a9\u4e8e\"while\"\u5faa\u73af\uff0c\u4fbf\u53ef\u8f7b\u6613\u8fbe\u5230\u5728\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u65f6\u505c\u6b62\u6267\u884c\u7684\u6548\u679c\u3002<\/p>\n

\u5728\u5b9e\u8df5\u4e2d\uff0cWhile\u5faa\u73af\u5e38\u88ab\u8fd0\u7528\u4e8e\u6587\u4ef6\u9010\u884c\u8bfb\u53d6\u53ca\u76ee\u5f55\u68c0\u7d22\u7b49\u9886\u57df\u3002\u901a\u8fc7\u7cbe\u786e\u7ed3\u5408\u591a\u79cd\u6761\u4ef6\u5224\u65ad\u4e0e\u903b\u8f91\u64cd\u4f5c\uff0c\u53ef\u6253\u9020\u51fa\u66f4\u4e3a\u7cbe\u7ec6\u4e14\u9ad8\u6548\u7684\u5faa\u73af\u673a\u5236\u3002<\/p>\n

3.if\u6761\u4ef6\u5224\u65ad\u8bed\u53e5<\/b><\/p>\n

IF\u8bed\u53e5\u4f5c\u4e3a\u6761\u4ef6\u5206\u652f\u63a7\u5236\u65b9\u5f0f\uff0c\u4f9d\u636e\u6307\u5b9a\u6761\u4ef6\u7684\u7ed3\u679c\u6765\u8fd0\u884c\u76f8\u5e94\u7684\u4ee3\u7801\u7247\u6bb5\u3002\u5728Linux\u64cd\u4f5c\u7cfb\u7edf\u73af\u5883\u4e0b\uff0c\u8fd9\u7c7b\u8bed\u53e5\u5e38\u5448\u73b0\u51fa\u5982\u4e0b\u7ec4\u7ec7\u683c\u5f0f\uff1a<\/p>\n

if[condition]<\/p>\n

then<\/p>\n

fi<\/p>\n

\u5728\u6b64\uff0ccondition\u4f5c\u4e3a\u4e00\u79cd\u8868\u793a\u5f0f\u6216\u6307\u4ee4\uff0c\u82e5\u8fd4\u56de\u7ed3\u679c\u4e3a\u771f\uff0c\u90a3\u4e48\u5c06\u6267\u884c\u7d27\u968f\u5176\u540e\u7684then\u6307\u4ee4\uff1b\u53cd\u4e4b\u5219\u76f4\u63a5\u8df3\u8fc7if\u90e8\u5206\u3002\u53ef\u53cd\u590d\u5d4c\u5957if\u8bed\u53e5\u5e76\u4e0eelse\u3001elif\u7b49\u5173\u952e\u5b57\u914d\u5408\u8fd0\u7528wps for linux\uff0c\u5b9e\u73b0\u591a\u79cd\u60c5\u51b5\u4e0b\u7684\u6761\u4ef6\u5224\u65ad\u3002<\/p>\n

\u501f\u52a9if\u8bed\u53e5linux while if<\/strong>\uff0c\u53ef\u4f9d\u4e0d\u540c\u60c5\u5883\u6267\u884c\u76f8\u5e94\u7f16\u7801\u903b\u8f91\u3002\u4f8b\u5982\uff0c\u5728Shell\u811a\u672c\u7f16\u5199\u65f6\u4f9d\u636e\u6587\u4ef6\u662f\u5426\u5b58\u5728\u505a\u51fa\u64cd\u4f5c\u5224\u65ad\uff0c\u6216\u5728\u7cfb\u7edf\u7ba1\u7406\u8fc7\u7a0b\u4e2d\u5229\u7528\u7528\u6237\u6743\u9650\u5b9e\u65bd\u8bbf\u95ee\u9650\u5236\u3002<\/p>\n

linux while if_linux while if_linux while if<\/p>\n

4.while\u4e0eif\u7684\u7ed3\u5408\u8fd0\u7528<\/b><\/p>\n

\u5b9e\u8df5\u7f16\u7a0b\u4e2d\uff0c\u5e38\u5c06while\u4e0eif\u76f8\u7ed3\u5408\uff0c\u5b9e\u73b0\u590d\u6742\u4e14\u7075\u6d3b\u7684\u903b\u8f91\u63a7\u5236\u3002\u4f8b\u5982\uff0c\u5728\u5904\u7406\u6570\u636e\u65f6\uff0c\u53ef\u91c7\u7528while\u5faa\u73af\u8bfb\u53d6\u6570\u636e\u6d41\uff0c\u518d\u501f\u52a9if\u5224\u65ad\u6311\u9009\u51fa\u6ee1\u8db3\u6761\u4ef6\u7684\u6570\u636e\u8fdb\u884c\u540e\u7eed\u64cd\u4f5c\u3002<\/p>\n

\u5728Shell\u811a\u672c\u7f16\u7a0b\u4e2d\uff0c\u5e38\u7528\u5230while\u5faa\u73af\u5185\u5d4cif\u8bed\u53e5\u4ee5\u5e94\u5bf9\u591a\u79cd\u60c5\u5f62\u3002\u8fd9\u5957\u878d\u5408\u65b9\u6848\uff0c\u65e2\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u6613\u61c2\u6027\u4e0e\u7ef4\u62a4\u6027\uff0c\u66f4\u51c6\u786e\u5730\u6ee1\u8db3\u4e86\u7a0b\u5e8f\u8bbe\u8ba1\u9700\u6c42\u3002<\/p>\n

5.\u5b9e\u4f8b\u6f14\u793a\uff1a\u7edf\u8ba1\u6587\u4ef6\u884c\u6570<\/b><\/p>\n

\u4e3a\u6df1\u5165\u89e3\u6790Linux\u4e2dwhile\u53caif\u8bed\u53e5\u7684\u8fd0\u7528\u65b9\u5f0f\uff0c\u672c\u6587\u901a\u8fc7\u5177\u4f53\u5b9e\u4f8b\u793a\u8303\u5982\u4f55\u5229\u7528\u8fd9\u4e24\u5927\u5173\u952e\u5b57\u7cbe\u786e\u7edf\u8ba1\u6587\u4ef6\u4e2d\u67d0\u7c7b\u5b57\u7b26\u4e32\u51fa\u73b0\u6b21\u6570\u3002<\/p>\n

\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3atest.txt\u7684\u6587\u672c\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n

apple<\/p>\n

linux while if_linux while if_linux while if<\/p>\n

banana<\/p>\n

orange<\/p>\n

\u6211\u4eec\u671f\u671b\u7cbe\u786e\u8ba1\u7b97\u51fa\u5305\u542b\"apple\"\u5173\u952e\u5b57\u7684\u884c\u6570\uff0c\u4ee5\u4e0b\u4e3a\u5b9e\u73b0\u6b64\u76ee\u7684\u7684Shell\u7a0b\u5e8f\u793a\u4f8b\uff1a<\/p>\n

#!\/bin\/bash<\/p>\n

count=0<\/p>\n

whilereadline<\/p>\n

if[[$line==*apple*]]<\/p>\n

linux while if_linux while if_linux while if<\/p>\n

then<\/p>\n

((count++))<\/p>\n

fi<\/p>\n

done<test.txt<\/p>\n

\u8f93\u51fa\uff1a\"\u5305\u542b'\u82f9\u679c'\u7684\u884c\u6570\u4e3a\uff1a$count\u3002\"<\/p>\n

\u501f\u52a9\u4e0a\u8ff0\u811a\u672c\uff0c\u91c7\u7528while\u5faa\u73af\u53ef\u9010\u884c\u89e3\u6790test.txt\u6587\u672c\uff0c\u4f9d\u9760if\u6761\u4ef6\u5224\u65ad\"apple\"\u5173\u952e\u5b57\u51fa\u73b0\u7684\u884c\u6b21\u3002\u6700\u540e\u7ed3\u679c\u663e\u793a\u4e3a\"\u542b\u6709'apple'\u7684\u884c\u6570\u5171\u8ba13\u884c\"\u3002<\/p>\n

6.\u8fdb\u9636\u5e94\u7528\uff1a\u76d1\u63a7\u7cfb\u7edf\u8d44\u6e90<\/b><\/p>\n

linux while if_linux while if_linux while if<\/p>\n

\u7cfb\u7edf\u7ba1\u7406\u4e0e\u76d1\u63a7\u9886\u57df\u4e2d\uff0c\u9664\u4e86\u5bf9\u6587\u4ef6\u884c\u6570\u7684\u7b80\u5355\u7edf\u8ba1\uff0c\u8fd8\u5de7\u5999\u5730\u8fd0\u7528\u4e86while\u53caif\u8bed\u53e5\uff0c\u5982\uff1a\u5728\u64b0\u5199\u76d1\u63a7\u811a\u672c\u65f6\uff0c\u53ef\u501f\u52a9while\u5faa\u73af\u6301\u7eed\u76d1\u6d4b\u7cfb\u7edf\u8d44\u6e90\u72b6\u51b5\uff0c\u540c\u65f6\u5229\u7528if\u5224\u65ad\u4ee5\u89e6\u53d1\u9884\u8b66\u6216\u6267\u884c\u76f8\u5e94\u64cd\u4f5c\u3002<\/p>\n

\u81ea\u52a8\u5316\u8fd0\u7ef4\u6280\u672f\u4e2d\uff0cwhile\u4e0eif\u76f8\u7ed3\u5408\u80fd\u6709\u6548\u5b9e\u65bd\u5b9a\u65f6\u4efb\u52a1\u76d1\u63a7\u53ca\u5f02\u5e38\u5904\u7406\u529f\u80fd\u3002\u901a\u8fc7\u7cbe\u7ec6\u8c03\u6574\u811a\u672c\u903b\u8f91\u548c\u7b97\u6cd5\uff0c\u786e\u4fdd\u7cfb\u7edf\u7684\u7a33\u5065\u5b89\u5168\u8fd0\u884c\u8d77\u5230\u4e86\u5173\u952e\u4f5c\u7528\u3002<\/p>\n

7.\u603b\u7ed3\u4e0e\u5c55\u671b<\/b><\/p>\n

\u901a\u8fc7\u672c\u6587\u5bf9\u4e8eLinux\u7cfb\u7edf\u4e2dwhile\u4ee5\u53caif\u8bed\u53e5\u7684\u6df1\u5165\u5256\u6790\u53ca\u5b9e\u6218\u793a\u4f8b\u7684\u6f14\u793a\uff0c\u6211\u4eec\u5f97\u4ee5\u6df1\u5316\u5bf9\u4e8e\u8fd9\u4e9b\u5173\u952e\u6307\u4ee4\u5728\u7a0b\u5e8f\u8bbe\u8ba1\u4e2d\u7684\u6838\u5fc3\u89d2\u8272\u53ca\u5176\u8fd0\u884c\u673a\u5236\u7684\u7406\u89e3\uff0c\u540c\u65f6\u4e5f\u638c\u63e1\u4e86\u5982\u4f55\u7075\u6d3b\u8fd0\u7528\u5b83\u4eec\u4e8e\u5b9e\u9645\u9879\u76ee\u5f00\u53d1\u4e4b\u4e2d\u3002<\/p>\n

\u9274\u4e8e\u79d1\u6280\u8fdb\u6b65\u4ee5\u53ca\u5e02\u573a\u9700\u6c42\u7684\u4e0d\u65ad\u6f14\u53d8\uff0cLinux\u4f5c\u4e3a\u4e3b\u5bfc\u6027\u64cd\u4f5c\u7cfb\u7edf\u5c06\u5ef6\u7eed\u5176\u6838\u5fc3\u5730\u4f4d\u3002\u800c\u5bf9\u4e8e\u5982'while'\u53ca'if'\u7b49\u57fa\u7840\u4e14\u6838\u5fc3\u7684\u7f16\u7a0b\u6982\u5ff5\uff0c\u5728\u7814\u53d1\u53ca\u8fd0\u7ef4\u6d3b\u52a8\u4e2d\u4ea6\u4f1a\u6301\u7eed\u5360\u636e\u91cd\u8981\u4f4d\u7f6elinux\u6587\u672c\u7f16\u8f91\u5668\uff0c\u4e3a\u6211\u4eec\u63d0\u5347\u4efb\u52a1\u6210\u679c\u6548\u7387\u63d0\u4f9b\u52a9\u529b\u3002<\/p>\n

\u671f\u671b\u8bfb\u8005\u80fd\u501f\u6b64\u6df1\u5165\u4e86\u89e3Linux\u7684while\u53caif\u8bed\u53e5\uff0c\u5e76\u5728\u5b9e\u6218\u4e2d\u63d0\u5347\u7f16\u7a0b\u6280\u5de7\u4e0e\u89e3\u51b3\u95ee\u9898\u80fd\u529b\u3002\u540c\u65f6\uff0c\u6211\u4eec\u70ed\u5207\u671f\u5f85\u5404\u4f4d\u5206\u4eab\u81ea\u8eab\u89c1\u89e3\u53ca\u7ecf\u9a8c\uff0c\u5171\u63a2\u5982\u4f55\u5584\u7528\u8fd9\u4e9b\u5de5\u5177\u63a8\u52a8\u79d1\u6280\u8fdb\u6b65\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

while\u5faa\u73af\u8bed\u53e5\u501f\u52a9if\u8bed\u53e5\uff0c\u53ef\u4f9d\u4e0d\u540c\u60c5\u5883\u6267\u884c\u76f8\u5e94\u7f16\u7801\u903b\u8f91\u3002while\u4e0eif\u7684\u7ed3\u5408\u8fd0\u7528\u8f93\u51fa\uff1a\u5305\u542b\u82f9\u679c\u7684\u884c\u6570\u4e3a\uff1a$count\u3002\u501f\u52a9\u4e0a\u8ff0\u811a\u672c\uff0c\u91c7\u7528while\u5faa\u73af\u53ef\u9010\u884c\u89e3\u6790test.<\/p>\n","protected":false},"author":1,"featured_media":290844,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[988],"tags":[999],"class_list":["post-290843","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tougao","tag-999"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/290843","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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=290843"}],"version-history":[{"count":0,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/290843\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/290844"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=290843"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=290843"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=290843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}