{"id":760,"date":"2022-03-13T18:34:31","date_gmt":"2022-03-13T10:34:31","guid":{"rendered":"https:\/\/www.buyao007.icu\/?p=760"},"modified":"2022-03-13T18:34:33","modified_gmt":"2022-03-13T10:34:33","slug":"3-shell%e7%bc%96%e7%a8%8b%e4%b9%8b%e5%be%aa%e7%8e%af","status":"publish","type":"post","link":"https:\/\/www.buyao007.icu\/?p=760","title":{"rendered":"3.shell\u7f16\u7a0b\u4e4b\u5faa\u73af"},"content":{"rendered":"\n<p>\u5b59\u5bcc\u9633\uff0c \u6c5f\u6e56\u4eba\u79f0\u6ca1\u4eba\u79f0\u3002\u591a\u5e74\u4e92\u8054\u7f51\u8fd0\u7ef4\u5de5\u4f5c\u7ecf\u9a8c\uff0c\u66fe\u8d1f\u8d23\u8fc7\u5b59\u5e03\u65af\u5927\u89c4\u6a21\u96c6\u7fa4\u67b6\u6784\u81ea\u52a8\u5316\u8fd0\u7ef4\u7ba1\u7406\u5de5\u4f5c\u3002\u64c5\u957fWeb\u96c6\u7fa4\u67b6\u6784\u4e0e\u81ea\u52a8\u5316\u8fd0\u7ef4\uff0c\u66fe\u8d1f\u8d23\u56fd\u5185\u67d0\u5927\u578b\u535a\u5ba2\u7f51\u7ad9\u8fd0\u7ef4\u5de5\u4f5c\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">1.shell\u5faa\u73af\u8bed\u53e5-for<\/h2>\n\n\n\n<p><strong><em>1.for\u5faa\u73af\u57fa\u672c\u6982\u8ff0<\/em><\/strong><\/p>\n\n\n\n<p>1.\u4ec0\u4e48\u662f\u5faa\u73af<\/p>\n\n\n\n<p>\u811a\u672c\u5728\u6267\u884c\u4efb\u52a1\u7684\u65f6\u5019\uff0c\u603b\u4f1a\u9047\u5230\u9700\u8981\u5faa\u73af\u6267\u884c\u7684\u65f6\u5019\uff0c\u6bd4\u5982\u8bf4\u6279\u91cf\u521b\u5efa100\u4e2a\u7528\u6237\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6765\u5b9e\u73b0\u3002<\/p>\n\n\n\n<p>2.\u4ec0\u4e48\u662ffor\u5faa\u73af<\/p>\n\n\n\n<p>\u5f88\u591a\u4eba\u628afor\u5faa\u73af\u53eb\u505a\u6761\u4ef6\u5faa\u73af\uff0c\u56e0\u4e3afor\u5faa\u73af\u7684\u6b21\u6570\u548c\u7ed9\u4e0e\u7684\u6761\u4ef6\u662f\u6210\u6b63\u6bd4\u7684\uff0c\u4e5f\u5c31\u662f\u4f60\u7ed95\u4e2a\u6761\u4ef6\uff0c\u90a3\u4e48\u4ed6\u5c31\u5faa\u73af5\u6b21\u3002<\/p>\n\n\n\n<p><strong><em>2.for\u5faa\u73af\u57fa\u7840\u8bed\u6cd5<\/em><\/strong><\/p>\n\n\n\n<p>1.for\u5faa\u73af\u8bed\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for \u53d8\u91cf\u540d in &#91;\u53d6\u503c\u5217\u8868]\r\ndo\r\n \u8981\u505a\u7684\u4e8b\u60c5\r\ndone\r\n#for\u5faa\u73af\u793a\u4f8b\uff08\u9ed8\u8ba4\u4ee5\u7a7a\u683c\u505a\u5206\u5272\uff0c\u7ed9\u4e86\u4e09\u4e2a\u6761\u4ef6\uff09\r\nfor var in file1 file2 file3\r\ndo\r\n  echo the text is $var\r\ndone\r<\/code><\/pre>\n\n\n\n<p>2.for\u5faa\u73af\u57fa\u672c\u4f7f\u7528\u793a\u4f8b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;root@fsf ~]# cat for-1.sh\r\n#!\/bin\/bash\r\nfor i in {1..10}\r\ndo\r\n  useradd test-$i\r\n  echo test-$i is ok\r\ndone\r<\/code><\/pre>\n\n\n\n<p>3.for\u5faa\u73af\u57fa\u672c\u4f7f\u7528\u793a\u4f8b-\u4ece\u547d\u4ee4\u4e2d\u53d6\u503c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;root@fsf ~]# cat for-5.sh \r\n#!\/bin\/bash\r\nfor i in `cat \/etc\/hosts`\r\ndo\r\n echo $i\r\ndone\r\n#for\u5faa\u73af\u9ed8\u8ba4\u4ee5\u4f7f\u7528\u7a7a\u683c\u4e3a\u5206\u9694\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528IFS\u6765\u81ea\u5b9a\u4e49\u5206\u9694\u7b26\r\n#\u4ee5\u5192\u53f7\u505a\u5206\u5272\u7b26 IFS=:\r\n#\u4ee5\u5192\u53f7\u3001\u5206\u53f7\u548c\u53cc\u5f15\u53f7\u4f5c\u4e3a\u5b57\u6bb5\u5206\u9694\u7b26 IFS=\uff1a\uff1b\u201d\r\n#\u4ee5\u6362\u884c\u7b26\u4f5c\u4e3a\u5b57\u6bb5\u7684\u5206\u9694\u7b26 IFS=$\u2019\\n\u2019\r<\/code><\/pre>\n\n\n\n<p>4.for\u5faa\u73af\u57fa\u672c\u4f7f\u7528\u793a\u4f8b\uff0cC\u8bed\u8a00\u98ce\u683c\u7684for<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u8bed\u6cd5\u683c\u5f0f\r\nfor \uff08\uff08i=0;i&lt;10;i++\uff09\uff09\r\ndo\r\n  commands\r\ndone\r<\/code><\/pre>\n\n\n\n<p>5.\u5e76\u53d1\u6267\u884c\uff08\u540e\u53f0\u8fd0\u884c\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;root@fsf ~]# cat for.sh \r\n#!\/bin\/sh\r\nfor i in  `seq 254`\r\ndo\r\n      { IP=10.0.0.$i\r\n      ping -c1 -W1 $IP &amp;>\/dev\/null\r\n      if &#91; $? -eq 0 ];then\r\n       echo \"$IP \u5728\u7ebf\"\r\n      fi } &amp;\r\n\t\r\ndone\r\nwait###############\u8ba9\u540e\u53f0\u6267\u884c\u7684\u7ed3\u675f\u540e\u518d\u5411\u4e0b\u6267\u884c\r\necho \"\u5728\u7ebf\u53d6IP\u5b8c\u6210................\"\r<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">\u4e8c\u3001shell\u5faa\u73af\u8bed\u53e5-while<\/h2>\n\n\n\n<p><strong><em>1.while\u5faa\u73af\u57fa\u672c\u6982\u8ff0<\/em><\/strong><\/p>\n\n\n\n<p>1.\u4ec0\u4e48\u662fwhile<\/p>\n\n\n\n<p>While\u5728shell\u4e2d\u4e5f\u662f\u8d1f\u8d23\u5faa\u73af\u7684\u8bed\u53e5\uff0c\u548cfor\u4e00\u6837\u3002<\/p>\n\n\n\n<p>2.while\u5faa\u73af\u548cfor\u5faa\u73af\u600e\u4e48\u9009\uff1f<\/p>\n\n\n\n<p>\u56e0\u4e3a\u529f\u80fd\u4e00\u6837\uff0c\u5f88\u591a\u4eba\u5728\u5b66\u4e60\u548c\u5de5\u4f5c\u4e2d\u9047\u5230\u5faa\u73af\u5230\u5e95\u8be5\u4f7f\u7528for\u8fd8\u662fwhile\u5462\uff1f\u5f88\u591a\u4eba\u4e0d\u77e5\u9053\uff0c\u5c31\u4f1a\u51fa\u73b0\u6709\u4eba\u4e00\u9047\u5230\u5faa\u73af\u5c31\u4f7f\u7528for\u3001\u6709\u4eba\u4e00\u9047\u5230\u5faa\u73af\u5c31\u4f7f\u7528while\u3002\u5230\u5e95\u9009for\u597d\u8fd8\u662fwhile\u597d\uff1a<br>1\uff0e\u77e5\u9053\u5faa\u73af\u6b21\u6570\u7684\u4f7f\u7528for\uff0c\u6bd4\u5982\u4e00\u5929\u5faa\u73af24\u6b21\uff1b<br>2\uff0e\u5982\u679c\u4e0d\u77e5\u9053\u8981\u5faa\u73af\u591a\u5c11\u6b21\uff0c\u90a3\u5c31\u7528while\uff0c\u6bd4\u5982\u731c\u6570\u5b57\u6e38\u620f\uff0c\u6bcf\u4e2a\u4eba\u731c\u5bf9\u7684\u6b21\u6570\u662f\u672a\u77e5\u7684\u3002<\/p>\n\n\n\n<p><strong><em>2\uff0eWhile\u5faa\u73af\u57fa\u7840\u8bed\u6cd5<\/em><\/strong><\/p>\n\n\n\n<p>1.while\u5faa\u73af\u8bed\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u5f53\u6761\u4ef6\u6d4b\u8bd5\u6210\u7acb\uff08\u6761\u4ef6\u6d4b\u8bd5\u4e3a\u771f\uff09\uff0c\u6267\u884c\u5faa\u73af\u4f53\r\nwhile \u6761\u4ef6\u6d4b\u8bd5\r\ndo\r\n   \u6267\u884c\u7684\u5faa\u73af\u4f53\r\ndone\r<\/code><\/pre>\n\n\n\n<p>2.while\u5faa\u73af\u57fa\u672c\u4f7f\u7528\u793a\u4f8b\uff0c\u964d\u5e8f\u8f93\u51fa10-1\u7684\u6570\u5b57<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;root@fsf ~]# cat tty.sh \r\n#!\/bin\/bash\r\nvar=10\r\nwhile &#91; $var -gt 0 ]\r\ndo\r\n  echo  $var \r\n  var=$&#91;$var-1]\r\ndone\r<\/code><\/pre>\n\n\n\n<p>3.\u5d4c\u5957\u5faa\u73af\u6587\u4ef6\u6bd4\u8f83\uff0c\u5224\u65ad\/tmp\/sfy\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff0c\u5982\u679c\u4e0d\u5b58\u5728\u52193s\u8f93\u51fa\u4e00\u6b21notfound\u3002\u5982\u679c\u5b58\u5728\u5219\u81ea\u52a8\u9000\u51fa\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;root@fsf ~]# cat while_file.sh \r\n#!\/bin\/bash\r\nwhile &#91; ! -d \/tmp\/sfy ]\r\ndo\r\n echo \"not found \/tmp\/sfy\"\r\n sleep 3\r\ndone\r<\/code><\/pre>\n\n\n\n<p>4.\u5faa\u73af\u5d4c\u5957\u5b57\u7b26\u6bd4\u8f83\uff0c\u5224\u65ad\u7528\u6237\u8f93\u5165\u7684\u7528\u6237\u540d\uff0c\u5982\u679c\u4e0d\u662froot\u5219\u4e00\u76f4\u8ba9\u5176\u8f93\u5165<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;root@fsf ~]# cat while_string.sh\r\n#!\/bin\/bash\r\nread -p \"Login: \" account\r\nwhile &#91; $account != 'root' ]\r\ndo\r\n read -p \"Login: \" account\r\ndone\r<\/code><\/pre>\n\n\n\n<p>5.while\u5faa\u73afread\u8bfb\u5165<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>while read line     #line \u662f\u81ea\u5b9a\u4e49\u7684\u53d8\u91cf while\u662f\u6309\u7167\u884c\u8bfb\u53d6\u6587\u4ef6 \u800c\u4e0d\u662ffor\u7684\u7a7a\u683c\r\ndo \r\n   \u547d\u4ee4\r\ndone&lt;file.txt\r\n#############\u4f7f\u7528\u793a\u4f8b\u7edf\u8ba1\u6587\u4ef6\u884c\u6570\r\n&#91;root@fsf ~]# cat file.sh\r\n#!\/bin\/sh\r\nwhile read line\r\ndo\r\n     let i++\r\ndone&lt;\/etc\/passwd\r\necho $i \u884c\r<\/code><\/pre>\n\n\n\n<p><strong><em>3.while\u5faa\u73af\u63a7\u5236\u8bed\u53e5<\/em><\/strong><\/p>\n\n\n\n<p>\u5728\u6211\u4eec\u4f7f\u7528\u5faa\u73af\u8bed\u53e5\u8fdb\u884c\u5faa\u73af\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6709\u65f6\u5019\u9700\u8981\u5728\u672a\u8fbe\u5230\u5faa\u73af\u7ed3\u675f\u6761\u4ef6\u65f6\u5f3a\u5236\u8df3\u51fa\u5faa\u73af\uff0c\u90a3\u4e48shell\u7ed9\u6211\u4eec\u63d0\u4f9b\u4e86\u5185\u7f6e\u65b9\u6cd5\u6765\u5b9e\u73b0\u8be5\u529f\u80fd\uff1aexit\u3001break\u3001continue<\/p>\n\n\n\n<p>1.exit\uff0c\u9000\u51fa\u6574\u4e2a\u7a0b\u5e8f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5f53\u811a\u672c\u78b0\u5230exit\u65f6\uff0c\u76f4\u63a5\u9000\u51fa\uff0c\u5269\u4f59\u4e0d\u7ba1\u6709\u591a\u5c11\u4ee3\u7801\u90fd\u4e0d\u6267\u884c\u3002\r\n&#91;root@fsf ~]# cat for_exit.sh\r\n#!\/bin\/bash\r\nfor i in {1..3}\r\ndo\r\n  echo \"123\"\r\n  exit####\u4e0b\u9762\u7684\u90fd\u4e0d\u4f1a\u6267\u884c\uff0c\u76f4\u63a5\u9000\u51fa\r\n  echo \"456\"\r\ndone\r\necho \"Done......\"\r<\/code><\/pre>\n\n\n\n<p>2.break\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u7ed3\u675f\u5f53\u524d\u5faa\u73af\uff0c\u4f46\u4f1a\u6267\u884c\u5faa\u73af\u4e4b\u540e\u7684\u6240\u6709\u4ee3\u7801\u3002\r\n&#91;root@fsf ~]# cat for_exit.sh\r\n#!\/bin\/bash\r\nfor i in {1..3}\r\ndo\r\n  echo \"123\"\r\n  break\r\n  echo \"456\"\r\ndone\r\necho \"Done......\"\r<\/code><\/pre>\n\n\n\n<p>3.continue\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5ffd\u7565\u672c\u6b21\u5faa\u73af\u5269\u4f59\u7684\u4ee3\u7801\uff0c\u76f4\u63a5\u8fdb\u884c\u4e0b\u4e00\u6b21\u5faa\u73af\uff0c\u76f4\u5230\u5faa\u73af\u7ed3\u675f\uff0c\u7136\u540e\u7ee7\u7eed\u5faa\u73af\u4e4b\u540e\u7684\u4ee3\u7801\u3002\r\n&#91;root@fsf ~]# cat for_exit.sh\r\n#!\/bin\/bash\r\nfor i in {1..3}\r\ndo\r\n  echo \"123\"\r\n  continue\r\n  echo \"456\"\r\ndone\r\necho \"Done......\"\r\n&#91;root@fsf ~]# sh for_exit.sh\r\n123\r\n123\r\n123\r\nDone......\r<\/code><\/pre>\n\n\n\n<p>ps\uff1a\u9650\u5236\u7279\u6b8a\u4fe1\u53f7\uff08\u7981\u6b62ctrl+c\u548cz\uff09<\/p>\n\n\n\n<p>trap \"echo \u4e0d\u8981\u4e71\u6309\u5c0f\u5fc3\u7206\u70b8\" HUP INT TSTP<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b59\u5bcc\u9633\uff0c \u6c5f\u6e56\u4eba\u79f0\u6ca1\u4eba\u79f0\u3002\u591a\u5e74\u4e92\u8054\u7f51\u8fd0\u7ef4\u5de5\u4f5c\u7ecf\u9a8c\uff0c\u66fe\u8d1f\u8d23\u8fc7\u5b59\u5e03\u65af\u5927\u89c4\u6a21\u96c6\u7fa4\u67b6\u6784\u81ea\u52a8\u5316\u8fd0\u7ef4\u7ba1\u7406\u5de5\u4f5c\u3002\u64c5\u957fWeb\u96c6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"_links":{"self":[{"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/posts\/760"}],"collection":[{"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=760"}],"version-history":[{"count":1,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/posts\/760\/revisions"}],"predecessor-version":[{"id":761,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/posts\/760\/revisions\/761"}],"wp:attachment":[{"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=760"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=760"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=760"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}