{"id":2007,"date":"2015-03-14T21:42:58","date_gmt":"2015-03-14T13:42:58","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=2007"},"modified":"2015-03-14T21:42:58","modified_gmt":"2015-03-14T13:42:58","slug":"linux%e4%b8%8b%e7%9a%84%e4%b8%80%e4%ba%9b%e4%bd%bf%e7%94%a8%e7%bb%8f%e9%aa%8c%e5%b0%8f%e7%bb%93_2","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/2007.html","title":{"rendered":"Linux\u4e0b\u7684\u4e00\u4e9b\u4f7f\u7528\u7ecf\u9a8c\u5c0f\u7ed3_2"},"content":{"rendered":"<h5>1.\u5982\u4f55\u76d1\u63a7\u3001\u5206\u6790Linux\u7cfb\u7edf\u7684\u8d1f\u8f7d\u72b6\u6001\u7b49\u5173\u952e\u4fe1\u606f\uff1f<\/h5>\n<pre class=\"lang:default decode:true \">$ uptime\n$ w\n$ cat \/proc\/loadavg\n$ vmstat\n$ mpstat -P ALL\n$ sar<\/pre>\n<pre class=\"lang:default decode:true\">\/proc\/net\/dev   (all netwrok interfaces for all their values)\n\/proc\/diskstats (all disks for all their values)\n\/proc\/net\/snmp  (total IPv4, TCP and UDP usage)\n\/proc\/net\/netstat    (more IPv4 usage)\n\/proc\/net\/stat\/nf_conntrack  (connection tracking performance)\n\/proc\/net\/ip_vs\/stats       (IPVS connection statistics)\n\/proc\/stat      (CPU utilization)\n\/proc\/meminfo   (memory information)\n\/proc\/vmstat    (system performance)<\/pre>\n<ul>\n<li><a href=\"https:\/\/github.com\/kahun\/awesome-sysadmin\" target=\"_blank\">https:\/\/github.com\/kahun\/awesome-sysadmin<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/sbilly\/awesome-security\" target=\"_blank\">https:\/\/github.com\/sbilly\/awesome-security<\/a><\/li>\n<\/ul>\n<h5>2.Linux\u4e0b\u7684\u6d6e\u70b9\u6570\u8ba1\u7b97<\/h5>\n<h6>\u641c\u7d22\u5173\u952e\u5b57\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/search.aol.com\/aol\/search?enabled_terms=&amp;s_it=comsearch&amp;q=linux+Floating+point+calculation\" target=\"_blank\">http:\/\/search.aol.com\/aol\/search? q=linux+Floating+point+calculation<\/a><\/li>\n<li><a href=\"http:\/\/search.aol.com\/aol\/search?s_it=topsearchbox.search&amp;s_chn=prt_aol20-os&amp;v_t=comsearch&amp;q=linux+expr+Floating+point+calculation\" target=\"_blank\">http:\/\/search.aol.com\/aol\/search? q=linux+expr+Floating+point+calculation<\/a><\/li>\n<\/ul>\n<h6>\u53c2\u8003\u7ed3\u8bba\uff1a<\/h6>\n<p>bc\u6216awk<\/p>\n<p>for maths operations, try to use bc or awk , not bash or expr<\/p>\n<pre class=\"lang:default decode:true\">&gt; echo \"scale = 2; 20 * 100 \/ 30\" | bc\n66.66\n&gt; echo \"scale = 2; 20 \/ 30 * 100\" | bc\n66.00\n\n&gt; echo \"22 5\" | awk '{printf \"%.2f \\n\", $1\/$2}'\n4.40<\/pre>\n<pre class=\"lang:default decode:true\"># bc &lt;&lt;&lt; 'scale=4;20+5\/2'\nbc &lt;&lt;&lt; 'scale=4;20+5\/2'\n22.5000\n\n# echo \"22 5\" | awk '{printf \"%.2f \\n\", $1\/$2}'\necho \"22 5\" | awk '{printf \"%.2f \\n\", $1\/$2}'\n4.40\n\n# expr 2+4\nexpr 2+4\n2+4\n# expr 2 + 4\nexpr 2 + 4\n6\n\n# echo 20 5 2 \/ + p | dc\necho 20 5 2 \/ + p | dc\n22\n# echo 4 k 20 5 2 \/ + p | dc\necho 4 k 20 5 2 \/ + p | dc\n22.5000\n\n# perl -E \"say 20+5\/2\"\nperl -E \"say 20+5\/2\"\n22.5\n\n# python -c \"print 20+5\/2\"\npython -c \"print 20+5\/2\"\n22\n# python -c \"print 20+5\/2.0\"\npython -c \"print 20+5\/2.0\"\n22.5\n\n$ php -r 'echo 20+5\/2;'\n22.5\n\n# ruby -e 'p 20+5\/2'\nruby -e 'p 20+5\/2'\n22\n# ruby -e 'p 20+5\/2.0'\nruby -e 'p 20+5\/2.0'\n22.5\n\n# sqlite3 &lt;&lt;&lt; 'select 20+5\/2;'\nsqlite3 &lt;&lt;&lt; 'select 20+5\/2;'\n22\n# sqlite3 &lt;&lt;&lt; 'select 20+5\/2.0;'\nsqlite3 &lt;&lt;&lt; 'select 20+5\/2.0;'\n22.5<\/pre>\n<pre class=\"lang:default decode:true\">#!\/bin\/bash\n# Floating point number functions.\n# http:\/\/www.linuxjournal.com\/content\/floating-point-math-bash\n\n# Default scale used by float functions.\nfloat_scale=2\n\n# Evaluate a floating point number expression.\nfunction float_eval() {\n    local stat=0\n    local result=0.0\n    if [[ $# -gt 0 ]]; then\n        result=$(echo \"scale=$float_scale; $*\" | bc -q 2&gt;\/dev\/null)\n        stat=$?\n        if [[ $stat -eq 0  &amp;&amp;  -z \"$result\" ]]; then stat=1; fi\n    fi\n    echo $result\n    return $stat\n}\n\n# Evaluate a floating point number conditional expression.\nfunction float_cond() {\n    local cond=0\n    if [[ $# -gt 0 ]]; then\n        cond=$(echo \"$*\" | bc -q 2&gt;\/dev\/null)\n        if [[ -z \"$cond\" ]]; then cond=0; fi\n        if [[ \"$cond\" != 0  &amp;&amp;  \"$cond\" != 1 ]]; then cond=0; fi\n    fi\n    local stat=$((cond == 0))\n    return $stat\n}\n\n# Test code if invoked directly.\nif [[ $(basename $0 .sh) == 'float' ]]; then\n    # Use command line arguments if there are any.\n    if [[ $# -gt 0 ]]; then\n        echo $(float_eval $*)\n    else\n        # Turn off pathname expansion so * doesn't get expanded\n        set -f\n        e=\"12.5 \/ 3.2\"\n        echo $e is $(float_eval \"$e\")\n        e=\"100.4 \/ 4.2 + 3.2 * 6.5\"\n        echo $e is $(float_eval \"$e\")\n        if float_cond '10.0 &gt; 9.3'; then\n            echo \"10.0 is greater than 9.3\"\n        fi\n        if float_cond '10.0 &lt; 9.3'; then\n            echo \"Oops\"\n        else\n            echo \"10.0 is not less than 9.3\"\n        fi\n        a=12.0\n        b=3.0\n        c=$(float_eval \"$a \/ $b\")\n        echo \"$a \/ $b\" is $c\n        set +f\n    fi\nfi<\/pre>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/12722095\/how-can-i-get-a-float-division-in-bash\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/12722095\/how-can-i-get-a-float-division-in-bash<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/12147040\/division-in-script-and-floating-point\/24431665#24431665\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/12147040\/division-in-script-and-floating-point<\/a><\/li>\n<li><a href=\"http:\/\/www.linuxjournal.com\/content\/floating-point-math-bash\" target=\"_blank\">http:\/\/www.linuxjournal.com\/content\/floating-point-math-bash<\/a><\/li>\n<li><a href=\"http:\/\/unix.stackexchange.com\/questions\/40786\/how-to-do-integer-float-calculations-in-bash-or-other-languages-frameworks\" target=\"_blank\">http:\/\/unix.stackexchange.com\/questions\/40786\/how-to-do-integer-float-calculations-in-bash-or-other-languages-frameworks<\/a><\/li>\n<\/ul>\n<h5>3.grep\u7684\u4e00\u4e9b\u77e5\u8bc6\u70b9<\/h5>\n<h6>\u641c\u7d22\u5173\u952e\u5b57\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/search.aol.com\/aol\/search?q=grep+-E+pattern\" target=\"_blank\">http:\/\/search.aol.com\/aol\/search?q=grep+-E+pattern<\/a><\/li>\n<li><a href=\"http:\/\/search.aol.com\/aol\/search?q=grep+perl-regexp\" target=\"_blank\">http:\/\/search.aol.com\/aol\/search?q=grep+perl-regexp<\/a><\/li>\n<li><a href=\"http:\/\/search.aol.com\/aol\/search?q=grep+-P+%27the+exclamation+mark%27\" target=\"_blank\">http:\/\/search.aol.com\/aol\/search?q=grep+-P+%27the+exclamation+mark%27<\/a><\/li>\n<\/ul>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"https:\/\/www.gnu.org\/software\/grep\/manual\/grep.html\" target=\"_blank\">https:\/\/www.gnu.org\/software\/grep\/manual\/grep.html<\/a><\/li>\n<li><a href=\"http:\/\/linux.die.net\/man\/3\/pcrepattern\" target=\"_blank\">http:\/\/linux.die.net\/man\/3\/pcrepattern<\/a><\/li>\n<li><a href=\"http:\/\/www.thegeekstuff.com\/2011\/10\/grep-or-and-not-operators\/\" target=\"_blank\">http:\/\/www.thegeekstuff.com\/2011\/10\/grep-or-and-not-operators\/ <\/a>\u00a0#Nice<\/li>\n<li><a href=\"http:\/\/unix.stackexchange.com\/questions\/21764\/grep-or-regex-problem\" target=\"_blank\">http:\/\/unix.stackexchange.com\/questions\/21764\/grep-or-regex-problem <\/a>\u00a0#Nice<\/li>\n<li><a href=\"http:\/\/ss64.com\/bash\/grep.html\" target=\"_blank\">http:\/\/ss64.com\/bash\/grep.html <\/a>\u00a0#Nice<\/li>\n<li><a href=\"http:\/\/www.cyberciti.biz\/faq\/grep-regular-expressions\/\" target=\"_blank\">http:\/\/www.cyberciti.biz\/faq\/grep-regular-expressions\/<\/a><\/li>\n<li><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/using-grep-regular-expressions-to-search-for-text-patterns-in-linux\" target=\"_blank\">https:\/\/www.digitalocean.com\/community\/tutorials\/using-grep-regular-expressions-to-search-for-text-patterns-in-linux<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/9081\/grep-a-file-but-show-several-surrounding-lines\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/9081\/grep-a-file-but-show-several-surrounding-lines<\/a><\/li>\n<li><a href=\"http:\/\/ixyzero.com\/blog\/regex.html\" target=\"_blank\">http:\/\/ixyzero.com\/blog\/regex.html<\/a><\/li>\n<\/ul>\n<h6>#grep\u5728\u5339\u914d\u65f6\uff0c\u9700\u8981\u5c06\u611f\u53f9\u53f7\u201d!\u201d\u7ed9\u8f6c\u4e49\u6389<\/h6>\n<ul>\n<li><a href=\"http:\/\/unix.stackexchange.com\/questions\/33339\/cant-use-exclamation-mark-in-bash\" target=\"_blank\">http:\/\/unix.stackexchange.com\/questions\/33339\/cant-use-exclamation-mark-in-bash<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/13461828\/escaping-the-exclamation-point-in-grep\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/13461828\/escaping-the-exclamation-point-in-grep<\/a><\/li>\n<\/ul>\n<h5>4.\u7528grep\u8fdb\u884c\u201c\u6216\u201d\u5339\u914d<\/h5>\n<p>==\u5b9e\u73b0\u201c\u6216\u201d\u7684\u529f\u80fd==<br \/>\n\u5c06\u4e0b\u9762\u7684\u8fd93\u4e2a\u6b63\u5219\u5408\u5e76\u4e3a1\u4e2a\uff1a<\/p>\n<pre class=\"lang:default decode:true\">userID=[0-9]{16}\nname='sp_id' value='[0-9]{16}'\nId=[0-9]{16}<\/pre>\n<p>\u76f4\u63a5\u5728\u547d\u4ee4\u884c\u4e0b\uff1a<\/p>\n<pre class=\"lang:default decode:true\"># grep -Ei \"id(' value='|=)[0-9]{16}\" all.log\n# cat all.log | grep -Ei \"id(' value='|=)[0-9]{16}\"<\/pre>\n<p>\u5728\u811a\u672c\u4e2d\uff1a<\/p>\n<pre class=\"lang:default decode:true\">#!\/bin\/bash\nregex_1=\"id('\\svalue='|=)[0-9]{16}|CDATA\\[[0-9]{16}\" #\u9700\u8981\u5c06\u7a7a\u683c\u66ff\u6362\u4e3a\u2019\\s\u2019\ngrep -Ei $regex_1 all.log\ngrep -Pi $regex_1 all.log<\/pre>\n<p>\u5982\u679c\u4e0a\u9762\u7684\u90a3\u4e2a\u7a7a\u683c\u6ca1\u6709\u88ab\u66ff\u6362\u4e3a&#8217;\\s&#8217;\u5c31\u4f1a\u62a5\u9519\uff1a<br \/>\ngrep: Unmatched ( or \\(<\/p>\n<pre class=\"lang:default decode:true \"># xxxx=\"id' value='[0-9]{16}\"\n# echo $xxxx\nid' value='[0-9]{16}\n# cat all.log | grep -Ei $xxxx\ngrep: value='[0-9]{16}: No such file or directory\n# cat all.log | grep -Ei \"id' value='[0-9]{16}\"\n...ok...\n==\n# xxxx=\"id\\'\\ value=\\'[0-9]{16}\"\n# cat all.log | grep -Ei $xxxx\ngrep: Trailing backslash\n==\n# xxxx=\"id'\\svalue='[0-9]{16}\"\n# cat all.log | grep -Ei $xxxx\n...ok...<\/pre>\n<p>\u7ed3\u8bba\uff1a\u8bb0\u5f97\u7a7a\u683c\u7528&#8217;\\s&#8217;\u66ff\u6362\uff08\u7279\u6b8a\u7b26\u53f7\u8bb0\u5f97\u8f6c\u4e49\u5904\u7406\uff09\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.\u5982\u4f55\u76d1\u63a7\u3001\u5206\u6790Linux\u7cfb\u7edf\u7684\u8d1f\u8f7d\u72b6\u6001\u7b49\u5173\u952e\u4fe1\u606f\uff1f $ uptime $ w $ cat \/proc\/lo [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,11,6],"tags":[74,477,77,30],"class_list":["post-2007","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-linux","category-other","tag-awk","tag-bc","tag-grep","tag-linux"],"views":2441,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2007","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=2007"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2007\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=2007"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=2007"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=2007"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}