{"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":"
\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 \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 |