{"id":207696,"date":"2020-12-28T10:47:21","date_gmt":"2020-12-28T02:47:21","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=207696"},"modified":"2020-12-22T10:48:57","modified_gmt":"2020-12-22T02:48:57","slug":"shell-dev-null","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/shell-dev-null.html","title":{"rendered":"Shell\u811a\u672c\u4e2d\u7684 \/Dev\/Null \u7528\u9014"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\/dev\/null \u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u8bbe\u5907\u6587\u4ef6\uff0c\u5b83\u4e22\u5f03\u4e00\u5207\u5199\u5165\u5176\u4e2d\u7684\u6570\u636e \u53ef\u4ee5\u5c06\u5b83 \u89c6\u4e3a\u4e00\u4e2a\u9ed1\u6d1e, \u5b83\u7b49\u6548\u4e8e\u53ea\u5199\u6587\u4ef6, \u5199\u5165\u5176\u4e2d\u7684\u6240\u6709\u5185\u5bb9\u90fd\u4f1a\u6d88\u5931, \u5c1d\u8bd5\u4ece\u4e2d\u8bfb\u53d6\u6216\u8f93\u51fa\u4e0d\u4f1a\u6709\u4efb\u4f55\u7ed3\u679c\uff0c\u540c\u6837\uff0c\/dev\/null \u5728\u547d\u4ee4\u884c\u548c\u811a\u672c\u4e2d\u90fd\u975e\u5e38\u6709\u7528\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\/dev\/null \u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u8bbe\u5907\u6587\u4ef6\uff0c\u5b83\u4e22\u5f03\u4e00\u5207\u5199\u5165\u5176\u4e2d\u7684\u6570\u636e \u53ef\u4ee5\u5c06\u5b83 \u89c6\u4e3a\u4e00\u4e2a\u9ed1\u6d1e, \u5b83\u7b49\u6548\u4e8e\u53ea\u5199\u6587\u4ef6, \u5199\u5165\u5176\u4e2d\u7684\u6240\u6709\u5185\u5bb9\u90fd\u4f1a\u6d88\u5931, \u5c1d\u8bd5\u4ece\u4e2d\u8bfb\u53d6\u6216\u8f93\u51fa\u4e0d\u4f1a\u6709\u4efb\u4f55\u7ed3\u679c\uff0c\u540c\u6837\uff0c\/dev\/null \u5728\u547d\u4ee4\u884c\u548c\u811a\u672c\u4e2d\u90fd\u975e\u5e38\u6709\u7528<\/p>\n

\u7528\u9014<\/strong><\/div>\n

\/dev\/null \u901a\u5e38\u88ab\u7528\u4e8e\u4e22\u5f03\u4e0d\u9700\u8981\u7684\u8f93\u51fa\u6d41\uff0c\u6216\u4f5c\u4e3a\u7528\u4e8e\u8f93\u5165\u6d41\u7684\u7a7a\u6587\u4ef6\uff0c\u8fd9\u4e9b\u64cd\u4f5c\u901a\u5e38\u7531\u91cd\u5b9a\u5411\u5b8c\u6210\uff0c\u4efb\u4f55\u4f60\u60f3\u4e22\u5f03\u7684\u6570\u636e\u90fd\u53ef\u4ee5\u5199\u5165\u5176\u4e2d<\/p>\n

\u4e22\u5f03\u6807\u51c6\u8f93\u51fa<\/p>\n

\u5728\u5199shell\u811a\u672c\u7684\u65f6\u5019\uff0c\u53ea\u60f3\u901a\u8fc7\u547d\u4ee4\u7684\u7ed3\u679c\u6267\u884c\u540e\u9762\u7684\u903b\u8f91\uff0c\u800c\u4e0d\u60f3\u547d\u4ee4\u6267\u884c\u8fc7\u7a0b\u4e2d\u6709\u4e00\u5927\u5806\u4e2d\u95f4\u7ed3\u679c\u8f93\u51fa\uff0c\u8fd9\u65f6\u5019\u53ef\u4ee5\u628a\u547d\u4ee4\u6267\u884c\u8fc7\u7a0b\u4e2d\u7684\u8f93\u5165\u5168\u90e8\u5199\u5165 \/dev\/null<\/p>\n

\u73b0\u6709 a.sh \u811a\u672c\uff0c\u5b83\u7684\u529f\u80fd\u662f\u5224\u65ad\u4f20\u5165\u7684\u7cfb\u7edf\u547d\u4ee4\u662f\u5426\u5b58\u5728\uff0c\u811a\u672c\u5185\u5bb9\u5982\u4e0b<\/p>\n

\r\n#!\/bin\/bash \r\n    \r\n   command -v $1 \r\n    \r\n   if [[ $? -eq 0 ]]; then \r\n      echo \"command $1 exist...\" \r\n   else \r\n      echo \"command $1 not exist...\" \r\n   fi \r\n<\/pre>\n

\u6267\u884c .\/a.sh top \u547d\u4ee4\uff0c\u8f93\u51fa\u5982\u4e0b<\/p>\n

\r\n[tt@ecs-centos-7 dev_test]$ .\/a.sh top \r\n\/bin\/top \r\ncommand top exist... \r\n<\/pre>\n

\u8bf4\u660e: command -v \u547d\u4ee4\u540d \u662f\u67e5\u627e\u6307\u5b9a\u547d\u4ee4\u540d\u7684\u547d\u4ee4\u662f\u5426\u5b58\u5728\uff0c\u5982\u679c\u5b58\u5728\uff0c\u8f93\u51fa\u6307\u5b9a\u547d\u4ee4\u540d\u7684\u8def\u5f84\uff0c\u5426\u5219\uff0c\u4e0d\u505a\u4efb\u4f55\u8f93\u51fa<\/p>\n

$? \u8868\u793a\u524d\u4e00\u6761\u547d\u4ee4\u7684\u6267\u884c\u7ed3\u679c, 0 \u8868\u793a\u6210\u529f\uff0c\u5176\u4ed6\u8868\u793a\u5931\u8d25<\/p>\n

\u811a\u672c\u7684\u6267\u884c\u7ed3\u679c\u4e2d\u5148\u8f93\u51fa\u4e86 top \u547d\u4ee4\u7684\u8def\u5f84\uff0c\u7d27\u63a5\u7740\u8f93\u51fa\u4e86top\u547d\u4ee4\u5b58\u5728\u7684\u65e5\u5fd7<\/p>\n

\u628a command -v $1\u7684\u7ed3\u679c\u91cd\u5b9a\u5411\u5230 \/dev\/null \u53ef\u4ee5\u5c4f\u853d\u6389 top\u547d\u4ee4\u8def\u5f84\u7684\u8f93\u51fa\uff0c\u8c03\u6574\u4e4b\u540e\u7684a.sh \u5185\u5bb9\u5982\u4e0b<\/p>\n

\r\n#!\/bin\/bash \r\n  \r\n command -v $1 >\/dev\/null \r\n  \r\n if [[ $? -eq 0 ]]; then \r\n    echo \"command $1 exist...\" \r\n else \r\n    echo \"command $1 not exist...\" \r\n fi \r\n<\/pre>\n

\u518d\u6b21\u6267\u884c .\/a.sh top\uff0c\u7ed3\u679c\u5982\u4e0b<\/p>\n

\r\n[tt@ecs-centos-7 dev_test]$ .\/a.sh top \r\ncommand top exist... \r\n[tt@ecs-centos-7 dev_test]$ \r\n<\/pre>\n

\u4ece\u6267\u884c\u7ed3\u679c\u53ef\u4ee5\u770b\u51fa\uff0c\u628a command -v $1 \u7684\u4e2d\u95f4\u7ed3\u679c\u91cd\u5b9a\u5411\u5230 \/dev\/null \u4e4b\u540e\uff0c\u5c31\u4e0d\u4f1a\u518d\u8f93\u51fatop\u547d\u4ee4\u7684\u8def\u5f84\u4e86<\/p>\n

\u800c\u4e14\uff0c\u4fee\u6539\u4e4b\u540e\u7684\u811a\u672c\u6267\u884c\u7ed3\u679c\u548c\u539f\u6765\u7684\u7ed3\u679c\u662f\u4e00\u6837\u7684<\/p>\n

\u4e22\u5f03\u6807\u51c6\u9519\u8bef\u8f93\u51fa<\/p>\n

\u5728shell\u811a\u672c\u4e2d\uff0c\u5220\u9664\u4e00\u4e2a\u6587\u4ef6\u7684\u65f6\u5019\uff0c\u9700\u8981\u5148\u5224\u65ad\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff0c\u7136\u540e\u624d\u80fd\u6267\u884c\u5220\u9664\u64cd\u4f5c\uff0c\u5426\u5219\u5220\u9664\u7684\u65f6\u5019\u4f1a\u8f93\u51fa\u9519\u8bef, \u4e00\u822c\u7684\u5220\u9664\u6587\u4ef6\u811a\u672c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n

\r\n#!\/bin\/bash \r\n    \r\n   if [ -f $1 ]; then \r\n      rm $1 \r\n   fi \r\n<\/pre>\n

\u53ef\u4ee5\u901a\u8fc7\u628a\u5220\u9664\u547d\u4ee4\u7684\u8f93\u51fa\u91cd\u5b9a\u5411\u5230 \/dev\/null \u6765\u907f\u514d\u8f93\u51fa\u9519\u8bef\u4fe1\u606f, \u540c\u65f6\u4e5f\u4e0d\u7528\u5224\u65ad\u6587\u4ef6\u662f\u5426\u5b58\u5728\u4e86, \u8c03\u6574\u4e4b\u540e\u7684\u5220\u9664\u811a\u672c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n

\r\n#!\/bin\/bash \r\n \r\nrm $1 >\/dev\/null 2>$1 \r\n<\/pre>\n

\u5206\u522b\u6267\u884c\u547d\u4ee4 .\/d.sh t1.txt\u3001.\/d.sh t2.txt \uff0c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n

\r\n[tt@ecs-centos-7 dev_test]$ ls t*.txt \r\nt1.txt \r\n[tt@ecs-centos-7 dev_test]$ .\/d.sh t1.txt \r\n[tt@ecs-centos-7 dev_test]$ .\/d.sh t2.txt \r\n[tt@ecs-centos-7 dev_test]$ ls t*.txt \r\nls: \u65e0\u6cd5\u8bbf\u95eet*.txt: \u6ca1\u6709\u90a3\u4e2a\u6587\u4ef6\u6216\u76ee\u5f55 \r\n<\/pre>\n

ls: \u65e0\u6cd5\u8bbf\u95eet*.txt: \u6ca1\u6709\u90a3\u4e2a\u6587\u4ef6\u6216\u76ee\u5f55<\/p>\n

t1.txt \u6587\u4ef6\u4f4d\u4e8e\u5f53\u524d\u76ee\u5f55\u4e0b\uff0ct2.txt \u4e0d\u5b58\u5728\uff0c\u4ece\u6267\u884c\u7ed3\u679c\u53ef\u4ee5\u770b\u51fa\uff0c\u4e0d\u7ba1\u662f\u5220\u9664\u5b58\u5728\u7684\u6587\u4ef6\u8fd8\u662f\u4e0d\u5b58\u5728\u7684\u6587\u4ef6\u90fd\u4e0d\u4f1a\u6709\u9519\u8bef\u8f93\u51fa\u4fe1\u606f\u4e86<\/p>\n

\u6e05\u7a7a\u6587\u4ef6\u5185\u5bb9<\/strong><\/div>\n

\u6e05\u7a7a\u6587\u4ef6\u5185\u5bb9\u6709\u5f88\u591a\u79cd\u65b9\u6cd5\uff0c\u8fd9\u91cc\u4ecb\u7ecd\u4e00\u79cd\u5229\u7528 \/dev\/null \u6e05\u7a7a\u6587\u4ef6\u5185\u5bb9\u7684\u65b9\u6cd5\uff0c\u5177\u4f53\u7684\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n

\r\n[tt@ecs-centos-7 dev_test]$ cat t.txt  \r\n123456 \r\n[tt@ecs-centos-7 dev_test]$ cat \/dev\/null > t.txt  \r\n[tt@ecs-centos-7 dev_test]$ cat t.txt \r\n<\/pre>\n
\u65e5\u5fd7\u7684\u5904\u7406<\/strong><\/div>\n

\u5728\u811a\u672c\u4e2d\uff0c\u4e3a\u4e86\u65b9\u4fbf\u8c03\u8bd5\uff0c\u7ecf\u5e38\u4f1a\u52a0\u4e00\u4e9b\u65e5\u5fd7\u6253\u5370\u7684\u903b\u8f91\uff0c\u6709\u65f6\u8fd9\u79cd\u8c03\u8bd5\u65e5\u5fd7\u8fd8\u6bd4\u8f83\u591a\uff0c\u811a\u672c\u6d4b\u8bd5\u901a\u8fc7\u4e4b\u540e\uff0c\u8fd9\u4e9b\u8c03\u8bd5\u65e5\u5fd7\u53ef\u80fd\u5c31\u5220\u9664\u6216\u8005\u6ce8\u91ca\u6389\u4e86<\/p>\n

\u8fd9\u91cc\u63d0\u4f9b\u4e00\u4e2a\u5c0f\u6280\u5de7\uff0c\u65e2\u4e0d\u7528\u5220\u9664\u4e5f\u4e0d\u7528\u6ce8\u91ca\u6389\u65e5\u5fd7\uff0c\u540c\u65f6\u6267\u884c\u811a\u672c\u7684\u65f6\u5019\u8fd8\u4e0d\u4f1a\u8f93\u51fa\u8fd9\u4e9b\u8c03\u8bd5\u65e5\u5fd7<\/p>\n

\u6bd4\u5982: \u5f53\u524d\u76ee\u5f55\u6709\u4e00\u4e2a\u65e5\u5fd7\u6587\u4ef6 log.txt\uff0c\u811a\u672c\u7684\u8c03\u8bd5\u65e5\u5fd7\u4f1a\u4ee5 echo \" this is debug log\" >> log.txt \u7684\u5f62\u5f0f\u5199\u5165\u8fd9\u4e2a\u6587\u4ef6\u4e2d<\/p>\n

\u73b0\u5728\u811a\u672c\u529f\u80fd\u6d4b\u8bd5\u901a\u8fc7\u4e86\uff0c\u8c03\u8bd5\u65e5\u5fd7\u4e0d\u9700\u8981\u5199\u5165log.txt\u4e86<\/p>\n

\u53ef\u4ee5\u8fd9\u4e48\u505a\uff1a\u539f\u6765\u7684\u811a\u672c\u539f\u6837\u4e0d\u52a8\uff0c\u672c\u5730\u5148\u5220\u9664 log.txt\uff0c\u7136\u540e\u6267\u884c ln -s \/dev\/null .\/log.txt \u547d\u4ee4\uff0c\u8be5\u547d\u4ee4\u5efa\u7acb\u4e86\u4e00\u4e2a log.txt \u5230 \/dev\/nulll\u7684\u8f6f\u8fde\u63a5\uff0c\u4ee5\u540e\u90fd\u6709\u5199\u5165 log.txt \u7684\u5185\u5bb9\u5b9e\u9645\u90fd\u4f1a\u5199\u5165 \/dev\/null \uff0c\u800c\u5199\u5165 \/dev\/null \u7684\u6240\u6709\u5185\u5bb9\u90fd\u4f1a\u88ab\u4e22\u5f03\u6389<\/p>\n","protected":false},"excerpt":{"rendered":"

\/dev\/null \u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u8bbe\u5907\u6587\u4ef6\uff0c\u5b83\u4e22\u5f03\u4e00\u5207\u5199\u5165\u5176\u4e2d\u7684\u6570\u636e \u53ef\u4ee5\u5c06\u5b83 \u89c6\u4e3a\u4e00\u4e2a\u9ed1\u6d1e, \u5b83\u7b49\u6548\u4e8e\u53ea\u5199\u6587\u4ef6 […]<\/p>\n","protected":false},"author":370,"featured_media":41354,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-207696","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\/207696","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\/370"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=207696"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/207696\/revisions"}],"predecessor-version":[{"id":207857,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/207696\/revisions\/207857"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/41354"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=207696"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=207696"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=207696"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}