{"id":156107,"date":"2019-09-17T11:46:17","date_gmt":"2019-09-17T03:46:17","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=156107"},"modified":"2019-08-29T15:47:30","modified_gmt":"2019-08-29T07:47:30","slug":"shell-for-while","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/shell-for-while.html","title":{"rendered":"Shell\u811a\u672c\u5e94\u7528 – for\u3001while\u5faa\u73af\u8bed\u53e5"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u901a\u8fc7Shell\u811a\u672c\u5e94\u7528\uff08\u4e8c\uff09\u5b66\u4e60\u5230\u4e86if\u6761\u4ef6\u6761\u4ef6\u8bed\u53e5\u7684\u4f7f\u7528\u65b9\u6cd5\u7b49\u3002Shell\u4f5c\u4e3a\u4e00\u79cd\u811a\u672c\u7f16\u7a0b\u8bed\u8a00\uff0c\u540c\u6837\u4e86\u5305\u542b\u4e86\u5faa\u73af\uff0c\u5206\u652f\u7b49\u5176\u4ed6\u7a0b\u5e8f\u63a7\u5236\u7ed3\u6784\uff0c\u4ece\u800c\u80fd\u591f\u8f7b\u677e\u5b8c\u6210\u66f4\u52a0\u590d\u6742\u3001\u5f3a\u5927\u7684\u529f\u80fd\u3002\u6211\u4eec\u4eca\u5929\u5c31\u6765\u8ba4\u8bc6for\u3001while\u3001case\u8bed\u53e5\u7684\u5177\u4f53\u5e94\u7528\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u4e00\u3001for\u5faa\u73af\u8bed\u53e5<\/strong><\/div>\n

\u5728\u5b9e\u9645\u5de5\u4f5c\u4e2d\uff0c\u7ecf\u5e38\u4f1a\u9047\u5230\u67d0\u9879\u4efb\u52a1\u9700\u8981\u591a\u6b21\u6267\u884c\u7684\u60c5\u51b5\uff0c\u800c\u6bcf\u6b21\u6267\u884c\u65f6\u4ec5\u4ec5\u662f\u5904\u7406\u7684\u5bf9\u8c61\u4e0d\u4e00\u6837\uff0c\u5176\u4ed6\u547d\u4ee4\u76f8\u540c\u3002\u4f8b\u5982\uff1a\u6839\u636e\u901a\u8baf\u5f55\u4e2d\u7684\u59d3\u540d\u5217\u8868\u521b\u5efa\u7cfb\u7edf\u8d26\u53f7\u7b49\u60c5\u51b5\u3002<\/p>\n

\u5f53\u9762\u5bf9\u5404\u79cd\u5217\u8868\u91cd\u590d\u5de5\u4f5c\u4efb\u52a1\u65f6\uff0c\u4f7f\u7528if\u8bed\u53e5\u5df2\u7ecf\u4e0d\u80fd\u6ee1\u8db3\u9700\u8981\u4e86\uff0c\u5c31\u9700\u8981\u4f7f\u7528\u5230for\u8bed\u53e5\u3002<\/p>\n

1.for\u8bed\u53e5\u7ed3\u6784<\/strong><\/span><\/div>\n

\u4f7f\u7528for\u5faa\u73af\u8bed\u53e5\u65f6\uff0c\u9700\u8981\u6307\u5b9a\u4e00\u4e2a\u53d8\u91cf\u53ca\u53ef\u80fd\u7684\u53d6\u503c\u5217\u8868\uff0c\u9488\u5bf9\u4e0d\u540c\u7684\u53d6\u503c\u91cd\u590d\u6267\u884c\u76f8\u540c\u7684\u547d\u4ee4\u5e8f\u5217\uff0c\u77e5\u9053\u53d8\u91cf\u503c\u7528\u5b8c\u9000\u51fa\u5faa\u73af\u3002<\/p>\n

1\uff09for\u5faa\u73af\u8bed\u53e5\u7684\u8bed\u6cd5\u7ed3\u6784\uff1a<\/strong><\/p>\n

for \u53d8\u91cf\u540d in \u53d6\u503c\u5217\u8868
\ndo
\n\u547d\u4ee4\u5e8f\u5217
\ndone<\/p>\n

\u8bed\u53e5\u7ed3\u6784\u4e2d\uff0cfor\u8bed\u53e5\u7684\u64cd\u4f5c\u5bf9\u8c61\u4e3a\u7528\u6237\u5427\u6307\u5b9a\u540d\u79f0\u7684\u53d8\u91cf\uff0c\u5e76\u901a\u8fc7in\u5173\u952e\u5b57\u4e3a\u8be5\u53d8\u91cf\u9884\u5148\u8bbe\u7f6e\u4e86\u4e00\u4e2a\u53d6\u503c\u5217\u8868\uff0c\u591a\u4e2a\u53d6\u503c\u4e4b\u95f4\u7528\u7a7a\u683c\u8fdb\u884c\u5206\u9694\u3002\u4f4d\u4e8edo\u2026\u2026done\u4e4b\u95f4\u7684\u547d\u4ee4\u5e8f\u5217\u79f0\u4e3a\u5faa\u73af\u4f53\uff0c\u5176\u4e2d\u7684\u6267\u884c\u8bed\u53e5\u9700\u8981\u5e94\u7528\u53d8\u91cf\u4ee5\u5b8c\u6210\u76f8\u5e94\u7684\u4efb\u52a1\u3002<\/p>\n

2\uff09for\u5faa\u73af\u8bed\u53e5\u6d41\u7a0b\u56fe<\/strong><\/p>\n

\"\"<\/p>\n

3\uff09for\u5faa\u73af\u8bed\u53e5\u7684\u4f7f\u7528\u573a\u666f<\/strong><\/p>\n

1. \u5faa\u73af\u6b21\u6570\u672a\u77e5\uff1b
\n2.\u5b8c\u6574\u904d\u5386\u6574\u4e2a\u53d6\u503c\u5217\u8868\u3002<\/p>\n

2.for\u8bed\u53e5\u5e94\u7528\u793a\u4f8b<\/strong><\/span><\/div>\n

\u8981\u6c42\uff1a
\n1.\u6839\u636e\u59d3\u540d\u5217\u8868\u6279\u91cf\u521b\u5efa\u7528\u6237\uff1b
\n2.\u7528\u6237\u7684\u521d\u59cb\u5bc6\u7801\u662f\u201c123456\u201d
\n\u5b9e\u65bd\u6b65\u9aa4\uff1a<\/p>\n

\r\n[root@localhost ~]# vim \/root\/users.txt \/\/\u65b0\u5efa\u4e00\u4e2a\u53d6\u503c\u5217\u8868\r\nxiaozhang\r\nxiaoli\r\nxiaowang\r\nxiaosun\r\n[root@localhost ~]# vim useraddfor.sh \/\/\u521b\u5efa\u6279\u91cf\u521b\u5efa\u7528\u6237\u7684\u811a\u672c\r\n#!\/bin\/bash\r\nULIST=$(cat \/root\/users.txt)\r\nfor UNAME in $ULIST\r\ndo\r\nuseradd $UNAME\r\necho \"123456\" | passwd --stdin $UNAME &> \/dev\/null\r\ndone\r\n[root@localhost ~]# sh useraddfor.sh \/\/\u6267\u884c\u811a\u672c\r\n[root@localhost ~]# tail -4 \/etc\/passwd \/\/\u9a8c\u8bc1\u6548\u679c\r\nxiaozhang:x:1001:1001::\/home\/xiaozhang:\/bin\/bash\r\nxiaoli:x:1002:1002::\/home\/xiaoli:\/bin\/bash\r\nxiaowang:x:1003:1003::\/home\/xiaowang:\/bin\/bash\r\nxiaosun:x:1004:1004::\/home\/xiaosun:\/bin\/bash\r\n<\/pre>\n

\u6ce8\u610f\uff1aif\u8bed\u53e5\u3001for\u8bed\u53e5\u53ca\u5176\u4ed6\u5404\u79cdshell\u811a\u672c\u8bed\u53e5\u90fd\u662f\u53ef\u4ee5\u5d4c\u5957\u4f7f\u7528\u7684\u3002<\/p>\n

\u4e8c\u3001while\u5faa\u73af\u8bed\u53e5<\/strong><\/div>\n

for\u5faa\u73af\u8bed\u53e5\u975e\u5e38\u9002\u7528\u4e8e\u5217\u8868\u5bf9\u8c61\u65e0\u89c4\u5f8b\uff0c\u4e14\u5217\u8868\u6765\u6e90\u5df2\u56fa\u5b9a\u7684\u573a\u5408\uff0c\u800c\u5bf9\u4e8e\u8981\u6c42\u63a7\u5236\u5faa\u73af\u6b21\u6570\uff0c\u64cd\u4f5c\u5bf9\u8c61\u6309\u6570\u5b57\u987a\u5e8f\u7f16\u53f7\u3001\u6309\u7279\u5b9a\u6761\u4ef6\u6267\u884c\u91cd\u590d\u64cd\u4f5c\u7b49\u60c5\u51b5\uff0c\u5219\u66f4\u9002\u5408\u4f7f\u7528\u2014\u2014while\u5faa\u73af\u8bed\u53e5\u3002<\/p>\n

1.while\u8bed\u53e5\u7684\u7ed3\u6784<\/strong><\/span><\/div>\n

\u4f7f\u7528while\u5faa\u73af\u8bed\u53e5\u662f\uff0c\u53ef\u4ee5\u6839\u636e\u7279\u5b9a\u7684\u6761\u4ef6\u53cd\u590d\u6267\u884c\u4e00\u4e2a\u547d\u4ee4\u5e8f\u5217\uff0c\u77e5\u9053\u8be5\u6761\u4ef6\u4e0d\u518d\u6ee1\u8db3\u4e3a\u6b62\u3002\u5728\u811a\u672c\u5e94\u7528\u4e2d\uff0c\u5e94\u8be5\u5c3d\u91cf\u907f\u514d\u51fa\u73b0\u6b7b\u5faa\u73af\u7684\u60c5\u51b5\uff0c\u5426\u5219\u540e\u8fb9\u7684\u547d\u4ee4\u64cd\u4f5c\u5c06\u65e0\u6cd5\u6267\u884c\u3002\u56e0\u6b64\uff0c\u5faa\u73af\u4f53\u5185\u7684\u547d\u4ee4\u5e8f\u5217\u4e2d\u5e94\u5305\u62ec\u4fee\u6539\u6d4b\u8bd5\u6761\u4ef6\u7684\u8bed\u53e5\uff0c\u4ee5\u4fbf\u5728\u9002\u5f53\u7684\u65f6\u5019\u4f7f\u6d4b\u8bd5\u6761\u4ef6\u4e0d\u518d\u6210\u7acb\uff0c\u4ece\u800c\u7ed3\u675f\u5faa\u73af\u3002<\/p>\n

1\uff09while\u5faa\u73af\u8bed\u53e5\u7684\u8bed\u6cd5\u7ed3\u6784<\/strong><\/p>\n

while \u6761\u4ef6\u6d4b\u8bd5\u64cd\u4f5c
\ndo
\n\u547d\u4ee4\u5e8f\u5217
\ndone<\/p>\n

2\uff09while\u5faa\u73af\u8bed\u53e5\u6d41\u7a0b\u56fe<\/strong>
\n\"\"
\n\u4f7f\u7528while\u5faa\u73af\u8bed\u53e5\u65f6\uff0c\u6709\u4e24\u4e2a\u7279\u6b8a\u7684\u6761\u4ef6\u6d4b\u8bd5\u64cd\u4f5c\u3002\u5373true\uff08\u771f\uff09\u548cfalse\uff08\u5047\uff09\u3002\u4f7f\u7528true\u4f5c\u4e3a\u6761\u4ef6\u65f6\uff0c\u8868\u793a\u6761\u4ef6\u6c38\u8fdc\u6210\u7acb\uff0c\u5faa\u73af\u4f53\u5185\u7684\u547d\u4ee4\u5e8f\u5217\u5c06\u65e0\u9650\u6267\u884c\u4e0b\u53bb\uff0c\u9664\u975e\u5f3a\u5236\u7ec8\u6b62\u811a\u672c\uff08\u6216\u4f7f\u7528exit\u8bed\u53e5\u9000\u51fa\u811a\u672c\uff09\uff1b\u53cd\u4e4b\u82e5\u4f7f\u7528false\u4f5c\u4e3a\u6761\u4ef6\uff0c\u5219\u5faa\u73af\u4f53\u5c06\u4e0d\u4f1a\u6267\u884c\u3002<\/p>\n

3\uff09while\u5faa\u73af\u8bed\u53e5\u7684\u4f7f\u7528\u573a\u666f<\/strong><\/p>\n

1.\u5faa\u73af\u6b21\u6570\u5df2\u77e5\uff1b
\n2.\u5fc5\u987b\u6709\u4e00\u4e2a\u53ef\u4ee5\u63a7\u5236\u5faa\u73af\u53d8\u91cf\u7684\u8bed\u53e5\u3002<\/p>\n

2.while\u8bed\u53e5\u5e94\u7528\u793a\u4f8b<\/strong><\/span><\/div>\n

\u8981\u6c42\uff1a
\n\u6279\u91cf\u521b\u5efa\u6709\u89c4\u5f8b\u7f16\u53f7\u7684\u7528\u6237
\n\u5b9e\u65bd\u6b65\u9aa4\uff1a<\/p>\n

\r\n[root@localhost ~]# vim useraddwhile.sh\r\n#!\/bin\/bash\r\nPREFIX=\"stu\"\r\ni=1\r\nwhile\r\n[ $i -le 10 ]\r\ndo\r\nuseradd ${PREFIX}$i\r\necho \"123456\" | passwd --stdin ${PREFIX}$i &> \/dev\/null\r\nlet i++\r\ndone\r\n[root@localhost ~]# sh useraddwhile.sh\r\n[root@localhost ~]# tail \/etc\/passwd\r\nstu1:x:1005:1005::\/home\/stu1:\/bin\/bash\r\nstu2:x:1006:1006::\/home\/stu2:\/bin\/bash\r\nstu3:x:1007:1007::\/home\/stu3:\/bin\/bash\r\nstu4:x:1008:1008::\/home\/stu4:\/bin\/bash\r\nstu5:x:1009:1009::\/home\/stu5:\/bin\/bash\r\nstu6:x:1010:1010::\/home\/stu6:\/bin\/bash\r\nstu7:x:1011:1011::\/home\/stu7:\/bin\/bash\r\nstu8:x:1012:1012::\/home\/stu8:\/bin\/bash\r\nstu9:x:1013:1013::\/home\/stu9:\/bin\/bash\r\nstu10:x:1014:1014::\/home\/stu10:\/bin\/bash\r\n<\/pre>\n

\u5faa\u73af\u4f53\u5185\u90e8\uff0c\u901a\u8fc7\u8bed\u53e5\u201clet i++\u201d\uff08\u7b49\u540c\u4e8ei=expr $i + 1\uff09\u6765\u662f\u53d8\u91cfi\u7684\u503c\u589e\u52a01\uff0c\u4e00\u76f4\u5faa\u73af\uff0c\u76f4\u5230\u6ee1\u8db3\u6761\u4ef6\u4e3a\u6b62\u3002<\/p>\n

\u4e09\u3001case\u5206\u652f\u8bed\u53e5<\/strong><\/div>\n
1.case\u8bed\u53e5\u7684\u7ed3\u6784<\/strong><\/span><\/div>\n

1\uff09case\u8bed\u53e5\u7684\u8bed\u6cd5\u7ed3\u6784<\/strong><\/p>\n

case \u53d8\u91cf\u503c in
\n\u6a21\u5f0f1)
\n\u547d\u4ee4\u5e8f\u52171
\n;;
\n\u6a21\u5f0f2)
\n\u547d\u4ee4\u5e8f\u52172
\n;;
\n\u2026\u2026
\n* )
\n\u9ed8\u8ba4\u547d\u4ee4\u5e8f\u5217
\nesac<\/p>\n

2\uff09case\u8bed\u53e5\u6d41\u7a0b\u56fe<\/strong>
\n\"\"
\n\u4f7f\u7528case\u5206\u652f\u8bed\u53e5\u662f\uff0c\u51e0\u4e2a\u503c\u5f97\u6ce8\u610f\u7684\u7279\u70b9\uff1a
\nShell\u811a\u672c\u5e94\u7528\uff08\u4e09\uff09<\/p>\n

3\uff09case\u8bed\u53e5\u7684\u4f7f\u7528\u573a\u666f<\/strong><\/p>\n

\u67d0\u4e2a\u53d8\u91cf\u5b58\u5728\u591a\u79cd\u53d6\u503c\uff0c\u9700\u8981\u5bf9\u5176\u4e2d\u7684\u6bcf\u4e00\u79cd\u53d6\u503c\u5206\u522b\u6267\u884c\u4e0d\u540c\u7684\u547d\u4ee4\u5e8f\u5217\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0e\u591a\u5206\u652f\u7684if\u8bed\u53e5\u975e\u5e38\u76f8\u4f3c\uff0c\u53ea\u4e0d\u8fc7if\u8bed\u53e5\u9700\u8981\u5224\u65ad\u591a\u4e2a\u4e0d\u540c\u7684\u6761\u4ef6\uff0c\u800ccase\u8bed\u53e5\u53ea\u662f\u5224\u65ad\u4e00\u4e2a\u53d8\u91cf\u7684\u4e0d\u540c\u53d6\u503c\u3002<\/p>\n

2.case\u8bed\u53e5\u5e94\u7528\u5b9e\u4f8b<\/strong><\/span><\/div>\n
\r\n[root@localhost ~]# vim hitkey.sh\r\n#!\/bin\/bash\r\nread -p \"\u8bf7\u8f93\u5165\u4e00\u4e2a\u5b57\u7b26\uff0c\u5e76\u6309Enter\u952e\u786e\u8ba4\uff1a\" KEY\r\ncase \"$KEY\" in\r\n[a-z]|[A-Z])\r\necho \"\u4f60\u8f93\u5165\u7684\u662f\u5b57\u6bcd $KEY\"\r\n;;\r\n[0-9])\r\necho \"\u4f60\u8f93\u5165\u7684\u662f\u6570\u5b57 $KEY\"\r\n;;\r\n*)\r\necho \"\u4f60\u8f93\u5165\u7684\u662f\u975e\u6cd5\u5b57\u7b26 $KEY\"\r\nesac\r\n[root@localhost ~]# sh hitkey.sh\r\n\u8bf7\u8f93\u5165\u4e00\u4e2a\u5b57\u7b26\uff0c\u5e76\u6309Enter\u952e\u786e\u8ba4\uff1a1\r\n\u4f60\u8f93\u5165\u7684\u662f\u6570\u5b57 1\r\n[root@localhost ~]# sh hitkey.sh\r\n\u8bf7\u8f93\u5165\u4e00\u4e2a\u5b57\u7b26\uff0c\u5e76\u6309Enter\u952e\u786e\u8ba4\uff1aw\r\n\u4f60\u8f93\u5165\u7684\u662f\u5b57\u6bcd w\r\n[root@localhost ~]# sh hitkey.sh\r\n\u8bf7\u8f93\u5165\u4e00\u4e2a\u5b57\u7b26\uff0c\u5e76\u6309Enter\u952e\u786e\u8ba4\uff1a@\r\n\u4f60\u8f93\u5165\u7684\u662f\u975e\u6cd5\u5b57\u7b26 @\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

\u5728\u5b9e\u9645\u5de5\u4f5c\u4e2d\uff0c\u7ecf\u5e38\u4f1a\u9047\u5230\u67d0\u9879\u4efb\u52a1\u9700\u8981\u591a\u6b21\u6267\u884c\u7684\u60c5\u51b5\uff0c\u800c\u6bcf\u6b21\u6267\u884c\u65f6\u4ec5\u4ec5\u662f\u5904\u7406\u7684\u5bf9\u8c61\u4e0d\u4e00\u6837\uff0c\u5176\u4ed6\u547d\u4ee4\u76f8\u540c\u3002\u4f8b\u5982\uff1a\u6839 […]<\/p>\n","protected":false},"author":63,"featured_media":156111,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-156107","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\/156107","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=156107"}],"version-history":[{"count":8,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/156107\/revisions"}],"predecessor-version":[{"id":157513,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/156107\/revisions\/157513"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/156111"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=156107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=156107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=156107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}