{"id":157874,"date":"2019-09-26T08:00:06","date_gmt":"2019-09-26T00:00:06","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=157874"},"modified":"2019-09-17T13:15:27","modified_gmt":"2019-09-17T05:15:27","slug":"linux-shell-script","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/linux-shell-script.html","title":{"rendered":"\u5206\u4eab\u5b9e\u7528\u76d1\u63a7\u811a\u672c:\u4f7f\u7528Shell\u68c0\u67e5\u8fdb\u7a0b\u662f\u5426\u5b58\u5728"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5728\u8fd0\u7ef4\u5de5\u4f5c\u4e2d\uff0c\u4e3a\u4fdd\u8bc1\u4e1a\u52a1\u7684\u6b63\u5e38\u8fd0\u884c\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u65f6\u67e5\u770b\u6307\u5b9a\u7684\u8fdb\u7a0b\u662f\u5426\u8fd8\u5b58\u5728\uff0c\u4e00\u65e6\u8fdb\u7a0b\u9000\u51fa\u4e86\uff0c\u8bf4\u660e\u670d\u52a1\u53ef\u80fd\u5b58\u5728\u95ee\u9898\uff0c\u8fd0\u7ef4\u5de5\u7a0b\u5e08\u5c31\u53ef\u4ee5\u5728\u7b2c\u4e00\u65f6\u95f4\u5904\u7406\u95ee\u9898\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u5728\u8fd0\u7ef4\u5de5\u4f5c\u4e2d\uff0c\u4e3a\u4fdd\u8bc1\u4e1a\u52a1\u7684\u6b63\u5e38\u8fd0\u884c\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u65f6\u67e5\u770b\u6307\u5b9a\u7684\u8fdb\u7a0b\u662f\u5426\u8fd8\u5b58\u5728\uff0c\u4e00\u65e6\u8fdb\u7a0b\u9000\u51fa\u4e86\uff0c\u8bf4\u660e\u670d\u52a1\u53ef\u80fd\u5b58\u5728\u95ee\u9898\uff0c\u8fd0\u7ef4\u5de5\u7a0b\u5e08\u5c31\u53ef\u4ee5\u5728\u7b2c\u4e00\u65f6\u95f4\u5904\u7406\u95ee\u9898\u3002\u4eca\u5929\u5c31\u8ddf\u5927\u5bb6\u5206\u4eab\u4e00\u4e2a\u5b9e\u7528\u7684shell\u811a\u672c\u2014\u2014\u65f6\u523b\u68c0\u67e5\u4f60\u7684\u8fdb\u7a0b\u72b6\u6001\u3002
\n\"\"<\/p>\n

\u5728\u5bf9\u8fdb\u7a0b\u8fdb\u884c\u76d1\u63a7\u65f6\uff0c\u6211\u4eec\u4e00\u822c\u9700\u8981\u5f97\u5230\u8be5\u8fdb\u7a0b\u7684 ID\uff0c\u8fdb\u7a0b ID \u662f\u8fdb\u7a0b\u7684\u552f\u4e00\u6807\u8bc6\uff0c\u4f46\u662f\u6709\u65f6\u53ef\u80fd\u5728\u670d\u52a1\u5668\u4e0a\u4e0d\u540c\u7528\u6237\u4e0b\u8fd0\u884c\u7740\u591a\u4e2a\u76f8\u540c\u8fdb\u7a0b\u540d\u7684\u8fdb\u7a0b\u3002<\/p>\n

\u4e0b\u9762\u7684\u51fd\u6570 GetPID \u7ed9\u51fa\u4e86\u83b7\u53d6\u6307\u5b9a\u7528\u6237\u4e0b\u6307\u5b9a\u8fdb\u7a0b\u540d\u7684\u8fdb\u7a0b ID \u529f\u80fd(\u76ee\u524d\u53ea\u8003\u8651\u8fd9\u4e2a\u7528\u6237\u4e0b\u542f\u52a8\u4e00\u4e2a\u6b64\u8fdb\u7a0b\u540d\u7684\u8fdb\u7a0b)\uff0c\u5b83\u6709\u4e24\u4e2a\u53c2\u6570\u4e3a\u7528\u6237\u540d\u548c\u8fdb\u7a0b\u540d\uff0c\u5b83\u9996\u5148\u4f7f\u7528 ps \u67e5\u627e\u8fdb\u7a0b\u4fe1\u606f\uff0c\u540c\u65f6\u901a\u8fc7 grep \u8fc7\u6ee4\u51fa\u9700\u8981\u7684\u8fdb\u7a0b\uff0c\u6700\u540e\u901a\u8fc7 sed \u548c awk \u67e5\u627e\u9700\u8981\u8fdb\u7a0b\u7684 ID \u503c(\u6b64\u51fd\u6570\u53ef\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u4fee\u6539\uff0c\u6bd4\u5982\u9700\u8981\u8fc7\u6ee4\u5176\u5b83\u4fe1\u606f\u7b49)\u3002<\/p>\n

\u5bf9\u8fdb\u7a0b\u8fdb\u884c\u76d1\u63a7<\/p>\n

function GetPID #User #Name  \r\n {  \r\n PsUser=$1  \r\n PsName=$2  \r\n pid=`ps -u $PsUser|grep $PsName|grep -v grep|grep -v vi|grep -v dbx\\n  \r\n |grep -v tail|grep -v start|grep -v stop |sed -n 1p |awk '{print $1}'`  \r\n echo $pid  \r\n } \r\n<\u3001pre>\r\n<\/pre>\n
\u793a\u4f8b\u6f14\u793a\uff1a<\/strong><\/div>\n
1. \u6e90\u7a0b\u5e8f(\u4f8b\u5982\u67e5\u627e\u7528\u6237\u4e3a root\uff0c\u8fdb\u7a0b\u540d\u4e3a TestApp \u7684\u8fdb\u7a0b ID)<\/strong><\/span><\/div>\n
PID=`GetPID root TestApp`    \r\n echo $PID \r\n<\/pre>\n
2. \u7ed3\u679c\u8f93\u51fa<\/strong><\/span><\/div>\n
11426  \r\n [dyu@xilinuxbldsrv shell]$ \r\n<\/pre>\n
3. \u7ed3\u679c\u5206\u6790<\/strong><\/span><\/div>\n

\u4ece\u4e0a\u9762\u7684\u8f93\u51fa\u53ef\u89c1\uff1a11426 \u4e3a root \u7528\u6237\u4e0b\u7684 TestApp \u7a0b\u5e8f\u7684\u8fdb\u7a0b ID\u3002<\/p>\n

4. \u547d\u4ee4\u8be6\u89e3<\/strong><\/span><\/div>\n

(1)ps: \u67e5\u770b\u7cfb\u7edf\u4e2d\u77ac\u95f4\u8fdb\u7a0b\u4fe1\u606f\u3002 \u53c2\u6570\uff1a<\/p>\n

-u< \u7528\u6237\u8bc6\u522b\u7801 > \u5217\u51fa\u5c5e\u4e8e\u8be5\u7528\u6237\u7684\u7a0b\u5e8f\u7684\u72b6\u51b5\uff0c\u4e5f\u53ef\u4f7f\u7528\u7528\u6237\u540d\u79f0\u6765\u6307\u5b9a
\n-p< \u8fdb\u7a0b\u8bc6\u522b\u7801 > \u6307\u5b9a\u8fdb\u7a0b\u8bc6\u522b\u7801\uff0c\u5e76\u5217\u51fa\u8be5\u8fdb\u7a0b\u7684\u72b6\u51b5
\n-o \u6307\u5b9a\u8f93\u51fa\u683c\u5f0f
\n(2)grep: \u7528\u4e8e\u67e5\u627e\u6587\u4ef6\u4e2d\u7b26\u5408\u5b57\u7b26\u4e32\u7684\u5f53\u524d\u884c\u3002 \u53c2\u6570\uff1a<\/p>\n

-v \u53cd\u5411\u9009\u62e9\uff0c\u4ea6\u5373\u663e\u793a\u51fa\u6ca1\u6709 \u2018\u641c\u5bfb\u5b57\u7b26\u4e32\u2019 \u5185\u5bb9\u7684\u90a3\u4e00\u884c
\n(3)sed: \u4e00\u4e2a\u975e\u4ea4\u4e92\u6027\u6587\u672c\u7f16\u8f91\u5668\uff0c\u5b83\u7f16\u8f91\u6587\u4ef6\u6216\u6807\u51c6\u8f93\u5165\u5bfc\u51fa\u7684\u6587\u4ef6\uff0c\u4e00\u6b21\u53ea\u80fd\u5904\u7406\u4e00\u884c\u5185\u5bb9\u3002 \u53c2\u6570\uff1a<\/p>\n

-n \u8bfb\u53d6\u4e0b\u4e00\u4e2a\u8f93\u5165\u884c\uff0c\u7528\u4e0b\u4e00\u4e2a\u547d\u4ee4\u5904\u7406\u65b0\u7684\u884c\u800c\u4e0d\u662f\u7528\u7b2c\u4e00\u4e2a\u547d\u4ee4
\np \u6807\u5fd7 \u6253\u5370\u5339\u914d\u884c
\n(4)awk\uff1a\u4e00\u79cd\u7f16\u7a0b\u8bed\u8a00\uff0c\u7528\u4e8e\u5728 linux\/unix \u4e0b\u5bf9\u6587\u672c\u548c\u6570\u636e\u8fdb\u884c\u5904\u7406\u3002\u9664\u4e86\u5728\u547d\u4ee4\u884c\u4e2d\u4f7f\u7528\uff0c\u5b83\u66f4\u591a\u662f\u4f5c\u4e3a\u811a\u672c\u6765\u4f7f\u7528\u3002 \u53c2\u6570\uff1a<\/p>\n

-F fs or \u2013field-separator fs \uff1a\u6307\u5b9a\u8f93\u5165\u6587\u4ef6\u6298\u5206\u9694\u7b26\uff0cfs \u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u6216\u8005\u662f\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5982 -F:\u3002
\nPS\uff1a\u6709\u65f6\u6709\u53ef\u80fd\u8fdb\u7a0b\u6ca1\u6709\u542f\u52a8\uff0c\u4e0b\u9762\u7684\u529f\u80fd\u662f\u68c0\u67e5\u8fdb\u7a0b ID \u662f\u5426\u5b58\u5728\uff0c\u5982\u679c\u6b64\u8fdb\u7a0b\u6ca1\u6709\u8fd0\u884c\u8f93\u51fa\uff1a<\/p>\n

The process does not exist.  \r\n # \u68c0\u67e5\u8fdb\u7a0b\u662f\u5426\u5b58\u5728 \r\n if [ \"-$PID\" == \"-\" ]  \r\n then  \r\n {  \r\n echo \"The process does not exist.\" \r\n }  \r\n fi \r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

\u5728\u8fd0\u7ef4\u5de5\u4f5c\u4e2d\uff0c\u4e3a\u4fdd\u8bc1\u4e1a\u52a1\u7684\u6b63\u5e38\u8fd0\u884c\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u65f6\u67e5\u770b\u6307\u5b9a\u7684\u8fdb\u7a0b\u662f\u5426\u8fd8\u5b58\u5728\uff0c\u4e00\u65e6\u8fdb\u7a0b\u9000\u51fa\u4e86\uff0c\u8bf4\u660e\u670d\u52a1\u53ef\u80fd\u5b58\u5728\u95ee\u9898 […]<\/p>\n","protected":false},"author":1898,"featured_media":157876,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[212],"class_list":["post-157874","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread","tag-shell"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/157874","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\/1898"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=157874"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/157874\/revisions"}],"predecessor-version":[{"id":157962,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/157874\/revisions\/157962"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/157876"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=157874"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=157874"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=157874"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}