{"id":1792,"date":"2015-01-18T15:47:47","date_gmt":"2015-01-18T07:47:47","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=1792"},"modified":"2015-01-18T15:47:47","modified_gmt":"2015-01-18T07:47:47","slug":"bash%e7%9a%84%e7%9f%a5%e8%af%86%e7%82%b9%e6%80%bb%e7%bb%93_0","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/1792.html","title":{"rendered":"Bash\u7684\u77e5\u8bc6\u70b9\u603b\u7ed3_0"},"content":{"rendered":"<h5>1.\u5728bash\u4e2d\u5982\u4f55\u6e05\u9664\u67d0\u4e2a\u53d8\u91cf\u5468\u56f4\u7684\u7a7a\u767d\u7b26\u53f7\uff08\u8d77\u5230\u7c7b\u4f3c\u4e8ePython\u4e2d\u7684strip()\u51fd\u6570\u7684\u4f5c\u7528\uff09\uff1f<\/h5>\n<p><a href=\"http:\/\/search.aol.com\/aol\/search?q=shell+strip+space\" target=\"_blank\">http:\/\/search.aol.com\/aol\/search?q=shell+strip+space<\/a><\/p>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/13659318\/how-to-remove-space-from-string\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/13659318\/how-to-remove-space-from-string<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/369758\/how-to-trim-whitespace-from-bash-variable\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/369758\/how-to-trim-whitespace-from-bash-variable<\/a><\/li>\n<li><a title=\"Linux shell\u811a\u672c\u7684\u5b57\u7b26\u4e32\u622a\u53d6\u7b49\u64cd\u4f5c\" href=\"http:\/\/ixyzero.com\/blog\/archives\/512.html\" target=\"_blank\">Linux shell\u811a\u672c\u7684\u5b57\u7b26\u4e32\u622a\u53d6\u7b49\u64cd\u4f5c<\/a><\/li>\n<\/ul>\n<h6>\u65b9\u6cd5\u4e00\uff1atr\u547d\u4ee4<\/h6>\n<pre class=\"lang:default decode:true\">tr -d ' '\necho \" test test test \" | tr -d ' '<\/pre>\n<h6>\u65b9\u6cd5\u4e8c\uff1ased\u547d\u4ee4<\/h6>\n<pre class=\"lang:default decode:true\"># sed 's\/ \/\/g'\n# echo \" test test test \" | sed 's\/ *$\/\/'\n# echo \" test test test \" | sed 's\/^ *\/\/'\n# echo \" test test test \" | sed -e 's\/^ *\/\/' -e 's\/ *$\/\/'<\/pre>\n<h6>\u65b9\u6cd5\u4e09\uff1a<\/h6>\n<p>\u5b57\u7b26\u4e32\u622a\u53d6<\/p>\n<pre class=\"lang:default decode:true\"># rtrim=${trimmed%% }\n# ltrim=${trimmed## }<\/pre>\n<p>\u5b57\u7b26\u4e32\u66ff\u6362<\/p>\n<pre class=\"lang:default decode:true\"># echo ${s\/\/[[:blank:]]\/}<\/pre>\n<h6>\u65b9\u6cd5\u56db\uff1axargs\u547d\u4ee4<\/h6>\n<pre class=\"lang:default decode:true\"># echo \" lol \" | xargs<\/pre>\n<p>\u5dee\u4e0d\u591a\u5c31\u8fd9\u4e9b\u4e86\uff0c\u5176\u5b83\u7684\u4e5f\u591a\u591a\u5c11\u5c11\u662f\u4e00\u4e9b\u53d8\u5f62\u2026\u2026<\/p>\n<h5>2.\u83b7\u53d6\u6587\u4ef6\u7684\u76ee\u5f55\u540d\/\u6587\u4ef6\u540d<\/h5>\n<pre class=\"lang:default decode:true\">#!\/bin\/bash\n\n# 1.\u83b7\u53d6\u811a\u672c\u6240\u5728\u76ee\u5f55\u7684\u540d\u79f0\nbase_dir=$(dirname $0)\/..\npwd\ncd $base_dir\npwd\n\n# 2.\u83b7\u53d6\u811a\u672c\u7684\u6587\u4ef6\u540d\nfile_abs_name=$0\nbase_name=${file_abs_name##\/*\/}\nbase_name_no_ext=${base_name%.*}<\/pre>\n<ul>\n<li><a title=\"Linux shell\u811a\u672c\u7684\u5b57\u7b26\u4e32\u622a\u53d6\u7b49\u64cd\u4f5c\" href=\"http:\/\/ixyzero.com\/blog\/archives\/512.html\" target=\"_blank\">Linux shell\u811a\u672c\u7684\u5b57\u7b26\u4e32\u622a\u53d6\u7b49\u64cd\u4f5c<\/a><\/li>\n<\/ul>\n<h5>3.grep\u547d\u4ee4\u7684\u4e00\u4e9btips<\/h5>\n<pre class=\"lang:default decode:true \">   -e PATTERN, --regexp=PATTERN\n\t  Use  PATTERN  as\tthe  pattern.\tThis  can be used to specify multiple search patterns, or to protect a pattern beginning with a hyphen (-).  (-e is specified by POSIX.)\n\n   -f FILE, --file=FILE\n\t  Obtain patterns from FILE, one per line.\tThe empty file contains zero patterns, and therefore matches nothing.  (-f is specified by POSIX.)\n\n   -i, --ignore-case\n\t  Ignore case distinctions in both the PATTERN and the input files.  (-i is specified by POSIX.)\n\n   -v, --invert-match\n\t  Invert the sense of matching, to select non-matching lines.  (-v is specified by POSIX.)\n\n   -c, --count\n\t  Suppress normal output; instead print a count of matching lines for each input file.  With the -v, --invert-match  option  (see  below),\tcount  non-matching lines.  (-c is specified by POSIX.)\n\n   -l, --files-with-matches\n\t  Suppress\tnormal output; instead print the name of each input file from which output would normally have been printed.  The scanning will stop on the first match.  (-l is specified by POSIX.)\n\n   -q, --quiet, --silent\n\t  Quiet; do not write anything to standard output.\tExit immediately with zero status if any match is found, even if an error was detected. Also see the -s or --no-messages option.  (-q is specified by POSIX.)\n\n   -n, --line-number\n\t  Prefix each line of output with the 1-based line number within its input file.  (-n is specified by POSIX.)\n\nContext Line Control\n   -A NUM, --after-context=NUM\n\t  Print NUM lines of trailing context after matching lines.  Places a line containing a group separator (--)  between  contiguous  groups  of  matches. With the -o or --only-matching option, this has no effect and a warning is given.\n\n   -B NUM, --before-context=NUM\n\t  Print  NUM  lines  of  leading  context before matching lines.  Places a line containing a group separator (--) between contiguous groups of matches. With the -o or --only-matching option, this has no effect and a warning is given.\n\n   -C NUM, -NUM, --context=NUM\n\t  Print NUM lines of output context.  Places a line containing a  group  separator\t(--)  between  contiguous  groups  of  matches.   With\tthe  -o  or --only-matching option, this has no effect and a warning is given.<\/pre>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true \"># cat \/proc\/net\/dev\nInter-|   Receive                                                |  Transmit\n face |bytes    packets errs drop fifo frame compressed multicast|bytes    packets errs drop fifo colls carrier compressed\n    lo:14551283   76129    0    0    0     0          0         0 14551283   76129    0    0    0     0       0          0\n  eth0:18007778  145326    0    0    0     0          0         0   502374    3647    0    0    0     0       0          0\n  sit0:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0\n\nMethod.2\n# grep -A1 'lo:' \/proc\/net\/dev | grep -v 'lo:' | awk -F':' '{print $1}'\n  eth0\n\n# grep -A1 'lo:' \/proc\/net\/dev | grep -v 'lo:' | awk -F':' '{print $1}' | sed 's\/[[:space:]]\/\/g'\neth0\n\nMethod.3\nIn [5]: print os.popen(\"grep -A1 'lo:' \/proc\/net\/dev | grep -v 'lo:' | awk -F':' '{print $1}' | sed 's\/[[:space:]]\/\/g'\").read()\neth0\nIn [6]: print os.popen(\"grep -A1 'lo:' \/proc\/net\/dev | grep -v 'lo:' | awk -F':' '{print $1}'\").read().strip()\neth0<\/pre>\n<h5>4.shell\u4e2d\u7684\u6761\u4ef6\u6d4b\u8bd5<\/h5>\n<ul>\n<li><a title=\"Shell\u6761\u4ef6\u6d4b\u8bd5\" href=\"http:\/\/ixyzero.com\/blog\/archives\/1225.html\" target=\"_blank\">Shell\u6761\u4ef6\u6d4b\u8bd5<\/a><\/li>\n<li>File test operators <a href=\"http:\/\/tldp.org\/LDP\/abs\/html\/fto.html\" target=\"_blank\">http:\/\/tldp.org\/LDP\/abs\/html\/fto.html<\/a><\/li>\n<li><a href=\"http:\/\/tldp.org\/LDP\/Bash-Beginners-Guide\/html\/sect_07_01.html\" target=\"_blank\">Introduction to if<\/a><\/li>\n<li><a href=\"http:\/\/search.aol.com\/aol\/search?q=shell+%E6%9D%A1%E4%BB%B6%E6%B5%8B%E8%AF%95\" target=\"_blank\">shell \u6761\u4ef6\u6d4b\u8bd5 &#8211; AOL Search Results<\/a><\/li>\n<li><a href=\"http:\/\/search.aol.com\/aol\/search?q=shell+file+if+test\" target=\"_blank\">http:\/\/search.aol.com\/aol\/search?q=shell+file+if+test<\/a><\/li>\n<li><a href=\"http:\/\/robots.thoughtbot.com\/the-unix-shells-humble-if\" target=\"_blank\">The Unix Shell&#8217;s Humble If<\/a><\/li>\n<li><a href=\"http:\/\/coderbee.net\/index.php\/linux\/20130509\/105\" target=\"_blank\">shell \u811a\u672c\u4e4b\u6761\u4ef6\u6d4b\u8bd5\u548c\u6d41\u7a0b\u7ed3\u6784<\/a><\/li>\n<li><a href=\"http:\/\/vbird.dic.ksu.edu.tw\/linux_basic\/0340bashshell-scripts.php\" target=\"_blank\">\u9e1f\u54e5\u7684 Linux \u79c1\u623f\u83dc &#8212; \u5b66\u4e60 Shell Scripts<\/a><\/li>\n<li><a href=\"http:\/\/mprc.pku.edu.cn\/mentors\/training\/TrainingCourses\/material\/ShellProgramming.HTM\" target=\"_blank\">Shell\u811a\u672c\u7f16\u7a0b\u7684\u5e38\u8bc6<\/a><\/li>\n<li><a href=\"http:\/\/www.ibm.com\/developerworks\/cn\/linux\/l-bash-test.html\" target=\"_blank\">Linux \u6280\u5de7: Bash \u6d4b\u8bd5\u548c\u6bd4\u8f83\u51fd\u6570<\/a><\/li>\n<\/ul>\n<h5>5.\u4f7f\u7528shell\u811a\u672c\u8fdb\u884c\u81ea\u52a8\u5316\u6d4b\u8bd5<\/h5>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/971945\/unit-testing-for-shell-scripts\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/971945\/unit-testing-for-shell-scripts<\/a><\/li>\n<li><a href=\"http:\/\/www.ibm.com\/developerworks\/library\/l-bashfv\/\" target=\"_blank\">Using Bash shell scripts for function testing<\/a><\/li>\n<\/ul>\n<h5>6.\u67e5\u770bLinux\u7684\u5185\u6838\u7248\u672c\u547d\u4ee4<\/h5>\n<pre class=\"lang:default decode:true \">$ uname -a\n\n$ lsb_release -a\n\n$ cat \/etc\/issue\n\n$ cat \/proc\/version\n\n$ getconf LONG_BIT\n\n$ file \/bin\/ls\n\n$ arch<\/pre>\n<h5>7.\u5bfc\u51famanual\u4e3atxt\u6587\u4ef6<\/h5>\n<pre class=\"lang:default decode:true \"># man grep | col -b &gt;grep_manual.txt<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.\u5728bash\u4e2d\u5982\u4f55\u6e05\u9664\u67d0\u4e2a\u53d8\u91cf\u5468\u56f4\u7684\u7a7a\u767d\u7b26\u53f7\uff08\u8d77\u5230\u7c7b\u4f3c\u4e8ePython\u4e2d\u7684strip()\u51fd\u6570\u7684\u4f5c\u7528\uff09\uff1f htt [&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,12],"tags":[83,77],"class_list":["post-1792","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-linux","category-tools","tag-bash","tag-grep"],"views":3128,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1792","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=1792"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1792\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=1792"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=1792"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=1792"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}