{"id":2160,"date":"2015-05-16T23:53:16","date_gmt":"2015-05-16T15:53:16","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=2160"},"modified":"2017-06-30T17:04:37","modified_gmt":"2017-06-30T09:04:37","slug":"%e5%86%99bash%e8%84%9a%e6%9c%ac%e7%9a%84%e4%b8%80%e4%ba%9b%e8%a7%84%e8%8c%83%e5%b0%8f%e7%bb%93","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/2160.html","title":{"rendered":"\u5199Bash\u811a\u672c\u7684\u4e00\u4e9b\u89c4\u8303\u5c0f\u7ed3"},"content":{"rendered":"<p>\u63a5\u89e6Linux\u5df2\u7ecf4\u30015\u5e74\u4e86\uff0c\u4e5f\u5199\u4e86\u4e0d\u5c11\u7684shell\u811a\u672c\uff08\u867d\u7136\u8bf4\u90fd\u5f88\u7b80\u5355\uff09\uff0c\u4e0d\u8fc7\u4e00\u76f4\u90fd\u6ca1\u6709\u5199\u8fc7\u6bd4\u8f83\u5927\u7684\u9879\u76ee\uff0c\u5e73\u65f6\u867d\u7136\u4f1a\u6536\u96c6\u4e00\u4e9b\u597d\u7684\u811a\u672c\uff0c\u4f46\u4e5f\u53ea\u662f\u5c0f\u77e5\u8bc6\u70b9\u4e0a\u7684\u603b\u7ed3\uff0c\u7f3a\u5c11\u4e00\u4e2a\u89c4\u8303\u6027\u7684\u4e1c\u897f\uff0c\u5c31\u662f\u201c\u6e38\u51fb\u961f\u201d\u7684\u611f\u89c9\uff0c\u5982\u679c\u518d\u8fd9\u4e48\u5199\u4e0b\u53bb\uff0c\u4f30\u8ba1\u4e5f\u5f88\u96be\u6210\u4e3a\u201c\u6b63\u89c4\u519b\u201d\uff0c\u6c34\u5e73\u4e5f\u4e0d\u4f1a\u6709\u5b9e\u8d28\u6027\u7684\u63d0\u9ad8\uff0c\u6240\u4ee5\u89c9\u5f97\u7ed9\u81ea\u5df1\u5199shell\u811a\u672c\uff08\u4ee5\u540e\u53ef\u80fd\u8fd8\u4f1a\u6709Python\u3001PHP\u3001Perl\u8fd9\u4e9b\u2026\u2026\uff09\u5b9a\u4e0b\u4e00\u4e2a\u9700\u8981\u9075\u5b88\u7684\u89c4\u8303\u4f1a\u5bf9\u81ea\u5df1\u7684\u6210\u957f\u6709\u6240\u5e2e\u52a9\uff0c\u4e8e\u662f\u5c31\u6709\u4e86\u8fd9\u7bc7\u6587\u7ae0\u3002\u6210\u957f\u662f\u4e00\u4e2a\u79ef\u7d2f\u7684\u8fc7\u7a0b\uff0c\u613f\u8d8a\u6765\u8d8a\u597d\uff01<\/p>\n<h5>\u5199Bash\u811a\u672c\u7684\u4e00\u4e9b\u89c4\u8303\u5c0f\u7ed3\uff1a<\/h5>\n<h6>0.\u6dfb\u52a0 debug \u6807\u8bc6{\u65b9\u4fbf\u8c03\u8bd5\u811a\u672c\uff0c\u9a8c\u8bc1\u6b63\u786e\u6027}<\/h6>\n<pre class=\"lang:default decode:true\">Debug=\"N\"\nmail_to=\"user@ixyzero.com\u00a0root@ixyzero.com\"\n[[ $Debug = 'Y' ]] &amp;&amp; mail_to=\"user@ixyzero.com\"<\/pre>\n<h6>1.PATH\u8bbe\u7f6e<\/h6>\n<pre class=\"lang:default decode:true\"># find \/ -type d -iname \"bin\"\n\/usr\/local\/bin\n\/usr\/bin\n\/bin\n# find \/ -type d -iname \"sbin\"\n\/usr\/local\/sbin\n\/usr\/sbin\n\/sbin\n\nexport PATH=$PATH:\/usr\/local\/bin:\/usr\/bin:\/bin:\/sbin:\/usr\/sbin\/:\/usr\/local\/sbin<\/pre>\n<h6>2.\u811a\u672c\u6240\u5728\u76ee\u5f55\u3001\u811a\u672c\u540d\u79f0<\/h6>\n<pre class=\"lang:default decode:true\">SCRIPT_SELF_PATH=\"$(cd \"$(dirname \"$0\")\"; pwd)\"\nSCRIPT_SELF_NAME=\"$(basename \"$0\")\"\n\nPROGNAME=`type $0 | awk '{print $3}'`  # search for executable on path\nPROGDIR=`dirname $PROGNAME`            # extract directory of program\nPROGNAME=`basename $PROGNAME`          # base name of program<\/pre>\n<h6>3.\u6839\u636e\u4e0a\u4e00\u4e2a\u547d\u4ee4\u7684\u8fd4\u56de\u503c\u6253\u5370\u63d0\u793a\u6d88\u606f<\/h6>\n<pre class=\"lang:default decode:true\">if [ $? -eq 0 ]; then\n    echo \"INFO|$(date +'%Y-%m-%d %H:%M:%S')|${FUNCNAME[0]}|Succeed!\"\nelse\n    echo \"ERROR|$(date +'%Y-%m-%d %H:%M:%S')|${FUNCNAME[0]}|Failed!\"\n    return 1\nfi<\/pre>\n<h6>4.\u6587\u4ef6[\u5939]\u4e0d\u5b58\u5728\u5219\u65b0\u5efa<\/h6>\n<pre class=\"lang:default decode:true\">result_path=\"\/root\/tmp\/result\"\nif [ ! -d \"$result_path\"]; then\n    mkdir -p \"$result_path\"\nfi<\/pre>\n<p>\u6216<\/p>\n<pre class=\"lang:default decode:true\">result_path=\"${HOME}\/hostsList\"\n[ -d \"$result_path\" ] || mkdir -p \"$result_path\"<\/pre>\n<h6>5.\u8ba1\u7b97\u811a\u672c\u7684\u6267\u884c\u6d88\u8017\u65f6\u95f4<\/h6>\n<pre class=\"lang:default decode:true\">function func_show_cost_time() {\n    local cost_all=\"$SECONDS\"\n    local cost_h=$((cost_all\/3600))\n    local cost_m=$((cost_all%3600\/60))\n    local cost_s=$((cost_all%60))\n    echo \"INFO|cost|$cost_all|$cost_h:$cost_m:$cost_s\"\n}<\/pre>\n<h6>6.\u67d0\u4e00\u5b57\u7b26\u96c6\u7684for\u5faa\u73af<\/h6>\n<pre class=\"lang:default decode:true\">items=\"0123456789abcdefghijklmnopqrstuvwxyz\"\ni=\"\"\nfor ((i=0;;i++)); do\n    item=${items:$i:1}\n    if [ -z \"$item\" ]; then\n        break\n    fi\n    echo $item\n    # Put command here\n    if [ $? -ne 0 ]; then\n        echo \"ERROR|command execute failed\"\n        exit 1\n    else\n        echo \"INFO|command execute succeed\"\n    fi\ndone<\/pre>\n<h6>7.\u67d0\u4e9b\u76f4\u63a5\u547d\u4ee4\u7684\u4f7f\u7528<\/h6>\n<pre class=\"lang:default decode:true\">pgrep\nreadlink\nexec_user=$(ps -o user --no-headers \"$pid\")<\/pre>\n<h6>8.\u8df3\u8fc7\u7a7a\u884c\/\u6ce8\u91ca\u884c<\/h6>\n<pre class=\"lang:default decode:true\">cat file.conf | grep -Ev \"(^$|^#$)\"<\/pre>\n<h6>9.\u9632\u6b62\u56e0\u4e3a\u6743\u9650\u4e0d\u591f\u5bfc\u81f4\u7684\u9519\u8bef<\/h6>\n<pre class=\"lang:default decode:true\">tmp=$(cat \/etc\/rsyslog.conf 2&gt;\/dev\/null || sudo cat \/etc\/rsyslog.conf 2&gt;\/dev\/null)<\/pre>\n<h6>10.\u7528test\u547d\u4ee4\u7b80\u5316\u811a\u672c<\/h6>\n<pre class=\"lang:default decode:true\">test \"$cc\" == \"163_box\" &amp;&amp; echo \"$notice_type\" | grep -q \"alert\"\ntest $? -eq 0 &amp;&amp; mail_cc=\"${mail_cc},root@ixyzero.com\"\n\n# http:\/\/www.ict.griffith.edu.au\/anthony\/info\/shell\/script.hints<\/pre>\n<h6>11.\u6839\u636e\u6587\u4ef6\u7684\u65b0\u65e7\u7a0b\u5ea6\u79fb\u52a8\u5230\u6307\u5b9a\u4f4d\u7f6e<\/h6>\n<pre class=\"lang:default decode:true \">tmp_file=$(mktemp -p ${HOME})\ntmp_time=$(date -d \"-2 days\" +'%Y-%m-%d 00:00:05')\ntouch -d \"$tmp_time\" \"$tmp_file\"\nfind ${HOME} -maxdepth 1 -type f -iname \"${HOSTNAME}*.txt\" ! -newer \"$tmp_file\" -print0 | xargs -0 -I {} mv {} ${result_path}\nrm -f $tmp_file<\/pre>\n<h6>12.shell\u811a\u672c\u5728\u6267\u884c\u5931\u8d25\u65f6\u7acb\u5373\u9000\u51fa<\/h6>\n<h6>\u641c\u7d22\u5173\u952e\u5b57\uff1a<\/h6>\n<ul>\n<li>linux bash script exit on error<\/li>\n<\/ul>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/1378274\/in-a-bash-script-how-can-i-exit-the-entire-script-if-a-certain-condition-occurs\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/1378274\/in-a-bash-script-how-can-i-exit-the-entire-script-if-a-certain-condition-occurs<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/2870992\/automatic-exit-from-bash-shell-script-on-error\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/2870992\/automatic-exit-from-bash-shell-script-on-error<\/a><\/li>\n<\/ul>\n<h6>\u53c2\u8003\u89e3\u7b54\uff1a<\/h6>\n<pre class=\"lang:default decode:true\">set -e    #add this to the second line of the bash script\nexit 0\/1\/2\/...\nbash -e script_name.sh<\/pre>\n<ul>\n<li><a href=\"https:\/\/github.com\/alebcay\/awesome-shell\" target=\"_blank\">Awesome Shell<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/progrium\/bashstyle\" target=\"_blank\">Bash Style<\/a><\/li>\n<\/ul>\n<p>=\u5f85\u7eed=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a5\u89e6Linux\u5df2\u7ecf4\u30015\u5e74\u4e86\uff0c\u4e5f\u5199\u4e86\u4e0d\u5c11\u7684shell\u811a\u672c\uff08\u867d\u7136\u8bf4\u90fd\u5f88\u7b80\u5355\uff09\uff0c\u4e0d\u8fc7\u4e00\u76f4\u90fd\u6ca1\u6709\u5199\u8fc7\u6bd4\u8f83\u5927\u7684\u9879\u76ee\uff0c\u5e73 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,11,7,12],"tags":[83,19],"class_list":["post-2160","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-linux","category-programing","category-tools","tag-bash","tag-tips"],"views":3429,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2160","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=2160"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2160\/revisions"}],"predecessor-version":[{"id":3378,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2160\/revisions\/3378"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=2160"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=2160"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=2160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}