{"id":178,"date":"2013-06-09T10:04:32","date_gmt":"2013-06-09T02:04:32","guid":{"rendered":"http:\/\/www.zyuns.com\/?p=178"},"modified":"2015-02-15T20:59:41","modified_gmt":"2015-02-15T12:59:41","slug":"wo-de-yi-xie-jian-dan-de-shell-jiao-ben-shi-li","status":"publish","type":"post","link":"https:\/\/www.siediyer.cn\/?p=178","title":{"rendered":"\u6211\u7684\u4e00\u4e9b\u7b80\u5355\u7684shell\u811a\u672c\u5b9e\u4f8b"},"content":{"rendered":"<div>\n<p>\u81ea\u5df1\u5199\u4e86\u4e00\u4e0b\u5c0f\u7684shell\u5b9e\u4f8b\uff0c\u867d\u7136\u5f88\u5c0f\uff0c\u4f46\u6240\u6709\u7684\u5927\u7684\u7a0b\u5e8f\u90fd\u662f\u7531\u5c0f\u7684\u6a21\u5757\u5806\u79ef\u8d77\u6765\u7684\uff0c\u7a0b\u5e8f\u5458\u4e00\u5b9a\u8981\u61c2\u5f97\u4e00\u79cd\u811a\u672c\u7684\u4e66\u5199\uff0c\u800c\u6211\uff0c\u53ea\u4f1a\u5728linux\u4e0b\u5de5\u4f5c\uff0c\u6240\u4ee5\u5c31\u53ea\u80fd\u5199linux\u7684shell\u811a\u672c\u4e86\uff0c\u5475\u5475\uff0c\u672c\u6587\u4f1a\u9646\u7eed\u66f4\u65b0\uff0c\u7ed9\u81ea\u5df1\u52a0\u6cb9\uff01<\/p>\n<p>1.\u6a21\u62dflinnux\u767b\u5f55shell<\/p>\n<p>#\/bin\/bash<br \/>\necho -n &#8220;login:&#8221;<br \/>\nread name<br \/>\necho -n &#8220;password:&#8221;<br \/>\nread passwd<br \/>\nif [ $name = &#8220;cht&#8221; -a $passwd = &#8220;abc&#8221; ];then<br \/>\necho &#8220;the host and password is right!&#8221;<br \/>\nelse echo &#8220;input is error!&#8221;<br \/>\nfi<\/p>\n<p>2.\u6bd4\u8f83\u4e24\u4e2a\u6570\u5927\u5c0f<\/p>\n<p>#\/bin\/bash<br \/>\necho &#8220;please enter two number&#8221;<br \/>\nread a<br \/>\nread b<br \/>\nif test $a -eq $b<br \/>\nthen echo &#8220;NO.1 = NO.2&#8221;<br \/>\nelif test $a -gt $b<br \/>\nthen echo &#8220;NO.1 &gt; NO.2&#8221;<br \/>\nelse echo &#8220;NO.1 &lt; NO.2&#8221;<br \/>\nfi<\/p>\n<p>3.\u67e5\u627e\/root\/\u76ee\u5f55\u4e0b\u662f\u5426\u5b58\u5728\u8be5\u6587\u4ef6<\/p>\n<p>#\/bin\/bash<br \/>\necho &#8220;enter a file name:&#8221;<br \/>\nread a<br \/>\nif test\u00a0 -e \/root\/$a<br \/>\nthen echo &#8220;the file is exist!&#8221;<br \/>\nelse echo &#8220;the file is not exist!&#8221;<br \/>\nfi<\/p>\n<p>4.for\u5faa\u73af\u7684\u4f7f\u7528<\/p>\n<p>#\/bin\/bash<br \/>\nclear<br \/>\nfor num in 1 2 3 4 5 6 7 8 9 10<br \/>\ndo<br \/>\necho &#8220;$num&#8221;<br \/>\ndone<\/p>\n<p>5.<\/p>\n<p>#\/bin\/bash<br \/>\necho &#8220;Please enter a user:&#8221;<br \/>\nread a<br \/>\nb=$(whoami)<br \/>\nif test $a = $b<br \/>\nthen echo &#8220;the user is running.&#8221;<br \/>\nelse echo &#8220;the user is not running.&#8221;<br \/>\nfi<\/p>\n<p>6.\u5220\u9664\u5f53\u524d\u76ee\u5f55\u4e0b\u5927\u5c0f\u4e3a0\u7684\u6587\u4ef6<\/p>\n<p>#\/bin\/bash<br \/>\nfor filename in `ls`<br \/>\ndo<br \/>\nif test -d $filename<br \/>\nthen b=0<br \/>\nelse<br \/>\na=$(ls -l $filename | awk &#8216;{ print $5 }&#8217;)<br \/>\nif test $a -eq 0<br \/>\nthen rm $filename<br \/>\nfi<br \/>\nfi<br \/>\ndone<\/p>\n<p>7.\u5982\u679c\/export\/um_lpp_source\u4e0b\u6709\u6587\u4ef6\uff0c\u90a3\u4e48\u5c06\u5176\u6587\u4ef6\u7cfb\u7edf\u5927\u5c0f\u6539\u4e3a3G<\/p>\n<p>#\/bin\/bash<br \/>\nwhile line=`ls \/export\/um_lpp_source`<br \/>\ndo<br \/>\nif test $line=&#8221;&#8221;<br \/>\nthen\u00a0 echo &#8220;NULL&#8221;<br \/>\nsleep 1<br \/>\nelse echo $line<br \/>\nchfs -a size=3G \/export\/um_lpp_source<br \/>\nexit 0<br \/>\nfi<br \/>\ndone<\/p>\n<p>&nbsp;<\/p>\n<p>8.\u6d4b\u8bd5IP\u5730\u5740<\/p>\n<p>#\/bin\/bash<br \/>\nfor i in\u00a0 1 2 3 4 5 6 7 8 9<br \/>\ndo<br \/>\necho &#8220;the number of $i computer is &#8221;<br \/>\nping -c 1 192.168.0.$i<br \/>\ndone<\/p>\n<p>9.\u5982\u679ctest.log\u7684\u5927\u5c0f\u5927\u4e8e0\uff0c\u90a3\u4e48\u5c06\/opt\u76ee\u5f55\u4e0b\u7684*.tar.gz\u6587\u4ef6<\/p>\n<p>#\/bin\/sh<br \/>\na=2<br \/>\nwhile name=&#8221;test.log&#8221;<br \/>\ndo<br \/>\nsleep 1<br \/>\nb=$(ls -l $name | awk &#8216;{print $5}&#8217;)<br \/>\nif test $b -ge $a<br \/>\n#then echo &#8220;OK&#8221;<br \/>\nthen `cp \/opt\/*.tar.gz .`<br \/>\nexit 0<br \/>\nfi<br \/>\ndone<\/p>\n<p>10.\u6253\u5370\u8bfb\u53d6\u7684\u5185\u5bb9\uff0c\u4e3a\u4e0b\u9762\u7684\u4f8b\u5b50\u505a\u51c6\u5907<\/p>\n<p>#\/bin\/bash<br \/>\nwhile read name<br \/>\ndo<br \/>\necho $name<br \/>\ndone<\/p>\n<p>11.\u4ece0.sh\u4e2d\u8bfb\u53d6\u5185\u5bb9\u5e76\u6253\u5370<\/p>\n<p>#\/bin\/bash<br \/>\nwhile read line<br \/>\ndo<br \/>\necho $line<br \/>\ndone &lt; 0.sh<\/p>\n<p>12.\u8bfb\u53d6a.c\u4e2d\u7684\u5185\u5bb9\u5e76\u505a\u52a01\u8fd0\u7b97<\/p>\n<p>#\/bin\/bash<br \/>\ntest -e a.c<br \/>\nwhile read line<br \/>\ndo<br \/>\na=$(($line+1))<br \/>\ndone &lt; a.c<br \/>\necho $a<\/p>\n<p>13.\u666e\u901a\u65e0\u53c2\u6570\u51fd\u6570<\/p>\n<p>#\/bin\/bash<br \/>\np ()<br \/>\n{<br \/>\necho &#8220;hello&#8221;<br \/>\n}<br \/>\np<\/p>\n<p>14.\u7ed9\u51fd\u6570\u4f20\u9012\u53c2\u6570<\/p>\n<p>#\/bin\/bash<br \/>\np_num ()<br \/>\n{<br \/>\nnum=$1<br \/>\necho $num<br \/>\n}<br \/>\nfor n in $@<br \/>\ndo<br \/>\np_num $n<br \/>\ndone<\/p>\n<p>15.\u521b\u5efa\u6587\u4ef6\u5939<\/p>\n<p>#\/bin\/bash<br \/>\nwhile :<br \/>\ndo<br \/>\necho &#8220;please input file&#8217;s name:&#8221;<br \/>\nread a<br \/>\nif test -e \/root\/$a<br \/>\nthen<br \/>\necho &#8220;the file is existing Please input new file name:&#8221;<br \/>\nelse<br \/>\nmkdir $a<br \/>\necho &#8220;you aye sussesful!&#8221;<br \/>\nbreak<br \/>\nfi<br \/>\ndone<\/p>\n<p>16.\u83b7\u53d6\u672c\u673aIP\u5730\u5740<\/p>\n<p>#\/bin\/bash<br \/>\nifconfig | grep &#8220;inet addr:&#8221; | awk &#8216;{ print $2 }&#8217;| sed &#8216;s\/addr:\/\/g&#8217;<\/p>\n<p>17.\u67e5\u627e\u6700\u5927\u6587\u4ef6<\/p>\n<p>#\/bin\/bash<br \/>\na=0<br \/>\nfor\u00a0 name in *.*<br \/>\ndo<br \/>\nb=$(ls -l $name | awk &#8216;{print $5}&#8217;)<br \/>\nif test $b -ge $a<br \/>\nthen a=$b<br \/>\nnamemax=$name<br \/>\nfi<br \/>\ndone<br \/>\necho &#8220;the max file is $namemax&#8221;<\/p>\n<p>18.\u67e5\u627e\u5f53\u524d\u7f51\u6bb5\u5185IP\u7528\u6237\uff0c\u91cd\u5b9a\u5411\u5230ip.txt\u6587\u4ef6\u4e2d<\/p>\n<p>#\/bin\/bash<br \/>\na=1<br \/>\nwhile :<br \/>\ndo<br \/>\na=$(($a+1))<br \/>\nif test $a -gt 255<br \/>\nthen break<br \/>\nelse<br \/>\necho $(ping -c 1 192.168.0.$a | grep &#8220;ttl&#8221; | awk &#8216;{print $4}&#8217;| sed &#8216;s\/:\/\/g&#8217;)<br \/>\nip=$(ping -c 1 192.168.0.$a | grep &#8220;ttl&#8221; | awk &#8216;{print $4}&#8217;| sed &#8216;s\/:\/\/g&#8217;)<br \/>\necho $ip &gt;&gt; ip.txt<br \/>\nfi<br \/>\ndone<\/p>\n<p>19.\u6253\u5370\u5f53\u524d\u7528\u6237<\/p>\n<p>#\/bin\/bash<br \/>\necho &#8220;Current User is :&#8221;<br \/>\necho $(ps | grep &#8220;$$&#8221; | awk &#8216;{print $2}&#8217;)<\/p>\n<p>20.case\u8bed\u53e5\u7ec3\u4e60<\/p>\n<p>#!\/bin\/bash<br \/>\nclear<br \/>\necho &#8220;enter a number from 1 to 5:&#8221;<br \/>\nread num<br \/>\ncase $num in<br \/>\n1) echo &#8220;you enter 1&#8221;<br \/>\n;;<br \/>\n2) echo &#8220;you enter 2&#8221;<br \/>\n;;<br \/>\n3) echo &#8220;you enter 3&#8221;<br \/>\n;;<br \/>\n4) echo &#8220;you enter 4&#8221;<br \/>\n;;<br \/>\n5) echo &#8220;you enter 5&#8221;<br \/>\n;;<br \/>\n*) echo &#8220;error&#8221;<br \/>\n;;<br \/>\nesac<\/p>\n<p>21.yes\/no\u8fd4\u56de\u4e0d\u540c\u7684\u7ed3\u6784<\/p>\n<p>#!\/bin\/bash<br \/>\nclear<br \/>\necho &#8220;enter [y\/n]:&#8221;<br \/>\nread a<br \/>\ncase $a in<br \/>\ny|Y|Yes|YES) echo &#8220;you enter $a&#8221;<br \/>\n;;<br \/>\nn|N|NO|no) echo &#8220;you enter $a&#8221;<br \/>\n;;<br \/>\n*) echo &#8220;error&#8221;<br \/>\n;;<br \/>\nesac<\/p>\n<p>22.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>23.\u5185\u7f6e\u547d\u4ee4\u7684\u4f7f\u7528<\/p>\n<p>#\/bin\/bash<\/p>\n<p>clear<br \/>\necho &#8220;Hello, $USER&#8221;<br \/>\necho<\/p>\n<p>echo &#8220;Today &#8216;s date id `date`&#8221;<\/p>\n<p>echo<\/p>\n<p>echo &#8220;the user is :&#8221;<br \/>\nwho<br \/>\necho<\/p>\n<p>echo &#8220;this is `uname -s`&#8221;<br \/>\necho<\/p>\n<p>echo &#8220;that&#8217;s all folks! &#8221;<\/p>\n<p>24.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>25.<\/p>\n<p>#\/bin\/bash<\/p>\n<p>26.\u6253\u5370\u65e0\u5bc6\u7801\u7528\u6237<\/p>\n<p>#\/bin\/bash<br \/>\necho &#8220;No Password User are :&#8221;<br \/>\necho $(cat \/etc\/shadow | grep &#8220;!!&#8221; | awk &#8216;BEGIN { FS=&#8221;:&#8221; }{print $1}&#8217;)<\/p>\n<p>27.<\/p>\n<p>#\/bin\/bash<\/p>\n<p>clear<br \/>\necho &#8220;Hello, $USER&#8221;<br \/>\necho<\/p>\n<p>echo &#8220;Today &#8216;s date id `date`&#8221;<\/p>\n<p>echo<\/p>\n<p>echo &#8220;the user is :&#8221;<br \/>\nwho<br \/>\necho<\/p>\n<p>echo &#8220;this is `uname -s`&#8221;<br \/>\necho<\/p>\n<p>echo &#8220;that&#8217;s all folks! &#8221;<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u81ea\u5df1\u5199\u4e86\u4e00\u4e0b\u5c0f\u7684shell\u5b9e\u4f8b\uff0c\u867d\u7136\u5f88\u5c0f\uff0c\u4f46\u6240\u6709\u7684\u5927\u7684\u7a0b\u5e8f\u90fd\u662f\u7531\u5c0f\u7684\u6a21\u5757\u5806\u79ef\u8d77\u6765\u7684\uff0c\u7a0b\u5e8f\u5458\u4e00\u5b9a\u8981\u61c2\u5f97\u4e00\u79cd\u811a\u672c\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-178","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/178","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=178"}],"version-history":[{"count":1,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/178\/revisions"}],"predecessor-version":[{"id":479,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/178\/revisions\/479"}],"wp:attachment":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}