{"id":1040,"date":"2014-08-17T16:29:41","date_gmt":"2014-08-17T16:29:41","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=1040"},"modified":"2014-08-17T16:29:41","modified_gmt":"2014-08-17T16:29:41","slug":"%e6%8a%93%e9%98%84%e7%9a%84%e6%b8%b8%e6%88%8f%e8%84%9a%e6%9c%ac","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/1040.html","title":{"rendered":"\u6293\u9604\u7684\u6e38\u620f\u811a\u672c"},"content":{"rendered":"<p>\u4e4b\u524d\u770boldboy\u7684\u535a\u5ba2\u65f6\u770b\u5230\u7684\u4e00\u9053\u9898\u76ee\uff0c\u867d\u7136\u5f53\u65f6\u6211\u6ca1\u6709\u505a\uff0c\u4f46\u662f\u6536\u96c6\u4e86\u5728\u6587\u7ae0\u8bc4\u8bba\u4e2d\u6bd4\u8f83\u597d\u7684shell\/Python\u811a\u672c\uff0c\u7136\u540e\u81ea\u5df1\u7ecf\u8fc7\u4fee\u6539\u3001\u8c03\u8bd5\uff0c\u6574\u7406\u51fa\u4e86\u4e0b\u9762\u76846\u4e2a\u811a\u672c\uff0c\u6bd4\u8f83\u597d\u73a9\uff0c\u5c31\u5206\u4eab\u51fa\u6765\u4e86\uff1a<\/p>\n<p>\u9700\u8981\u4e00\u4e2a\u6293\u9604\u7684\u7a0b\u5e8f\uff0c\u8981\u6c42\uff1a<br \/>\n1\u3001\u6267\u884c\u811a\u672c\u540e\uff0c<span style=\"color: #ff0000;\">\u8f93\u5165\u7528\u6237\u540d\uff0c\u968f\u673a\u4ea7\u751f01-99\u4e4b\u95f4\u7684\u6570\u5b57\uff0c\u524d\u9762\u5df2\u7ecf\u6293\u5230\u7684\u6570\u5b57\uff0c\u4e0b\u6b21\u4e0d\u80fd\u518d\u51fa\u73b0<\/span>\uff1b<br \/>\n2\u3001\u7b2c\u4e00\u4e2a\u8f93\u5165\u7528\u6237\u540d\u540e\uff0c\u5c4f\u5e55\u8f93\u51fa\u4fe1\u606f\uff0c\u5e76<span style=\"color: #ff0000;\">\u5c06\u540d\u5b57\u548c\u6570\u5b57\u8bb0\u5f55\u5230\u6587\u4ef6\u91cc\uff0c\u7a0b\u5e8f\u4e0d\u80fd\u9000\u51fa\uff0c\u7ee7\u7eed\u7b49\u5f85\u522b\u7684\u5b66\u751f\u8f93\u5165<\/span>\u3002<\/p>\n<hr \/>\n<h5>\u53c2\u8003\u7b54\u6848\uff1a<\/h5>\n<h6>1.\u8003\u8651\u5f97\u76f8\u5f53\u7ec6\u81f4\u7684shell\u811a\u672c<\/h6>\n<pre class=\"lang:default decode:true\">#!\/bin\/bash\n\nread -p \"Please enter your name:\" -t 10 name\nif [ $? -ne 0 ]\nthen\n\techo -e \"n!!!!!!Enter is timeout!!!!!!\"\n\texit 1\nfi\n\ntouch temp result\ncat \/dev\/null &gt; result\t#\u5b58\u653e\u6700\u540e\u7684\u7ed3\u679c\uff0c\u683c\u5f0f\u4e3a\uff1a$name is $number\ncat \/dev\/null &gt; temp\t#\u5b58\u653e\u4e34\u65f6\u7684\u7ed3\u679c\uff0c\u6bcf\u884c\u5185\u5bb9\u5747\u4e3a\uff1a$number\n\nwhile true\ndo\n\t#if [ $(grep ^$name result | wc -l) -ge 1 ]\t#\u8fd9\u91cc\u7684grep\u5224\u65ad\u6709\u8bef\uff0c\u6bd4\u5982\uff1a\u8f93\u5165\u4e86haha\u4e4b\u540e\u5c31\u65e0\u6cd5\u8f93\u5165\u7528\u6237\u540dh\n\tif [ $(grep ^b$nameb result | wc -l) -ge 1 ]\t#\u6dfb\u52a0b\u951a\u5b9a\u5355\u8bcd\n\tthen\n\t\techo \"The name already exists.\"\n\tfi\n\tif [ \"$name\" == \"exit\" ]\n\tthen\n\t\trm temp -f\n\t\techo \"******************************\"\n\t\techo \"The result of the top three:\"\n\t\tsort -k3rn result | head -3\t\t# sort\u7684\u5404\u79cd\u9009\u9879\u9700\u8981\u597d\u597d\u5b66\u4e60\n\t\techo \"******************************\"\n\t\texit 1\n\tfi\n\tif [ -z $name ]\t\t# \u8fd9\u4e2a\u5224\u65ad\u6d41\u7a0b\u7684\u9009\u62e9\u53ef\u4ee5\u597d\u597d\u601d\u8003\u601d\u8003\uff01\n\t\tthen\n\t\techo \"Your name is null.\"\n\t\tread -p \"Please enter your name:\"\n\tfi\n\tnumber=$(($RANDOM%99+1))\t#\u9700\u8981\u6ce8\u610f\u7a7a\u683c\u7684\u4f7f\u7528\n\tif [ $(grep \"^$number$\" temp | wc -l) -eq 0 ] &amp;&amp; [ $(grep ^$name result|wc -l) -eq 0 ];then\n\t\techo \"$name is $number\" &gt;&gt; result\n\t\techo \"$name is $number\"\n\t\techo $number &gt;&gt; temp\n\t\tif [ $(cat temp|wc -l) -ge 99 ];then\t#\u8003\u8651\u7684\u771f\u662f\u591f\u7ec6\u81f4\u7684\uff01\n\t\t\techo -e \"The number out of range.n exit!!!\"\n\t\t\trm temp -f\n\t\t\techo \"******************************\"\n\t\t\techo \"The result of the top three:\"\n\t\t\tsort -k3rn result|head -3\n\t\t\techo \"******************************\"\n\t\t\texit 1\n\t\tfi\n\tfi\n\techo \"Enter 'exit' end the process!\"\n\tread -p \"Please enter your name:\" -t 10 name\n\tif [ $? -ne 0 ]\n\tthen\n\t\techo -e \"n!!!!!!Enter is timeout!!!!!!!\"\n\t\techo \"******************************\"\n\t\techo \"The result of the top three:\"\n\t\tsort -k3rn result|head -3\n\t\techo \"******************************\"\n\t\texit 1\n\tfi\n\tsleep 0.1\ndone<\/pre>\n<h6>2.\u6bd4\u8f83\u7b80\u5355\u7684Python\u811a\u672c\uff0c\u6709\u5f88\u591a\u95ee\u9898\u6ca1\u6709\u8003\u8651<\/h6>\n<pre class=\"lang:default decode:true\">#!\/usr\/bin\/env python\n#coding=utf-8\nimport random\nrunning = True\nwhile running:\n\tname = raw_input('name:')\n\tif not name:\n\t\tbreak\n\tprint name\n\ta = range(1,99)\n\tslice = random.sample(a, 1)  #Python\u7684random.sample()\u4ece\u6307\u5b9a\u5e8f\u5217\u4e2d\u968f\u673a\u83b7\u53d6\u6307\u5b9a\u957f\u5ea6{\u7b2c\u4e8c\u4e2a\u53c2\u6570}\u7684\u7247\u6bb5\n\tfor i in slice:\t\t#\u8fd9\u91cc\u6ca1\u6709\u8fdb\u884c\u91cd\u590d\u4e0e\u5426\u7684\u5224\u65ad\n\t\tnum = str(i)\n\t\tprint num\n\tf = open('\/tmp\/user', 'a')\n\tf.write(name + \":::\" + num + \"n\")\n\tf.close<\/pre>\n<h6>3.\u6bd4\u8f83\u7b80\u5355\u7684Python\u811a\u672c(\u53ea\u7ba1\u5f55\u5165\u4e0d\u7ba1\u522b\u7684)<\/h6>\n<pre class=\"lang:default decode:true\">#!\/usr\/bin\/env python\n#coding=utf-8\nimport random\nimport ConfigParser\n\nscores = {}\n\ndef saveScore():\n\tconfig = ConfigParser.RawConfigParser()\n\t#config.read('score.cfg')\n\tconfig.add_section('score')\n\tfor k, v in scores.items():\n\t\tconfig.set('score', k, v)\t#\u4f7f\u7528set\u8bbe\u7f6ek,v\u503c\n\twith open('score.cfg', 'wb') as configfile:\n\t\tconfig.write(configfile)\n\tconfig = None\n\nusername = raw_input('\u8bf7\u8f93\u5165\u59d3\u540d:')\nwhile username:\n\tif username and not scores.has_key(username):\n\t\tscore = random.randint(1, 99)\n\t\tscores[username] = score\n\t\tprint('\u540d\u5b57:%s, \u5f97\u5206:%s' % (username, score))\n\t\tsaveScore()\n\telse:\n\t\tprint('\u8bf7\u8f93\u5165\u6b63\u786e\u7684\u540d\u5b57, \u6216\u8005\u540d\u5b57\u5df2\u7ecf\u5b58\u5728 n')\n\tusername = raw_input('\u8bf7\u8f93\u5165\u59d3\u540d:')<\/pre>\n<h6>4.\u6709\u4fdd\u5b58\u529f\u80fd\u7684Python\u811a\u672c<\/h6>\n<pre class=\"lang:default decode:true\">#!\/usr\/bin\/env python\n# -*- coding: utf-8 -*-\n\nimport random, pickle, os, sys\n\ndef load():\n\tf = open('list.txt', 'r')\n\tglobal list\n\tlist = pickle.load(f)\n\treturn list\n\tf.close()\ndef save():\n\tf = open('list.txt', 'w')\n\tpickle.dump(list, f)\n\tf.close()\ndef leave(x):\n\tprint \"\"\n\tprint \"%sThe TOP Three is:\" % x\n\tprint 'ID   NAME'\n\tprint '-----------------------'\n\tfor i in list2[-4:-1]:\t#\u53d6\u5217\u8868\u4e2d\u6700\u540e\u76843\u4e2a\u5143\u7d20{\u524d\u9762\u4f7f\u7528\u4e86sort\u51fd\u6570\u5bf9\u5217\u8868\u8fdb\u884c\u4e86\u6392\u5e8f}\n\t\tprint '%s   %s'%(i, list1[i])\n\tprint '-----------------------'\n\nif not os.path.exists('list.txt'):\n\tf = open('list.txt', 'w')\n\taa = {'id':'name'}\n\tpickle.dump(aa, f)\n\tf.close()\ntry:\n\twhile True:\n\t\tdigint = random.randrange(01, 100)\n\t\tlist1 = load()\n\t\tlist2 = list1.keys()\n\t\tlist2.sort()\n\t\tinpu = raw_input(\"Please input you English name:\").strip()\n\t\tif (list2.index(list2[-1]) == 100) or (inpu == 'exit'):\n\t\t\tleave(\"Now come to the end, \")\n\t\t\tsys.exit()\n\t\tif len(inpu) == 0:\n\t\t\tprint \"don't empty, please input again\"\n\t\t\tcontinue\n\t\tlist1 = load()\n\t\tif inpu in list1.values():\n\t\t\tprint \"system have it's name, please change\"\n\t\t\tcontinue\n\t\telse:\n\t\t\twhile digint in list1:\n\t\t\t\tdigint = random.randrange(01, 100)\n\t\t\t\tcontinue\n\t\t\telse:\n\t\t\t\tlist1[digint] = inpu\n\t\t\t\tprint \"%s's id is %s\"%(inpu, digint)\n\t\t\t\tsave()\nexcept KeyboardInterrupt:\n\tlist1 = load()\n\tlist2 = list1.keys()\n\tlist2.sort()\n\tleave(\"\")<\/pre>\n<h6>5.\u867d\u7136\u7b80\u5355\u4f46\u529f\u80fd\u633a\u5168\u7684shell\u811a\u672c<\/h6>\n<pre class=\"lang:default decode:true\">#!\/bin\/bash\n\n[ ! -f file ] &amp;&amp; touch file\nwhile true;\ndo\n\tread -p '\u8f93\u5165:' you\n\tif [ \"$you\" == 'exit' ];then\n\t\tbreak\n\tfi\n\tif [ -n \"$you\" ] &amp;&amp; ! egrep \"^$you.*\" file;then\n\t\twhile true;\n\t\tdo\n\t\t\trannum=$(expr $RANDOM % 99 + 1)\n\t\t\tif ! egrep \"$you number is $rannum\" file ;then\n\t\t\t\techo \"$you number is $rannum\" | tee -a file\n\t\t\t\tbreak\n\t\t\tfi\n\t\tdone\n\tfi\ndone<\/pre>\n<h6>6.\u529f\u80fd\u975e\u5e38\u5168\u7684shell\u811a\u672c<\/h6>\n<pre class=\"lang:default decode:true \">#!\/bin\/bash\n\n#\u6574\u4e2a\u6d41\u7a0b\u5c31\u662f\uff1a\n#\u5224\u65ad\u8f93\u5165\u5185\u5bb9\uff0c\u5206\u4e3a\uff1aNull|quit|sort|clear|\u53c2\u4e0e\u8005\u540d\u5b57\n#\u5f53\u8f93\u5165\u6b63\u5e38\u65f6\uff0c\u518d\u5224\u65ad\u6587\u4ef6\u5927\u5c0f\u662f\u5426\u4e3a0\uff0c\u82e5\u4e3a0\u5219\u76f4\u63a5\u8f93\u5165\uff1b\u5426\u5219\u6839\u636e\u5df2\u6709\u5185\u5bb9\u5224\u65ad\u7528\u6237\u540d\u662f\u5426\u5df2\u5b58\u5728\necho \"   ##################################################################\"\necho \"   #  Interesting companies shell practical programming problem\t#\"\necho \"   ##################################################################\"\necho \"   #   You Can Use :  'quit' to exit shell scripts\t\t\t\t#\"\necho \"   ##################################################################\"\necho \"   #   'sort' to sort the order in result file\t\t\t\t\t#\"\necho \"   ##################################################################\"\ntxt=temp.txt\ntouch \"$txt\"\nwhile(true);do\nread -p \"   #Please input your name : \" name\nif [ \"$name\" == \"\" ];then\n\techo \"   #WARNING: Username should not be empty! \"\nelif [ \"$name\" == \"quit\" ];then\n\texit 0\nelif [ \"$name\" == \"sort\" ];then\n\tcat temp.txt | sort -n\t#\u8fd9\u4e2a\u8c8c\u4f3c\u6ca1\u6709\u8d77\u5230\u6392\u5e8f\u7684\u4f5c\u7528?\nelif [ \"$name\" == \"cl\" ] || [ \"$name\" == \"clear\" ] ;then\n\tclear\nelse\n\tif [ -s \"$txt\" ];then\t#\u6587\u4ef6\u5927\u5c0f\u975e0\u65f6\u4e3a\u771f\n\t\ta=`grep -w \"$name\" \"$txt\" | wc -l`\n\t\tif [ \"$a\" -gt 0 ];then\t#\u5f53\u5df2\u5b58\u5728\u65f6\uff0c\u5219\u4f1a\u7ed9\u51fa\u63d0\u793a-\u663e\u793a\u5df2\u6709\u5185\u5bb9\u5e76\u9000\u51fa\u5f53\u524d\u5faa\u73af\n\t\t\techo \"   #WARNING: name as $name is existing\"\n\t\t\techo -n \"   \";grep -w \"$name\" \"$txt\"\n\t\t\tcontinue\n\t\telse\n\t\t\tdeclare -i num=$RANDOM%99+1\t\t#-i\u9009\u9879\u4f1a\u628a\"num\"\u5f53\u4f5c\u6574\u6570\u770b\u5f85\n\t\t\tb=`grep -w \"$num\" \"$txt\" |wc -l`\n\t\t\tif [ \"$b\" -gt 0 ];then\n\t\t\t\tcontinue\n\t\t\telse\n\t\t\t\techo -e \"$numt$name\"&gt;&gt;$txt\t#\u5b58\u5165\u6587\u4ef6\u7684\u683c\u5f0f\n\t\t\t\techo -n \"   \";grep -w \"$name\" \"$txt\"\n\t\t\tfi\n\t\tfi\n\telse\t#\u5f53\u6587\u4ef6\u5927\u5c0f\u4e3a0\u65f6\uff0c\u8868\u793a\u662f\u7b2c1\u6b21\u8fdb\u884c\uff0c\u76f4\u63a5\u5199\u5165\u7ed3\u679c\u5373\u53ef\n\t\tdeclare -i num=$RANDOM%99+1\n\t\techo -e \"$numt$name\" &gt;&gt;$txt\n\t\techo -n \"   \";grep -w \"$name\" \"$txt\"\n\tfi\nfi\ndone<\/pre>\n<p>&nbsp;<\/p>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<p>http:\/\/oldboy.blog.51cto.com\/2561410\/1308647<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e4b\u524d\u770boldboy\u7684\u535a\u5ba2\u65f6\u770b\u5230\u7684\u4e00\u9053\u9898\u76ee\uff0c\u867d\u7136\u5f53\u65f6\u6211\u6ca1\u6709\u505a\uff0c\u4f46\u662f\u6536\u96c6\u4e86\u5728\u6587\u7ae0\u8bc4\u8bba\u4e2d\u6bd4\u8f83\u597d\u7684shell\/Pyth [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,6,7],"tags":[318],"class_list":["post-1040","post","type-post","status-publish","format-standard","hentry","category-linux","category-other","category-programing","tag-lottery"],"views":2769,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1040","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=1040"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1040\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=1040"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=1040"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=1040"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}