{"id":1320,"date":"2024-05-09T11:34:30","date_gmt":"2024-05-09T03:34:30","guid":{"rendered":"https:\/\/www.qiangzhenshuai.com\/?p=1320"},"modified":"2024-05-10T00:15:44","modified_gmt":"2024-05-09T16:15:44","slug":"shell-while","status":"publish","type":"post","link":"https:\/\/www.qiangzhenshuai.com\/?p=1320","title":{"rendered":"SHELL-while"},"content":{"rendered":"<h1>1.while\u4ecb\u7ecd<\/h1>\n<p>while\u5faa\u73af\u662f\u5148\u6267\u884c\u4e00\u6b21\u5373do\u4e00\u6b21\uff0c\u51fa\u5faa\u73af\u4f53\u65f6\u5019\u5224\u65adwhile\u6761\u4ef6\u662f\u5426\u4e3a\u771f\u771f\uff0c\u8df3\u5230do\u7ee7\u7eed\u5faa\u73af\uff1b\u5047\uff0c\u9000\u51fa\u5faa\u73af\uff0c\u6267\u884c\u4e0b\u4e00\u884c\u4ee3\u7801<\/p>\n<p>while\u662f\u8ba1\u7b97\u673a\u7684\u4e00\u79cd\u57fa\u672c\u5faa\u73af\u6a21\u5f0f\u3002\u5f53\u6ee1\u8db3\u6761\u4ef6\u65f6\u8fdb\u5165\u5faa\u73af\uff0c\u8fdb\u5165\u5faa\u73af\u540e\uff0c\u5f53\u6761\u4ef6\u4e0d\u6ee1\u8db3\u65f6\uff0c\u8df3\u51fa\u5faa\u73af\u3002while\u8bed\u53e5\u7684\u4e00\u822c\u8868\u8fbe\u5f0f\u4e3a\uff1awhile(\u8868\u8fbe\u5f0f){\u5faa\u73af\u4f53}<\/p>\n<h1>2.while\u5faa\u73af\u8be6\u89e3<\/h1>\n<p>\u5728Shell\u811a\u672c\u4e2d\uff0cwhile\u5faa\u73af\u662f\u4e00\u79cd\u7528\u4e8e\u91cd\u590d\u6267\u884c\u4e00\u6bb5\u4ee3\u7801\u5757\u7684\u63a7\u5236\u7ed3\u6784\u3002while\u5faa\u73af\u4f1a\u5728\u6307\u5b9a\u7684\u6761\u4ef6\u4e3a\u771f\u65f6\u91cd\u590d\u6267\u884c\u4ee3\u7801\u5757\uff0c\u76f4\u5230\u6761\u4ef6\u4e3a\u5047\u4e3a\u6b62\u3002<\/p>\n<p>while\u5faa\u73af\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<div>\n<div>while [ condition ]<\/div>\n<div>do<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0# \u5728condition\u4e3a\u771f\u65f6\u6267\u884c\u7684\u4ee3\u7801\u5757<\/div>\n<div>done<\/div>\n<\/div>\n<p>\u5728\u8fd9\u4e2a\u8bed\u6cd5\u4e2d\uff0ccondition\u662f\u4e00\u4e2a\u7528\u4e8e\u5224\u65ad\u5faa\u73af\u662f\u5426\u7ee7\u7eed\u6267\u884c\u7684\u8868\u8fbe\u5f0f\u3002\u53ea\u8981condition\u7684\u503c\u4e3a\u771f\uff08\u975e\u96f6\uff09\uff0c\u5faa\u73af\u5c31\u4f1a\u7ee7\u7eed\u6267\u884c\u4ee3\u7801\u5757\u3002\u4e00\u65e6condition\u7684\u503c\u4e3a\u5047\uff080\uff09\uff0c\u5faa\u73af\u5c31\u4f1a\u7ec8\u6b62\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528while\u5faa\u73af\u7684\u793a\u4f8b\uff1a<\/p>\n<div>\n<div>#!\/bin\/bash<\/div>\n<div>counter=1<\/div>\n<div>while [ $counter -le 5 ]<\/div>\n<div>do<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0echo &#8220;Counter: $counter&#8221;<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0((counter++))<\/div>\n<div>done<\/div>\n<\/div>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cwhile\u5faa\u73af\u4f1a\u5728counter\u5c0f\u4e8e\u7b49\u4e8e5\u65f6\u6301\u7eed\u6267\u884c\u3002\u5728\u6bcf\u6b21\u5faa\u73af\u4e2d\uff0c\u4f1a\u8f93\u51fa\u5f53\u524d\u7684\u8ba1\u6570\u5668\u503c\uff0c\u5e76\u5c06\u8ba1\u6570\u5668\u589e\u52a01\u3002\u5f53\u8ba1\u6570\u5668\u8fbe\u52305\u65f6\uff0c\u5faa\u73af\u4f1a\u7ec8\u6b62\u3002<\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728while\u5faa\u73af\u4e2d\uff0c\u5982\u679c\u6761\u4ef6\u6c38\u8fdc\u4e3a\u771f\uff0c\u5faa\u73af\u5c06\u4f1a\u65e0\u9650\u5faa\u73af\u4e0b\u53bb\uff0c\u76f4\u5230\u624b\u52a8\u7ec8\u6b62\u811a\u672c\u7684\u6267\u884c\u6216\u8fbe\u5230\u7cfb\u7edf\u8d44\u6e90\u9650\u5236\u3002\u56e0\u6b64\uff0c\u786e\u4fdd\u5728\u5faa\u73af\u4e2d\u66f4\u65b0\u6761\u4ef6\uff0c\u4ee5\u907f\u514d\u65e0\u9650\u5faa\u73af\u7684\u53d1\u751f\u3002<\/p>\n<h1>3.\u6848\u4f8b<\/h1>\n<h4>true\u547d\u4ee4<\/h4>\n<p>[root@shell \/server\/scripts]$ cat while1.sh<br \/>\n#!\/bin\/bash<\/p>\n<p>while true<br \/>\ndo<br \/>\necho &#8220;This will run indefinitely until manually stopped.&#8221;<br \/>\nsleep 1<br \/>\ndone<\/p>\n<p>true\u662f\u4e00\u4e2aUnix\/Linux\u7cfb\u7edf\u4e2d\u7684\u4e00\u4e2a\u547d\u4ee4\uff0c\u7528\u4e8e\u59cb\u7ec8\u8fd4\u56de\u6210\u529f\uff08\u9000\u51fa\u72b6\u6001\u7801\u4e3a0\uff09\u3002\u5728Shell\u811a\u672c\u4e2d\uff0ctrue\u547d\u4ee4\u901a\u5e38\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u6c38\u8fdc\u4e3a\u771f\u7684\u6761\u4ef6\uff0c\u5e38\u7528\u4e8ewhile\u5faa\u73af\u4e2d\u3002<\/p>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cwhile true\u521b\u5efa\u4e86\u4e00\u4e2a\u6c38\u8fdc\u4e3a\u771f\u7684\u6761\u4ef6\uff0c\u56e0\u6b64\u5faa\u73af\u4f1a\u65e0\u9650\u5faa\u73af\u4e0b\u53bb\uff0c\u76f4\u5230\u624b\u52a8\u7ec8\u6b62\u811a\u672c\u7684\u6267\u884c\u3002\u5728\u6bcf\u6b21\u5faa\u73af\u4e2d\uff0c\u4f1a\u8f93\u51fa\u4e00\u6761\u6d88\u606f\u5e76\u4f11\u77201\u79d2\u3002\u8fd9\u79cd\u7528\u6cd5\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u4e00\u4e9b\u9700\u8981\u6301\u7eed\u8fd0\u884c\u7684\u4efb\u52a1\uff0c\u6bd4\u5982\u76d1\u63a7\u7cfb\u7edf\u72b6\u6001\u6216\u5b9e\u65f6\u5904\u7406\u6570\u636e\u7b49\u3002<\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4f7f\u7528true\u547d\u4ee4\u521b\u5efa\u65e0\u9650\u5faa\u73af\u65f6\uff0c\u8981\u786e\u4fdd\u6709\u5408\u9002\u7684\u7ec8\u6b62\u6761\u4ef6\u6216\u624b\u52a8\u505c\u6b62\u811a\u672c\u7684\u6267\u884c\uff0c\u4ee5\u907f\u514d\u8d44\u6e90\u6d6a\u8d39\u6216\u7cfb\u7edf\u8d1f\u8f7d\u8fc7\u9ad8\u3002<\/p>\n<h4>\u4ece1\u52a0\u5230100<\/h4>\n<p>[root@shell \/server\/scripts]$ cat whileshu.sh<br \/>\n#!\/bin\/sh<br \/>\ni=1<br \/>\nwhile [ 100 -ge $i ]<br \/>\ndo<br \/>\ncount=$[count+i]<br \/>\nlet i++<br \/>\ndone<br \/>\necho $count<br \/>\n[root@shell \/server\/scripts]$ sh whileshu.sh<br \/>\n5050<\/p>\n<h4>\u521b\u5efa\u7528\u6237<\/h4>\n<p>[root@shell \/server\/scripts]$ cat user.txt<br \/>\ntiedan 123<br \/>\nheidan 456<br \/>\ngoudan 789<br \/>\ngousheng 987<br \/>\ngangzi 654<\/p>\n<div>[root@shell \/server\/scripts]$ cat whileuser.sh<br \/>\n#!\/bin\/sh<br \/>\nwhile read line<br \/>\ndo<br \/>\nuser=`echo $line|awk &#8216;{print $1}&#8217;`<br \/>\npass=`echo $line|awk &#8216;{print $2}&#8217;`<br \/>\nuseradd $user<br \/>\nif [ $? -eq 0 ];then<br \/>\necho &#8220;$user\u521b\u5efa\u6210\u529f&#8221;<br \/>\necho $pass|passwd &#8211;stdin $user &amp;&gt;\/dev\/null<br \/>\n[ $? -eq 0 ] &amp;&amp; echo &#8220;\u5bc6\u7801\u521b\u5efa\u6210\u529f&#8221;<br \/>\necho -e &#8220;$user\\t$pass&#8221; &gt;&gt; passwd.txt<br \/>\nelse<br \/>\necho &#8220;\u7528\u6237\u5df2\u5b58\u5728&#8221;<br \/>\nfi<br \/>\ndone[root@shell \/server\/scripts]$ sh whileuser.sh<br \/>\ntiedan\u521b\u5efa\u6210\u529f<br \/>\n\u5bc6\u7801\u521b\u5efa\u6210\u529f<br \/>\nheidan\u521b\u5efa\u6210\u529f<br \/>\n\u5bc6\u7801\u521b\u5efa\u6210\u529f<br \/>\ngoudan\u521b\u5efa\u6210\u529f<br \/>\n\u5bc6\u7801\u521b\u5efa\u6210\u529f<br \/>\ngousheng\u521b\u5efa\u6210\u529f<br \/>\n\u5bc6\u7801\u521b\u5efa\u6210\u529f<br \/>\ngangzi\u521b\u5efa\u6210\u529f<br \/>\n\u5bc6\u7801\u521b\u5efa\u6210\u529f<\/p>\n<p>[root@shell \/server\/scripts]$ cat passwd.txt<br \/>\ntiedan 123<br \/>\nheidan 456<br \/>\ngoudan 789<br \/>\ngousheng 987<br \/>\ngangzi 654<\/p>\n<p>[root@shell ~]$ su &#8211; gousheng<br \/>\n[gousheng@shell ~]$ su &#8211; tiedan<br \/>\nPassword:<br \/>\nLast failed login: Fri May 10 00:06:43 CST 2024 on pts\/1<br \/>\nThere was 1 failed login attempt since the last successful login.<\/p>\n<p>[root@shell \/server\/scripts]$ tail -5 \/etc\/passwd<br \/>\ntiedan:x:1001:1001::\/home\/tiedan:\/bin\/bash<br \/>\nheidan:x:1002:1002::\/home\/heidan:\/bin\/bash<br \/>\ngoudan:x:1003:1003::\/home\/goudan:\/bin\/bash<br \/>\ngousheng:x:1004:1004::\/home\/gousheng:\/bin\/bash<br \/>\ngangzi:x:1005:1005::\/home\/gangzi:\/bin\/bash<\/p>\n<p>&nbsp;<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>1.while\u4ecb\u7ecd while\u5faa\u73af\u662f\u5148\u6267\u884c\u4e00\u6b21\u5373do\u4e00\u6b21\uff0c\u51fa\u5faa\u73af\u4f53\u65f6\u5019\u5224\u65adwhile\u6761\u4ef6\u662f\u5426\u4e3a\u771f\u771f\uff0c\u8df3\u5230do\u7ee7 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[25],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.qiangzhenshuai.com\/index.php?rest_route=\/wp\/v2\/posts\/1320"}],"collection":[{"href":"https:\/\/www.qiangzhenshuai.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.qiangzhenshuai.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.qiangzhenshuai.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.qiangzhenshuai.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1320"}],"version-history":[{"count":4,"href":"https:\/\/www.qiangzhenshuai.com\/index.php?rest_route=\/wp\/v2\/posts\/1320\/revisions"}],"predecessor-version":[{"id":1324,"href":"https:\/\/www.qiangzhenshuai.com\/index.php?rest_route=\/wp\/v2\/posts\/1320\/revisions\/1324"}],"wp:attachment":[{"href":"https:\/\/www.qiangzhenshuai.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1320"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.qiangzhenshuai.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1320"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.qiangzhenshuai.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1320"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}