4.3.1 if\u6761\u4ef6\u6d4b\u8bd5\u8bed\u53e5<\/strong><\/span><\/h6>\nif\u6761\u4ef6\u6d4b\u8bd5\u8bed\u53e5\u53ef\u4ee5\u8ba9\u811a\u672c\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u81ea\u52a8\u6267\u884c\u76f8\u5e94\u7684\u547d\u4ee4\u3002\u4ece\u6280\u672f\u89d2\u5ea6\u6765\u8bb2\uff0cif\u8bed\u53e5\u5206\u4e3a\u5355\u5206\u652f\u7ed3\u6784\u3001\u53cc\u5206\u652f\u7ed3\u6784\u3001\u591a\u5206\u652f\u7ed3\u6784\uff1b\u5176\u590d\u6742\u5ea6\u968f\u7740\u7075\u6d3b\u5ea6\u4e00\u8d77\u9010\u7ea7\u4e0a\u5347\u3002<\/p>\n
if\u6761\u4ef6\u8bed\u53e5\u7684\u5355\u5206\u652f\u7ed3\u6784\u7531if\u3001then\u3001fi\u5173\u952e\u8bcd\u7ec4\u6210\uff0c\u800c\u4e14\u53ea\u5728\u6761\u4ef6\u6210\u7acb\u540e\u624d\u6267\u884c\u9884\u8bbe\u7684\u547d\u4ee4\uff0c\u76f8\u5f53\u4e8e\u53e3\u8bed\u7684\u201c\u5982\u679c\u2026\u2026\u90a3\u4e48\u2026\u2026\u201d\u3002\u5355\u5206\u652f\u7684if\u8bed\u53e5\u5c5e\u4e8e\u6700\u7b80\u5355\u7684\u4e00\u79cd\u6761\u4ef6\u5224\u65ad\u7ed3\u6784\uff0c\u8bed\u6cd5\u683c\u5f0f\u5982\u56fe4-17\u6240\u793a\u3002
\n<\/p>\n
\u56fe4-17\u00a0 \u5355\u5206\u652f\u7684if\u8bed\u53e5<\/p>\n
\u4e0b\u9762\u4f7f\u7528\u5355\u5206\u652f\u7684if\u6761\u4ef6\u8bed\u53e5\u6765\u5224\u65ad\/media\/cdrom\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff0c\u82e5\u5b58\u5728\u5c31\u7ed3\u675f\u6761\u4ef6\u5224\u65ad\u548c\u6574\u4e2aShell\u811a\u672c\uff0c\u53cd\u4e4b\u5219\u53bb\u521b\u5efa\u8fd9\u4e2a\u76ee\u5f55\uff1a<\/p>\n
[root@linuxprobe ~]# vim mkcdrom.sh\r\n#!\/bin\/bash\r\nDIR=\"\/media\/cdrom\"\r\nif [ ! -e $DIR ]\r\nthen\r\nmkdir -p $DIR\r\nfi\r\n<\/pre>\n\u7531\u4e8e\u7b2c5\u7ae0\u624d\u8bb2\u89e3\u7528\u6237\u8eab\u4efd\u4e0e\u6743\u9650\uff0c\u56e0\u6b64\u8fd9\u91cc\u7ee7\u7eed\u7528\u201cbash \u811a\u672c\u540d\u79f0\u201d\u7684\u65b9\u5f0f\u6765\u6267\u884c\u811a\u672c\u3002\u5728\u6b63\u5e38\u60c5\u51b5\u4e0b\uff0c\u987a\u5229\u6267\u884c\u5b8c\u811a\u672c\u6587\u4ef6\u540e\u6ca1\u6709\u4efb\u4f55\u8f93\u51fa\u4fe1\u606f\uff0c\u4f46\u662f\u53ef\u4ee5\u4f7f\u7528ls\u547d\u4ee4\u9a8c\u8bc1\/media\/cdrom\u76ee\u5f55\u662f\u5426\u5df2\u7ecf\u6210\u529f\u521b\u5efa\uff1a<\/p>\n
[root@linuxprobe ~]# bash mkcdrom.sh\r\n[root@linuxprobe ~]# ls -d \/media\/cdrom\r\n\/media\/cdrom<\/pre>\n
\nif\u6761\u4ef6\u8bed\u53e5\u7684\u53cc\u5206\u652f\u7ed3\u6784\u7531if\u3001then\u3001else\u3001fi\u5173\u952e\u8bcd\u7ec4\u6210\uff0c\u5b83\u8fdb\u884c\u4e00\u6b21\u6761\u4ef6\u5339\u914d\u5224\u65ad\uff0c\u5982\u679c\u4e0e\u6761\u4ef6\u5339\u914d\uff0c\u5219\u53bb\u6267\u884c\u76f8\u5e94\u7684\u9884\u8bbe\u547d\u4ee4\uff1b\u53cd\u4e4b\u5219\u53bb\u6267\u884c\u4e0d\u5339\u914d\u65f6\u7684\u9884\u8bbe\u547d\u4ee4\uff0c\u76f8\u5f53\u4e8e\u53e3\u8bed\u7684\u201c\u5982\u679c\u2026\u2026\u90a3\u4e48\u2026\u2026\u6216\u8005\u2026\u2026\u90a3\u4e48\u2026\u2026\u201d\u3002if\u6761\u4ef6\u8bed\u53e5\u7684\u53cc\u5206\u652f\u7ed3\u6784\u4e5f\u662f\u4e00\u79cd\u5f88\u7b80\u5355\u7684\u5224\u65ad\u7ed3\u6784\uff0c\u8bed\u6cd5\u683c\u5f0f\u5982\u56fe4-18\u6240\u793a\u3002<\/p>\n
<\/p>\n
\u56fe4-18\u00a0\u53cc\u5206\u652f\u7684if\u6761\u4ef6\u8bed\u53e5<\/p>\n
\u4e0b\u9762\u4f7f\u7528\u53cc\u5206\u652f\u7684if\u6761\u4ef6\u8bed\u53e5\u6765\u9a8c\u8bc1\u67d0\u53f0\u4e3b\u673a\u662f\u5426\u5728\u7ebf\uff0c\u7136\u540e\u6839\u636e\u8fd4\u56de\u503c\u7684\u7ed3\u679c\uff0c\u8981\u4e48\u663e\u793a\u4e3b\u673a\u5728\u7ebf\u4fe1\u606f\uff0c\u8981\u4e48\u663e\u793a\u4e3b\u673a\u4e0d\u5728\u7ebf\u4fe1\u606f\u3002\u8fd9\u91cc\u7684\u811a\u672c\u4e3b\u8981\u4f7f\u7528ping\u547d\u4ee4\u6765\u6d4b\u8bd5\u4e0e\u5bf9\u65b9\u4e3b\u673a\u7684\u7f51\u7edc\u8054\u901a\u6027\uff0c\u800cLinux\u7cfb\u7edf\u4e2d\u7684ping\u547d\u4ee4\u4e0d\u50cfWindows\u4e00\u6837\u5c1d\u8bd54\u6b21\u5c31\u7ed3\u675f\uff0c\u56e0\u6b64\u4e3a\u4e86\u907f\u514d\u7528\u6237\u7b49\u5f85\u65f6\u95f4\u8fc7\u957f\uff0c\u9700\u8981\u901a\u8fc7-c\u53c2\u6570\u6765\u89c4\u5b9a\u5c1d\u8bd5\u7684\u6b21\u6570\uff0c\u5e76\u4f7f\u7528-i\u53c2\u6570\u5b9a\u4e49\u6bcf\u4e2a\u6570\u636e\u5305\u7684\u53d1\u9001\u95f4\u9694\uff0c\u4ee5\u53ca\u4f7f\u7528-W\u53c2\u6570\u5b9a\u4e49\u7b49\u5f85\u8d85\u65f6\u65f6\u95f4\u3002<\/p>\n
[root@linuxprobe ~]# vim chkhost.sh\r\n#!\/bin\/bash\r\nping -c 3 -i 0.2 -W 3 $1 &> \/dev\/null\r\nif [ $? -eq 0 ]\r\nthen\r\necho \"Host $1 is On-line.\"\r\nelse\r\necho \"Host $1 is Off-line.\"\r\nfi\r\n<\/pre>\n\u6211\u4eec\u57284.2.3\u5c0f\u8282\u4e2d\u7528\u8fc7$?\u53d8\u91cf\uff0c\u4f5c\u7528\u662f\u663e\u793a\u4e0a\u4e00\u6b21\u547d\u4ee4\u7684\u6267\u884c\u8fd4\u56de\u503c\u3002\u82e5\u524d\u9762\u7684\u90a3\u6761\u8bed\u53e5\u6210\u529f\u6267\u884c\uff0c\u5219$?\u53d8\u91cf\u4f1a\u663e\u793a\u6570\u5b570\uff0c\u53cd\u4e4b\u5219\u663e\u793a\u4e00\u4e2a\u975e\u96f6\u7684\u6570\u5b57\uff08\u53ef\u80fd\u4e3a1\uff0c\u4e5f\u53ef\u80fd\u4e3a2\uff0c\u53d6\u51b3\u4e8e\u7cfb\u7edf\u7248\u672c\uff09\u3002\u56e0\u6b64\u53ef\u4ee5\u4f7f\u7528\u6574\u6570\u6bd4\u8f83\u8fd0\u7b97\u7b26\u6765\u5224\u65ad$?\u53d8\u91cf\u662f\u5426\u4e3a0\uff0c\u4ece\u800c\u83b7\u77e5\u90a3\u6761\u8bed\u53e5\u7684\u6700\u7ec8\u5224\u65ad\u60c5\u51b5\u3002\u8fd9\u91cc\u7684\u670d\u52a1\u5668IP\u5730\u5740\u4e3a192.168.10.10\uff0c\u6211\u4eec\u6765\u9a8c\u8bc1\u4e00\u4e0b\u811a\u672c\u7684\u6548\u679c\uff1a<\/p>\n
[root@linuxprobe ~]# bash chkhost.sh 192.168.10.10\r\nHost 192.168.10.10 is On-line.\r\n[root@linuxprobe ~]# bash chkhost.sh 192.168.10.20\r\nHost 192.168.10.20 is Off-line.\r\n<\/pre>\nif\u6761\u4ef6\u8bed\u53e5\u7684\u591a\u5206\u652f\u7ed3\u6784\u7531if\u3001then\u3001else\u3001elif\u3001fi\u5173\u952e\u8bcd\u7ec4\u6210\uff0c\u5b83\u8fdb\u884c\u591a\u6b21\u6761\u4ef6\u5339\u914d\u5224\u65ad\uff0c\u8fd9\u591a\u6b21\u5224\u65ad\u4e2d\u7684\u4efb\u4f55\u4e00\u9879\u5728\u5339\u914d\u6210\u529f\u540e\u90fd\u4f1a\u6267\u884c\u76f8\u5e94\u7684\u9884\u8bbe\u547d\u4ee4\uff0c\u76f8\u5f53\u4e8e\u53e3\u8bed\u7684\u201c\u5982\u679c\u2026\u2026\u90a3\u4e48\u2026\u2026\u5982\u679c\u2026\u2026\u90a3\u4e48\u2026\u2026\u201d\u3002if\u6761\u4ef6\u8bed\u53e5\u7684\u591a\u5206\u652f\u7ed3\u6784\u662f\u5de5\u4f5c\u4e2d\u6700\u5e38\u4f7f\u7528\u7684\u4e00\u79cd\u6761\u4ef6\u5224\u65ad\u7ed3\u6784\uff0c\u5c3d\u7ba1\u76f8\u5bf9\u590d\u6742\u4f46\u662f\u66f4\u52a0\u7075\u6d3b\uff0c\u8bed\u6cd5\u683c\u5f0f\u5982\u56fe4-19\u6240\u793a\u3002 <\/p>\n
\u56fe 4-19 \u591a\u5206\u652f\u7684if\u6761\u4ef6\u8bed\u53e5<\/p>\n
\u4e0b\u9762\u4f7f\u7528\u591a\u5206\u652f\u7684if\u6761\u4ef6\u8bed\u53e5\u6765\u5224\u65ad\u7528\u6237\u8f93\u5165\u7684\u5206\u6570\u5728\u54ea\u4e2a\u6210\u7ee9\u533a\u95f4\u5185\uff0c\u7136\u540e\u8f93\u51fa\u5982Excellent\u3001Pass\u3001Fail\u7b49\u63d0\u793a\u4fe1\u606f\u3002\u5728Linux\u7cfb\u7edf\u4e2d\uff0cread\u662f\u7528\u6765\u8bfb\u53d6\u7528\u6237\u8f93\u5165\u4fe1\u606f\u7684\u547d\u4ee4\uff0c\u80fd\u591f\u628a\u63a5\u6536\u5230\u7684\u7528\u6237\u8f93\u5165\u4fe1\u606f\u8d4b\u503c\u7ed9\u540e\u9762\u7684\u6307\u5b9a\u53d8\u91cf\uff0c-p\u53c2\u6570\u7528\u4e8e\u5411\u7528\u6237\u663e\u793a\u4e00\u5b9a\u7684\u63d0\u793a\u4fe1\u606f\u3002\u5728\u4e0b\u9762\u7684\u811a\u672c\u793a\u4f8b\u4e2d\uff0c\u53ea\u6709\u5f53\u7528\u6237\u8f93\u5165\u7684\u5206\u6570\u5927\u4e8e\u7b49\u4e8e85\u5206\u4e14\u5c0f\u4e8e\u7b49\u4e8e100\u5206\uff0c\u624d\u8f93\u51faExcellent\u5b57\u6837\uff1b\u82e5\u5206\u6570\u4e0d\u6ee1\u8db3\u8be5\u6761\u4ef6\uff08\u5373\u5339\u914d\u4e0d\u6210\u529f\uff09\uff0c\u5219\u7ee7\u7eed\u5224\u65ad\u5206\u6570\u662f\u5426\u5927\u4e8e\u7b49\u4e8e70\u5206\u4e14\u5c0f\u4e8e\u7b49\u4e8e84\u5206\uff0c\u5982\u679c\u662f\uff0c\u5219\u8f93\u51faPass\u5b57\u6837\uff1b\u82e5\u4e24\u6b21\u90fd\u843d\u7a7a\uff08\u5373\u4e24\u6b21\u7684\u5339\u914d\u64cd\u4f5c\u90fd\u5931\u8d25\u4e86\uff09\uff0c\u5219\u8f93\u51faFail\u5b57\u6837\uff1a<\/p>\n
[root@linuxprobe ~]# vim chkscore.sh\r\n#!\/bin\/bash\r\nread -p \"Enter your score\uff080-100\uff09\uff1a\" GRADE\r\nif [ $GRADE -ge 85 ] && [ $GRADE -le 100 ] ; then\r\necho \"$GRADE is Excellent\"\r\nelif [ $GRADE -ge 70 ] && [ $GRADE -le 84 ] ; then\r\necho \"$GRADE is Pass\"\r\nelse\r\necho \"$GRADE is Fail\" \r\nfi\r\n[root@linuxprobe ~]# bash chkscore.sh\r\nEnter your score\uff080-100\uff09\uff1a88\r\n88 is Excellent\r\n[root@linuxprobe ~]# bash chkscore.sh \r\nEnter your score\uff080-100\uff09\uff1a80\r\n80 is Pass\r\n<\/pre>\n\u4e0b\u9762\u6267\u884c\u8be5\u811a\u672c\u3002\u5f53\u7528\u6237\u8f93\u5165\u7684\u5206\u6570\u5206\u522b\u4e3a30\u548c200\u65f6\uff0c\u5176\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n
[root@linuxprobe ~]# bash chkscore.sh\u00a0 \r\nEnter your score\uff080-100\uff09\uff1a30\r\n30 is Fail\r\n[root@linuxprobe ~]# bash chkscore.sh\r\nEnter your score\uff080-100\uff09\uff1a200 \r\n200 is Fail<\/pre>\n\u4e3a\u4ec0\u4e48\u8f93\u5165\u7684\u5206\u6570\u4e3a200\u65f6\uff0c\u4f9d\u7136\u663e\u793aFail\u5462\uff1f\u539f\u56e0\u5f88\u7b80\u5355\u2014\u6ca1\u6709\u6210\u529f\u5339\u914d\u811a\u672c\u4e2d\u7684\u4e24\u4e2a\u6761\u4ef6\u5224\u65ad\u8bed\u53e5\uff0c\u56e0\u6b64\u81ea\u52a8\u6267\u884c\u4e86\u6700\u7ec8\u7684\u515c\u5e95\u7b56\u7565\u3002\u53ef\u89c1\uff0c\u8fd9\u4e2a\u811a\u672c\u8fd8\u4e0d\u662f\u5f88\u5b8c\u7f8e\uff0c\u5efa\u8bae\u8bfb\u8005\u81ea\u884c\u5b8c\u5584\u8fd9\u4e2a\u811a\u672c\uff0c\u4f7f\u5f97\u7528\u6237\u5728\u8f93\u5165\u5927\u4e8e100\u6216\u5c0f\u4e8e0\u7684\u5206\u6570\u65f6\uff0c\u7ed9\u4e88Error\u62a5\u9519\u5b57\u6837\u7684\u63d0\u793a\u3002<\/p>\n
4.3.2 for\u6761\u4ef6\u5faa\u73af\u8bed\u53e5<\/span><\/strong><\/h6>\nfor\u5faa\u73af\u8bed\u53e5\u5141\u8bb8\u811a\u672c\u4e00\u6b21\u6027\u8bfb\u53d6\u591a\u4e2a\u4fe1\u606f\uff0c\u7136\u540e\u9010\u4e00\u5bf9\u4fe1\u606f\u8fdb\u884c\u64cd\u4f5c\u5904\u7406\uff0c\u5f53\u8981\u5904\u7406\u7684\u6570\u636e\u6709\u8303\u56f4\u65f6\uff0c\u4f7f\u7528for\u5faa\u73af\u8bed\u53e5\u518d\u9002\u5408\u4e0d\u8fc7\u4e86\u3002for\u5faa\u73af\u8bed\u53e5\u7684\u8bed\u6cd5\u683c\u5f0f\u5982\u56fe4-20\u6240\u793a\u3002<\/p>\n
<\/p>\n
\u56fe4-20\u00a0 for\u5faa\u73af\u8bed\u53e5\u7684\u8bed\u6cd5\u683c\u5f0f<\/p>\n
\u4e0b\u9762\u4f7f\u7528for\u5faa\u73af\u8bed\u53e5\u4ece\u5217\u8868\u6587\u4ef6\u4e2d\u8bfb\u53d6\u591a\u4e2a\u7528\u6237\u540d\uff0c\u7136\u540e\u4e3a\u5176\u9010\u4e00\u521b\u5efa\u7528\u6237\u8d26\u6237\u5e76\u8bbe\u7f6e\u5bc6\u7801\u3002\u9996\u5148\u521b\u5efa\u7528\u6237\u540d\u79f0\u7684\u5217\u8868\u6587\u4ef6users.txt\uff0c\u6bcf\u4e2a\u7528\u6237\u540d\u79f0\u5355\u72ec\u4e00\u884c\u3002\u8bfb\u8005\u53ef\u4ee5\u81ea\u884c\u51b3\u5b9a\u5177\u4f53\u7684\u7528\u6237\u540d\u79f0\u548c\u4e2a\u6570\uff1a<\/p>\n
[root@linuxprobe ~]# vim users.txt\r\nandy\r\nbarry\r\ncarl\r\nduke\r\neric\r\ngeorge\r\n<\/pre>\n\u63a5\u4e0b\u6765\u7f16\u5199Shell\u811a\u672cExample.sh\u3002\u5728\u811a\u672c\u4e2d\u4f7f\u7528read\u547d\u4ee4\u8bfb\u53d6\u7528\u6237\u8f93\u5165\u7684\u5bc6\u7801\u503c\uff0c\u7136\u540e\u8d4b\u503c\u7ed9PASSWD\u53d8\u91cf\uff0c\u5e76\u901a\u8fc7-p\u53c2\u6570\u5411\u7528\u6237\u663e\u793a\u4e00\u6bb5\u63d0\u793a\u4fe1\u606f\uff0c\u544a\u8bc9\u7528\u6237\u6b63\u5728\u8f93\u5165\u7684\u5185\u5bb9\u5373\u5c06\u4f5c\u4e3a\u8d26\u6237\u5bc6\u7801\u3002\u5728\u6267\u884c\u8be5\u811a\u672c\u540e\uff0c\u4f1a\u81ea\u52a8\u4f7f\u7528\u4ece\u5217\u8868\u6587\u4ef6users.txt\u4e2d\u83b7\u53d6\u5230\u6240\u6709\u7684\u7528\u6237\u540d\u79f0\uff0c\u7136\u540e\u9010\u4e00\u4f7f\u7528\u201cid \u7528\u6237\u540d\u201d\u547d\u4ee4\u67e5\u770b\u7528\u6237\u7684\u4fe1\u606f\uff0c\u5e76\u4f7f\u7528$?\u5224\u65ad\u8fd9\u6761\u547d\u4ee4\u662f\u5426\u6267\u884c\u6210\u529f\uff0c\u4e5f\u5c31\u662f\u5224\u65ad\u8be5\u7528\u6237\u662f\u5426\u5df2\u7ecf\u5b58\u5728\u3002<\/p>\n
\u9700\u8981\u591a\u8bf4\u4e00\u53e5\uff0c\/dev\/null\u662f\u4e00\u4e2a\u88ab\u79f0\u4f5cLinux\u9ed1\u6d1e\u7684\u6587\u4ef6\uff0c\u628a\u8f93\u51fa\u4fe1\u606f\u91cd\u5b9a\u5411\u5230\u8fd9\u4e2a\u6587\u4ef6\u7b49\u540c\u4e8e\u5220\u9664\u6570\u636e\uff08\u7c7b\u4f3c\u4e8e\u6ca1\u6709\u56de\u6536\u529f\u80fd\u7684\u5783\u573e\u7bb1\uff09\uff0c\u53ef\u4ee5\u8ba9\u7528\u6237\u7684\u5c4f\u5e55\u7a97\u53e3\u4fdd\u6301\u7b80\u6d01\u3002<\/p>\n
[root@linuxprobe ~]# vim Example.sh\r\n#!\/bin\/bash\r\nread -p \"Enter The Users Password : \" PASSWD\r\nfor UNAME in `cat users.txt`\r\ndo\r\nid $UNAME &> \/dev\/null\r\nif [ $? -eq 0 ]\r\nthen\r\necho \"Already exists\"\r\nelse\r\nuseradd $UNAME &> \/dev\/null\r\necho \"$PASSWD\" | passwd --stdin $UNAME &> \/dev\/null\r\nif [ $? -eq 0 ]\r\nthen\r\necho \"$UNAME , Create success\"\r\nelse\r\necho \"$UNAME ,\u00a0Create failure\"\r\nfi\r\nfi\r\ndone\r\n<\/pre>\n\u6267\u884c\u6279\u91cf\u521b\u5efa\u7528\u6237\u7684Shell\u811a\u672cExample.sh\uff0c\u5728\u8f93\u5165\u4e3a\u8d26\u6237\u8bbe\u5b9a\u7684\u5bc6\u7801\u540e\u5c06\u7531\u811a\u672c\u81ea\u52a8\u68c0\u67e5\u5e76\u521b\u5efa\u8fd9\u4e9b\u8d26\u6237\u3002\u7531\u4e8e\u5df2\u7ecf\u5c06\u591a\u4f59\u7684\u4fe1\u606f\u901a\u8fc7\u8f93\u51fa\u91cd\u5b9a\u5411\u7b26\u8f6c\u79fb\u5230\u4e86\/dev\/null\u9ed1\u6d1e\u6587\u4ef6\u4e2d\uff0c\u56e0\u6b64\u5728\u6b63\u5e38\u60c5\u51b5\u4e0b\u5c4f\u5e55\u7a97\u53e3\u9664\u4e86\u201c\u7528\u6237\u8d26\u6237\u521b\u5efa\u6210\u529f\u201d\uff08Create success\uff09\u7684\u63d0\u793a\u540e\u4e0d\u4f1a\u6709\u5176\u4ed6\u5185\u5bb9\u3002<\/p>\n
\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\/etc\/passwd\u662f\u7528\u6765\u4fdd\u5b58\u7528\u6237\u8d26\u6237\u4fe1\u606f\u7684\u6587\u4ef6\u3002\u5982\u679c\u60f3\u786e\u8ba4\u8fd9\u4e2a\u811a\u672c\u662f\u5426\u6210\u529f\u521b\u5efa\u4e86\u7528\u6237\u8d26\u6237\uff0c\u53ef\u4ee5\u6253\u5f00\u8fd9\u4e2a\u6587\u4ef6\uff0c\u770b\u5176\u4e2d\u662f\u5426\u6709\u8fd9\u4e9b\u65b0\u521b\u5efa\u7684\u7528\u6237\u4fe1\u606f\u3002<\/p>\n
[root@linuxprobe ~]# bash Example.sh\r\nEnter The Users Password : linuxprobe\r\nandy , Create success\r\nbarry , Create success\r\ncarl , Create success\r\nduke , Create success\r\neric , Create success\r\ngeorge , Create success\r\n[root@linuxprobe ~]# tail -6 \/etc\/passwd\r\nandy:x:1001:1001::\/home\/andy:\/bin\/bash\r\nbarry:x:1002:1002::\/home\/barry:\/bin\/bash\r\ncarl:x:1003:1003::\/home\/carl:\/bin\/bash\r\nduke:x:1004:1004::\/home\/duke:\/bin\/bash\r\neric:x:1005:1005::\/home\/eric:\/bin\/bash\r\ngeorge:x:1006:1006::\/home\/george:\/bin\/bash\r\n<\/pre>\n\u60a8\u8fd8\u8bb0\u5f97\u5728\u5b66\u4e60\u53cc\u5206\u652fif\u6761\u4ef6\u8bed\u53e5\u65f6\uff0c\u7528\u5230\u7684\u90a3\u4e2a\u6d4b\u8bd5\u4e3b\u673a\u662f\u5426\u5728\u7ebf\u7684\u811a\u672c\u4e48\uff1f\u65e2\u7136\u6211\u4eec\u73b0\u5728\u5df2\u7ecf\u638c\u63e1\u4e86for\u5faa\u73af\u8bed\u53e5\uff0c\u4e0d\u59a8\u505a\u4e9b\u66f4\u9177\u7684\u4e8b\u60c5\uff0c\u6bd4\u5982\u5c1d\u8bd5\u8ba9\u811a\u672c\u4ece\u6587\u672c\u4e2d\u81ea\u52a8\u8bfb\u53d6\u4e3b\u673a\u5217\u8868\uff0c\u7136\u540e\u81ea\u52a8\u9010\u4e2a\u6d4b\u8bd5\u8fd9\u4e9b\u4e3b\u673a\u662f\u5426\u5728\u7ebf\u3002<\/p>\n
\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u4e3b\u673a\u5217\u8868\u6587\u4ef6ipadds.txt\uff1a<\/p>\n
[root@linuxprobe ~]# vim\u00a0ipadds.txt\r\n192.168.10.10\r\n192.168.10.11\r\n192.168.10.12\r\n<\/pre>\n\u7136\u540e\u524d\u9762\u7684\u53cc\u5206\u652fif\u6761\u4ef6\u8bed\u53e5\u4e0efor\u5faa\u73af\u8bed\u53e5\u76f8\u7ed3\u5408\uff0c\u8ba9\u811a\u672c\u4ece\u4e3b\u673a\u5217\u8868\u6587\u4ef6ipadds.txt\u4e2d\u81ea\u52a8\u8bfb\u53d6IP\u5730\u5740\uff08\u7528\u6765\u8868\u793a\u4e3b\u673a\uff09\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9HLIST\u53d8\u91cf\uff0c\u4ece\u800c\u901a\u8fc7\u5224\u65adping\u547d\u4ee4\u6267\u884c\u540e\u7684\u8fd4\u56de\u503c\u6765\u9010\u4e2a\u6d4b\u8bd5\u4e3b\u673a\u662f\u5426\u5728\u7ebf\u3002\u811a\u672c\u4e2d\u51fa\u73b0\u7684$\uff08\u547d\u4ee4\uff09\u662f\u4e00\u79cd\u5b8c\u5168\u7c7b\u4f3c\u4e8e\u7b2c3\u7ae0\u7684\u8f6c\u4e49\u5b57\u7b26\u4e2d\u53cd\u5f15\u53f7`\u547d\u4ee4`\u7684Shell\u64cd\u4f5c\u7b26\uff0c\u6548\u679c\u540c\u6837\u662f\u6267\u884c\u62ec\u53f7\u6216\u53cc\u5f15\u53f7\u62ec\u8d77\u6765\u7684\u5b57\u7b26\u4e32\u4e2d\u7684\u547d\u4ee4\u3002\u5927\u5bb6\u5728\u7f16\u5199\u811a\u672c\u65f6\uff0c\u591a\u5b66\u4e60\u51e0\u79cd\u7c7b\u4f3c\u7684\u65b0\u65b9\u6cd5\uff0c\u53ef\u5728\u5de5\u4f5c\u4e2d\u5927\u663e\u8eab\u624b\uff1a<\/p>\n
[root@linuxprobe\u00a0~]# vim CheckHosts.sh\r\n#!\/bin\/bash\r\nHLIST=$(cat ~\/ipadds.txt)\r\nfor IP in $HLIST\r\ndo\r\nping -c 3 -i 0.2 -W 3 $IP &> \/dev\/null\r\nif [ $? -eq 0 ] ; then\r\necho \"Host $IP is On-line.\"\r\nelse\r\necho \"Host $IP is Off-line.\"\r\nfi\r\ndone\r\n[root@linuxprobe ~]# .\/CheckHosts.sh\r\nHost 192.168.10.10 is On-line.\r\nHost 192.168.10.11 is Off-line.\r\nHost 192.168.10.12 is Off-line.\r\n<\/pre>\n4.3.3 while\u6761\u4ef6\u5faa\u73af\u8bed\u53e5<\/strong><\/span><\/h6>\nwhile\u6761\u4ef6\u5faa\u73af\u8bed\u53e5\u662f\u4e00\u79cd\u8ba9\u811a\u672c\u6839\u636e\u67d0\u4e9b\u6761\u4ef6\u6765\u91cd\u590d\u6267\u884c\u547d\u4ee4\u7684\u8bed\u53e5\uff0c\u5b83\u7684\u5faa\u73af\u7ed3\u6784\u5f80\u5f80\u5728\u6267\u884c\u524d\u5e76\u4e0d\u786e\u5b9a\u6700\u7ec8\u6267\u884c\u7684\u6b21\u6570\uff0c\u5b8c\u5168\u4e0d\u540c\u4e8efor\u5faa\u73af\u8bed\u53e5\u4e2d\u6709\u76ee\u6807\u3001\u6709\u8303\u56f4\u7684\u4f7f\u7528\u573a\u666f\u3002while\u5faa\u73af\u8bed\u53e5\u901a\u8fc7\u5224\u65ad\u6761\u4ef6\u6d4b\u8bd5\u7684\u771f\u5047\u6765\u51b3\u5b9a\u662f\u5426\u7ee7\u7eed\u6267\u884c\u547d\u4ee4\uff0c\u82e5\u6761\u4ef6\u4e3a\u771f\u5c31\u7ee7\u7eed\u6267\u884c\uff0c\u4e3a\u5047\u5c31\u7ed3\u675f\u5faa\u73af\u3002while\u8bed\u53e5\u7684\u8bed\u6cd5\u683c\u5f0f\u5982\u56fe4-21\u6240\u793a\u3002 <\/p>\n
\u56fe4-21\u00a0 while\u5faa\u73af\u8bed\u53e5\u7684\u8bed\u6cd5\u683c\u5f0f<\/p>\n
\u63a5\u4e0b\u6765\u7ed3\u5408\u4f7f\u7528\u591a\u5206\u652f\u7684if\u6761\u4ef6\u6d4b\u8bd5\u8bed\u53e5\u4e0ewhile\u6761\u4ef6\u5faa\u73af\u8bed\u53e5\uff0c\u7f16\u5199\u4e00\u4e2a\u7528\u6765\u731c\u6d4b\u6570\u503c\u5927\u5c0f\u7684\u811a\u672cGuess.sh\u3002\u8be5\u811a\u672c\u4f7f\u7528$RANDOM\u53d8\u91cf\u6765\u8c03\u53d6\u51fa\u4e00\u4e2a\u968f\u673a\u7684\u6570\u503c\uff08\u8303\u56f4\u4e3a0\uff5e32767\uff09\uff0c\u5c06\u8fd9\u4e2a\u968f\u673a\u6570\u5bf91000\u8fdb\u884c\u53d6\u4f59\u64cd\u4f5c\uff0c\u5e76\u4f7f\u7528expr\u547d\u4ee4\u53d6\u5f97\u5176\u7ed3\u679c\uff0c\u518d\u7528\u8fd9\u4e2a\u6570\u503c\u4e0e\u7528\u6237\u901a\u8fc7read\u547d\u4ee4\u8f93\u5165\u7684\u6570\u503c\u8fdb\u884c\u6bd4\u8f83\u5224\u65ad\u3002\u8fd9\u4e2a\u5224\u65ad\u8bed\u53e5\u5206\u4e3a\u4e09\u79cd\u60c5\u51b5\uff0c\u5206\u522b\u662f\u5224\u65ad\u7528\u6237\u8f93\u5165\u7684\u6570\u503c\u662f\u7b49\u4e8e\u3001\u5927\u4e8e\u8fd8\u662f\u5c0f\u4e8e\u4f7f\u7528expr\u547d\u4ee4\u53d6\u5f97\u7684\u6570\u503c\u3002\u5f53\u524d\uff0c\u73b0\u5728\u8fd9\u4e9b\u5185\u5bb9\u4e0d\u662f\u91cd\u70b9\uff0c\u6211\u4eec\u5f53\u524d\u8981\u5173\u6ce8\u7684\u662fwhile\u6761\u4ef6\u5faa\u73af\u8bed\u53e5\u4e2d\u7684\u6761\u4ef6\u6d4b\u8bd5\u59cb\u7ec8\u4e3atrue\uff0c\u56e0\u6b64\u5224\u65ad\u8bed\u53e5\u4f1a\u65e0\u9650\u6267\u884c\u4e0b\u53bb\uff0c\u76f4\u5230\u7528\u6237\u8f93\u5165\u7684\u6570\u503c\u7b49\u4e8eexpr\u547d\u4ee4\u53d6\u5f97\u7684\u6570\u503c\u540e\uff0c\u8fd9\u4e24\u8005\u76f8\u7b49\u4e4b\u540e\u624d\u8fd0\u884cexit 0\u547d\u4ee4\uff0c\u7ec8\u6b62\u811a\u672c\u7684\u6267\u884c\u3002<\/p>\n
[root@linuxprobe ~]# vim Guess.sh\r\n#!\/bin\/bash\r\nPRICE=$(expr $RANDOM % 1000)\r\nTIMES=0\r\necho \"\u5546\u54c1\u5b9e\u9645\u4ef7\u683c\u4e3a0-999\u4e4b\u95f4\uff0c\u731c\u731c\u770b\u662f\u591a\u5c11\uff1f\"\r\nwhile true<\/strong>\r\ndo\r\nread -p \"\u8bf7\u8f93\u5165\u60a8\u731c\u6d4b\u7684\u4ef7\u683c\u6570\u76ee\uff1a\" INT\r\nlet TIMES++\r\nif [ $INT -eq $PRICE ] ; then\r\necho \"\u606d\u559c\u60a8\u7b54\u5bf9\u4e86\uff0c\u5b9e\u9645\u4ef7\u683c\u662f $PRICE\"\r\necho \"\u60a8\u603b\u5171\u731c\u6d4b\u4e86 $TIMES \u6b21\"\r\nexit 0\r\nelif [ $INT -gt $PRICE ] ; then\r\necho \"\u592a\u9ad8\u4e86\uff01\"\r\nelse\r\necho \"\u592a\u4f4e\u4e86\uff01\"\r\nfi\r\ndone\r\n<\/pre>\n\u5728\u8fd9\u4e2aGuess.sh\u811a\u672c\u4e2d\uff0c\u6211\u4eec\u6dfb\u52a0\u4e86\u4e00\u4e9b\u4ea4\u4e92\u5f0f\u7684\u4fe1\u606f\uff0c\u4ece\u800c\u4f7f\u5f97\u7528\u6237\u4e0e\u7cfb\u7edf\u7684\u4e92\u52a8\u6027\u5f97\u4ee5\u589e\u5f3a\u3002\u800c\u4e14\u6bcf\u5f53\u5faa\u73af\u5230let TIMES++\u547d\u4ee4\u65f6\u90fd\u4f1a\u8ba9TIMES\u53d8\u91cf\u5185\u7684\u6570\u503c\u52a01\uff0c\u7528\u6765\u7edf\u8ba1\u5faa\u73af\u603b\u8ba1\u6267\u884c\u4e86\u591a\u5c11\u6b21\u3002\u8fd9\u53ef\u4ee5\u8ba9\u7528\u6237\u5f97\u77e5\u603b\u5171\u731c\u6d4b\u4e86\u591a\u5c11\u6b21\u4e4b\u540e\uff0c\u624d\u731c\u5bf9\u4ef7\u683c\u3002<\/p>\n
[root@linuxprobe ~]# bash Guess.sh\r\n\u5546\u54c1\u5b9e\u9645\u4ef7\u683c\u4e3a0-999\u4e4b\u95f4\uff0c\u731c\u731c\u770b\u662f\u591a\u5c11\uff1f\r\n\u8bf7\u8f93\u5165\u60a8\u731c\u6d4b\u7684\u4ef7\u683c\u6570\u76ee\uff1a500\r\n\u592a\u4f4e\u4e86\uff01\r\n\u8bf7\u8f93\u5165\u60a8\u731c\u6d4b\u7684\u4ef7\u683c\u6570\u76ee\uff1a800\r\n\u592a\u9ad8\u4e86\uff01\r\n\u8bf7\u8f93\u5165\u60a8\u731c\u6d4b\u7684\u4ef7\u683c\u6570\u76ee\uff1a650\r\n\u592a\u4f4e\u4e86\uff01\r\n\u8bf7\u8f93\u5165\u60a8\u731c\u6d4b\u7684\u4ef7\u683c\u6570\u76ee\uff1a720\r\n\u592a\u9ad8\u4e86\uff01\r\n\u8bf7\u8f93\u5165\u60a8\u731c\u6d4b\u7684\u4ef7\u683c\u6570\u76ee\uff1a690\r\n\u592a\u4f4e\u4e86\uff01\r\n\u8bf7\u8f93\u5165\u60a8\u731c\u6d4b\u7684\u4ef7\u683c\u6570\u76ee\uff1a700\r\n\u592a\u9ad8\u4e86\uff01\r\n\u8bf7\u8f93\u5165\u60a8\u731c\u6d4b\u7684\u4ef7\u683c\u6570\u76ee\uff1a695\r\n\u592a\u9ad8\u4e86\uff01\r\n\u8bf7\u8f93\u5165\u60a8\u731c\u6d4b\u7684\u4ef7\u683c\u6570\u76ee\uff1a692\r\n\u592a\u9ad8\u4e86\uff01\r\n\u8bf7\u8f93\u5165\u60a8\u731c\u6d4b\u7684\u4ef7\u683c\u6570\u76ee\uff1a691\r\n\u606d\u559c\u60a8\u7b54\u5bf9\u4e86\uff0c\u5b9e\u9645\u4ef7\u683c\u662f 691\r\n\u60a8\u603b\u5171\u731c\u6d4b\u4e86 9 \u6b21\r\n<\/pre>\n4.3.4 case\u6761\u4ef6\u6d4b\u8bd5\u8bed\u53e5<\/strong><\/span><\/h6>\n\u5982\u679c\u60a8\u4e4b\u524d\u5b66\u4e60\u8fc7C\u8bed\u8a00\uff0c\u770b\u5230\u8fd9\u4e00\u5c0f\u8282\u7684\u6807\u9898\u80af\u5b9a\u4f1a\u4f1a\u5fc3\u4e00\u7b11\u201c\u8fd9\u4e0d\u5c31\u662fswitch\u8bed\u53e5\u561b\uff01\u201d\u662f\u7684\uff0ccase\u6761\u4ef6\u6d4b\u8bd5\u8bed\u53e5\u548cswitch\u8bed\u53e5\u7684\u529f\u80fd\u975e\u5e38\u76f8\u4f3c\uff01case\u8bed\u53e5\u662f\u5728\u591a\u4e2a\u8303\u56f4\u5185\u5339\u914d\u6570\u636e\uff0c\u82e5\u5339\u914d\u6210\u529f\u5219\u6267\u884c\u76f8\u5173\u547d\u4ee4\u5e76\u7ed3\u675f\u6574\u4e2a\u6761\u4ef6\u6d4b\u8bd5\uff1b\u800c\u5982\u679c\u6570\u636e\u4e0d\u5728\u6240\u5217\u51fa\u7684\u8303\u56f4\u5185\uff0c\u5219\u4f1a\u53bb\u6267\u884c\u661f\u53f7\uff08*\uff09\u4e2d\u6240\u5b9a\u4e49\u7684\u9ed8\u8ba4\u547d\u4ee4\u3002case\u8bed\u53e5\u7684\u8bed\u6cd5\u7ed3\u6784\u5982\u56fe4-22\u6240\u793a\u3002<\/p>\n
<\/p>\n
\u56fe4-22\u00a0 case\u6761\u4ef6\u6d4b\u8bd5\u8bed\u53e5\u7684\u8bed\u6cd5\u7ed3\u6784<\/p>\n
\u5728\u524d\u6587\u4ecb\u7ecd\u7684Guess.sh\u811a\u672c\u4e2d\u6709\u4e00\u4e2a\u81f4\u547d\u7684\u5f31\u70b9\u2014\u53ea\u80fd\u63a5\u53d7\u6570\u5b57\uff01\u60a8\u53ef\u4ee5\u5c1d\u8bd5\u8f93\u5165\u4e00\u4e2a\u5b57\u6bcd\uff0c\u4f1a\u53d1\u73b0\u811a\u672c\u7acb\u5373\u5c31\u5d29\u6e83\u4e86\u3002\u539f\u56e0\u662f\u5b57\u6bcd\u65e0\u6cd5\u4e0e\u6570\u5b57\u8fdb\u884c\u5927\u5c0f\u6bd4\u8f83\uff0c\u4f8b\u5982\uff0c\u201ca\u662f\u5426\u5927\u4e8e\u7b49\u4e8e3\u201d\u8fd9\u6837\u7684\u547d\u9898\u662f\u5b8c\u5168\u9519\u8bef\u7684\u3002\u6211\u4eec\u5fc5\u987b\u6709\u4e00\u5b9a\u7684\u63aa\u65bd\u6765\u5224\u65ad\u7528\u6237\u7684\u8f93\u5165\u5185\u5bb9\uff0c\u5f53\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u4e0d\u662f\u6570\u5b57\u65f6\uff0c\u811a\u672c\u80fd\u4e88\u4ee5\u63d0\u793a\uff0c\u4ece\u800c\u514d\u4e8e\u5d29\u6e83\u3002<\/p>\n
\u901a\u8fc7\u5728\u811a\u672c\u4e2d\u7ec4\u5408\u4f7f\u7528case\u6761\u4ef6\u6d4b\u8bd5\u8bed\u53e5\u548c\u901a\u914d\u7b26\uff08\u8be6\u89c1\u7b2c3\u7ae0\uff09\uff0c\u5b8c\u5168\u53ef\u4ee5\u6ee1\u8db3\u8fd9\u91cc\u7684\u9700\u6c42\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u7f16\u5199\u811a\u672cCheckkeys.sh\uff0c\u63d0\u793a\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u5b57\u7b26\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9\u53d8\u91cfKEY\uff0c\u7136\u540e\u6839\u636e\u53d8\u91cfKEY\u7684\u503c\u5411\u7528\u6237\u663e\u793a\u5176\u503c\u662f\u5b57\u6bcd\u3001\u6570\u5b57\u8fd8\u662f\u5176\u4ed6\u5b57\u7b26\u3002<\/p>\n
[root@linuxprobe ~]# vim Checkkeys.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 \"\u60a8\u8f93\u5165\u7684\u662f \u5b57\u6bcd\u3002\"\r\n;;\r\n[0-9])\r\necho \"\u60a8\u8f93\u5165\u7684\u662f \u6570\u5b57\u3002\"\r\n;;\r\n*)\r\necho \"\u60a8\u8f93\u5165\u7684\u662f \u7a7a\u683c\u3001\u529f\u80fd\u952e\u6216\u5176\u4ed6\u63a7\u5236\u5b57\u7b26\u3002\"\r\nesac\r\n[root@linuxprobe ~]# bash Checkkeys.sh\r\n\u8bf7\u8f93\u5165\u4e00\u4e2a\u5b57\u7b26\uff0c\u5e76\u6309Enter\u952e\u786e\u8ba4\uff1a6\r\n\u60a8\u8f93\u5165\u7684\u662f \u6570\u5b57\u3002\r\n[root@linuxprobe ~]# bash Checkkeys.sh\r\n\u8bf7\u8f93\u5165\u4e00\u4e2a\u5b57\u7b26\uff0c\u5e76\u6309Enter\u952e\u786e\u8ba4\uff1ap\r\n\u60a8\u8f93\u5165\u7684\u662f \u5b57\u6bcd\u3002\r\n[root@linuxprobe ~]# bash Checkkeys.sh\r\n\u8bf7\u8f93\u5165\u4e00\u4e2a\u5b57\u7b26\uff0c\u5e76\u6309Enter\u952e\u786e\u8ba4\uff1a^[[15~\r\n\u60a8\u8f93\u5165\u7684\u662f \u7a7a\u683c\u3001\u529f\u80fd\u952e\u6216\u5176\u4ed6\u63a7\u5236\u5b57\u7b26\u3002\r\n<\/pre>\n