{"id":2080,"date":"2015-04-18T17:53:18","date_gmt":"2015-04-18T09:53:18","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=2080"},"modified":"2015-04-18T17:53:18","modified_gmt":"2015-04-18T09:53:18","slug":"linux%e4%b8%8b%e7%9a%84%e5%b0%8f%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\/2080.html","title":{"rendered":"Linux\u4e0b\u7684\u5c0f\u77e5\u8bc6\u70b9\u603b\u7ed3_0"},"content":{"rendered":"<p>\u6700\u8fd1\u505a\u7684\u4e8b\u60c5\u90fd\u6bd4\u8f83\u96f6\u6563\uff0c\u4e0d\u592a\u7cfb\u7edf\uff0c\u5982\u679c\u4e0d\u53ca\u65f6\u8bb0\u5f55\u4e0b\u6765\u7684\u8bdd\uff0c\u518d\u9694\u4e00\u6bb5\u65f6\u95f4\u53bb\u770b\u7684\u8bdd\u5c31\u5bb9\u6613\u5fd8\u8bb0\u4e4b\u524d\u5904\u7406\/\u89e3\u51b3\u95ee\u9898\u65f6\u7528\u5230\u7684\u77e5\u8bc6\u3001\u6280\u5de7\uff0c\u53c8\u5f97\u82b1\u65f6\u95f4\uff0c\u6240\u4ee5\u4e0b\u9762\u5c31\u628a\u8fd1\u4e00\u6bb5\u65f6\u95f4\u78b0\u5230\u7684\u95ee\u9898\u3001\u60f3\u5230\u7684\u65b9\u6cd5\u8bb0\u5f55\u4e00\u4e9b\uff0c\u65b9\u4fbf\u540e\u671f\u968f\u65f6\u67e5\u770b\u3002<\/p>\n<h5>1.Vim\u8df3\u8f6c<\/h5>\n<p>\u9700\u5b9e\u73b0\u7684\u529f\u80fd\uff1a\u201cvim \u8df3\u81f3\u4e0a\u4e00\u4f4d\u7f6e\u201d<\/p>\n<pre class=\"lang:default decode:true\">''  #\u8df3\u8f6c\u5230\u5149\u6807\u4e0a\u6b21\u505c\u9760\u7684\u5730\u65b9\uff0c\u662f\u4e24\u4e2a\u5355\u5f15\u53f7\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u53cc\u5f15\u53f7<\/pre>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li>vim\u64cd\u4f5c\u5927\u5168 | \u94bf\u7551\u7684\u535a\u5ba2 <a href=\"http:\/\/54im.com\/linux\/vim%E6%93%8D%E4%BD%9C%E5%A4%A7%E5%85%A8.html\">http:\/\/54im.com\/linux\/vim%E6%93%8D%E4%BD%9C%E5%A4%A7%E5%85%A8.html<\/a><\/li>\n<li>VIM \u8df3\u8f6c | Andy&#8217;s Home <a href=\"http:\/\/www.wm177.net\/?p=175\">http:\/\/www.wm177.net\/?p=175<\/a><\/li>\n<li>linux\u4e0bvim\u547d\u4ee4\u8be6\u89e3 &#8211; \u5fc3\u9759\u8339\u6c34 <a href=\"http:\/\/seagg.github.io\/blog\/2013\/04\/26\/vim\/\">http:\/\/seagg.github.io\/blog\/2013\/04\/26\/vim\/<\/a><\/li>\n<li>VIM: quickref <a href=\"http:\/\/vimcdoc.sourceforge.net\/doc\/quickref.html\">http:\/\/vimcdoc.sourceforge.net\/doc\/quickref.html<\/a><\/li>\n<\/ul>\n<p>==<\/p>\n<h5>2.C\u8bed\u8a00\u57fa\u7840\u5b66\u4e60<\/h5>\n<h6>\u4ee3\u7801\u7247\u6bb5\uff1a<\/h6>\n<pre class=\"lang:default decode:true  \">#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n\n\/*\n* return 0: s is not an empty string\n* return 1: s is an empty string\n* http:\/\/stackoverflow.com\/questions\/3981510\/getline-check-if-line-is-whitespace\n*\/\nint str_is_empty(const char *s) {\n    while (*s != '\\0') {\n        if (!isspace(*s))\n            return 0;\n        s++;\n    }\n    return 1;\n}\n\nvoid main() {\n    char * s;\n    const char * a_space = \" \";\n    const char * a_newline = \"\\n\";\n    const char * a_test = \" \\n\\t   \";\n    const char * a_str = \" a_str \";\n    printf(\"' ' is %s\\n\", s = str_is_empty(a_space) ? \"empty\" : \"not_empty\");\n    printf(\"'\\\\n' is %s\\n\", s = str_is_empty(a_space) ? \"empty\" : \"not_empty\");\n    printf(\"' \\\\n\\\\t  ' is %s\\n\", s = str_is_empty(a_test) ? \"empty\" : \"not_empty\");\n    printf(\"' a_str ' is %s\\n\", s = str_is_empty(a_str) ? \"empty\" : \"not_empty\");\n}<\/pre>\n<h6>\u53c2\u8003\u6559\u7a0b\uff1a<\/h6>\n<ul>\n<li>\u9ad8\u8d28\u91cfC++\/C\u7f16\u7a0b\u6307\u5357 <a href=\"http:\/\/oss.org.cn\/man\/develop\/c&amp;c++\/c\/c.htm\">http:\/\/oss.org.cn\/man\/develop\/c&amp;c++\/c\/c.htm<\/a><\/li>\n<li>Linux C\u7f16\u7a0b\u4e00\u7ad9\u5f0f\u5b66\u4e60 <a href=\"http:\/\/akaedu.github.io\/book\/\">http:\/\/akaedu.github.io\/book\/<\/a><\/li>\n<li>qyuhen\/book \u00b7 GitHub <a href=\"https:\/\/github.com\/qyuhen\/book\">https:\/\/github.com\/qyuhen\/book<\/a><\/li>\n<li>Standard C\u8bed\u8a00\u6807\u51c6\u51fd\u6570\u5e93\u901f\u67e5 (Cheat Sheet) <a href=\"http:\/\/ganquan.info\/standard-c\/\">http:\/\/ganquan.info\/standard-c\/<\/a><\/li>\n<li>Linux\u5de5\u5177\u5feb\u901f\u6559\u7a0b \u2014 <a href=\"http:\/\/linuxtools-rst.readthedocs.org\/zh_CN\/latest\/\">http:\/\/linuxtools-rst.readthedocs.org\/zh_CN\/latest\/<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/justjavac\/free-programming-books-zh_CN\">https:\/\/github.com\/justjavac\/free-programming-books-zh_CN<\/a><\/li>\n<\/ul>\n<p>==<\/p>\n<h5>3.Linux\u4e0b\u8fdb\u884c\u5b57\u7b26\u4e32\u7684\u67e5\u627e\u548c\u66ff\u6362\u3001xargs\u7684-i\u9009\u9879<\/h5>\n<h6>\u641c\u7d22\u5173\u952e\u5b57\uff1a<\/h6>\n<ul>\n<li>Linux find and replace<\/li>\n<li>find xargs -i<\/li>\n<\/ul>\n<h6>\u53c2\u8003\u5185\u5bb9\uff1a<\/h6>\n<pre class=\"lang:default decode:true \">$ find \/tmp\/ -ctime -1 -name \"x*\" | xargs -I '{}' mv '{}' ~\/play\/\n$ file * | awk -F: '\/image\/ { print $1 }' | xargs -I '{}' mv '{}' destination\n\n$ find \/tmp\/ -ctime -1 -name 'x*' -print0 | xargs -r0 mv -t ~\/play\/\t# GNU's mv command\n$ file * | awk -F: '\/image\/ { print $1 }' | xargs mv -t destination\n\n$ find . -name \"*.bak\" -print0 | xargs -0 -I {} mv {} ~\/old.files\n$ find . -name \"*.bak\" -print0 | xargs -0 -I file mv file ~\/old.files<\/pre>\n<pre class=\"nums:false lang:default decode:true\">-I replace-str\n--replace[=replace-str]\n-i[replace-str]\n    Replace occurrences of replace-str in the initial arguments with names read from standard input. Also, unquoted blanks do not terminate arguments; instead, the input is split at newlines only. If replace-str is omitted (omitting it is allowed only for \u2018-i\u2019), it defaults to \u2018{}\u2019 (like for \u2018find -exec\u2019). Implies \u2018-x\u2019 and \u2018-l 1\u2019. The \u2018-i\u2019 option is deprecated in favour of the \u2018-I\u2019 option.<\/pre>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/8588789\/linux-find-and-replace\">http:\/\/stackoverflow.com\/questions\/8588789\/linux-find-and-replace<\/a><\/li>\n<li><a href=\"http:\/\/unix.stackexchange.com\/questions\/90886\/how-can-i-find-files-and-then-use-xargs-to-move-them\">http:\/\/unix.stackexchange.com\/questions\/90886\/how-can-i-find-files-and-then-use-xargs-to-move-them<\/a><\/li>\n<li><a href=\"http:\/\/www.cyberciti.biz\/faq\/linux-unix-bsd-xargs-construct-argument-lists-utility\/\">http:\/\/www.cyberciti.biz\/faq\/linux-unix-bsd-xargs-construct-argument-lists-utility\/<\/a><\/li>\n<li><a href=\"http:\/\/man.linuxde.net\/xargs\">http:\/\/man.linuxde.net\/xargs<\/a><\/li>\n<li><a href=\"http:\/\/www.gnu.org\/software\/findutils\/manual\/html_node\/find_html\/xargs-options.html#xargs-options\">http:\/\/www.gnu.org\/software\/findutils\/manual\/html_node\/find_html\/xargs-options.html#xargs-options<\/a><\/li>\n<\/ul>\n<p>==<\/p>\n<h5>4.Linux\u4e0b\u7684Immutable files<\/h5>\n<h6>\u641c\u7d22\u5173\u952e\u5b57\uff1a<\/h6>\n<ul>\n<li>Linux Immutable files<\/li>\n<li>Linux lsattr<\/li>\n<\/ul>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li>Linux \u6587\u4ef6\u7cfb\u7edf\u5b89\u5168\u653b\u7565 <a href=\"http:\/\/www.ibm.com\/developerworks\/cn\/linux\/l-cn-filesystem-sec\/\">http:\/\/www.ibm.com\/developerworks\/cn\/linux\/l-cn-filesystem-sec\/<\/a><\/li>\n<li>\uff08\u603b\u7ed3\uff09Linux\u7684chattr\u4e0elsattr\u547d\u4ee4\u8be6\u89e3 <a href=\"http:\/\/www.ha97.com\/5172.html\">http:\/\/www.ha97.com\/5172.html<\/a><\/li>\n<li><a href=\"http:\/\/unix.stackexchange.com\/questions\/32256\/whats-the-meaning-of-output-of-lsattr\">http:\/\/unix.stackexchange.com\/questions\/32256\/whats-the-meaning-of-output-of-lsattr<\/a><\/li>\n<li><a href=\"http:\/\/www.cyberciti.biz\/tips\/linux-password-trick.html\">http:\/\/www.cyberciti.biz\/tips\/linux-password-trick.html<\/a><\/li>\n<li><a href=\"http:\/\/unix.stackexchange.com\/questions\/132795\/how-to-search-for-files-with-immutable-attribute-set\">http:\/\/unix.stackexchange.com\/questions\/132795\/how-to-search-for-files-with-immutable-attribute-set<\/a><\/li>\n<li><a href=\"http:\/\/www.tecmint.com\/chattr-command-examples\/\">http:\/\/www.tecmint.com\/chattr-command-examples\/<\/a><\/li>\n<\/ul>\n<p>==<\/p>\n<h5>5.Awk\u7684\u51fd\u6570\u4f7f\u7528\/\u5b66\u4e60<\/h5>\n<h6>\u641c\u7d22\u5173\u952e\u5b57\uff1a<\/h6>\n<ul>\n<li>awk substr<\/li>\n<li>awk match then substr<\/li>\n<li>awk str function remove comma<\/li>\n<li>awk function<\/li>\n<\/ul>\n<h6>\u53c2\u8003\u5199\u6cd5\uff1a<\/h6>\n<pre class=\"lang:default decode:true\">$ cat file.log | awk -F'|' '\/bin\/ {split($2, a, \" \"); print $1,a[1],substr(a[5], 0, length(a[5])-1),a[10]}' &gt; test.log\n\n$ cat file.log | awk -F'|' '\/bin\/ {split($2, a, \" \"); print $1,a[1],substr(a[5], 0, length(a[5])-1),a[10]\"-\"substr(a[11], 0, length(a[11])-2)}' &gt; test.log\n\n$ awk '{dic[$3]++}; END{for(x in dic) print dic[x]\"\\t\"x}' test.log<\/pre>\n<p>awk\u7684<span style=\"color: #ff0000;\"><strong>\u5185\u5efa\u51fd\u6570split<\/strong><\/span>\u5141\u8bb8\u4f60\u628a\u4e00\u4e2a\u5b57\u7b26\u4e32\u5206\u9694\u4e3a\u5355\u8bcd\u5e76\u5b58\u50a8\u5728\u6570\u7ec4\u4e2d\u3002\u4f60\u53ef\u4ee5\u81ea\u5df1\u5b9a\u4e49\u57df\u5206\u9694\u7b26\u6216\u8005\u4f7f\u7528\u73b0\u5728FS(\u57df\u5206\u9694\u7b26)\u7684\u503c<\/p>\n<p>\u683c\u5f0f\uff1a<\/p>\n<blockquote><p>\u00a0 split (string, array, field separator)<\/p>\n<p>split (string, array)\u00a0\u00a0 &#8211;&gt;\u5982\u679c\u7b2c\u4e09\u4e2a\u53c2\u6570\u6ca1\u6709\u63d0\u4f9b\uff0cawk\u5c31\u9ed8\u8ba4\u4f7f\u7528\u5f53\u524dFS\u503c<\/p><\/blockquote>\n<p><span style=\"color: #ff0000;\"><strong>\u5185\u5efa\u51fd\u6570substr<\/strong><\/span>\u622a\u53d6\u5b57\u7b26\u4e32{\u8fd4\u56de\u4ece\u8d77\u59cb\u4f4d\u7f6e\u8d77\uff0c\u6307\u5b9a\u957f\u5ea6\u7684\u5b50\u5b57\u7b26\u4e32\uff1b\u82e5\u672a\u6307\u5b9a\u957f\u5ea6\uff0c\u5219\u8fd4\u56de\u4ece\u8d77\u59cb\u4f4d\u7f6e\u5230\u5b57\u7b26\u4e32\u672b\u5c3e\u7684\u5b50\u5b57\u7b26\u4e32}<\/p>\n<p>\u683c\u5f0f\uff1a<\/p>\n<blockquote><p>\u00a0 substr(s, p) \u00a0 \u8fd4\u56de\u5b57\u7b26\u4e32s\u4e2d\u4ecep\u5f00\u59cb\u7684\u540e\u7f00\u90e8\u5206<\/p>\n<p>substr(s, p, n) \u8fd4\u56de\u5b57\u7b26\u4e32s\u4e2d\u4ecep\u5f00\u59cb\u957f\u5ea6\u4e3an\u7684\u540e\u7f00\u90e8\u5206<\/p><\/blockquote>\n<h6>\u51fd\u6570length\u8fd4\u56de\u6574\u4e2a\u8bb0\u5f55\u4e2d\u7684\u5b57\u7b26\u6570<\/h6>\n<pre class=\"lang:default decode:true\">$ echo \"123\" | awk '{print length}'\n3\n$ echo \"123456\" | awk '{print length}'\n6<\/pre>\n<h6>\u53bb\u6389\u6587\u4ef6\u4e2d\u7684utf-8 BOM\u5934<\/h6>\n<pre class=\"lang:default decode:true\">awk '{if(NR==1)sub(\/^xefxbbxbf\/,\"\") print}' input.txt &gt; output.txt<\/pre>\n<h6>\u7528sed\/awk\u53bb\u6389\u884c\u5c3e\u7684\u9017\u53f7<\/h6>\n<pre class=\"lang:default decode:true\">sed 's\/,$\/\/' file.txt &gt; file.nocomma\nawk '{print substr($0, 0, length($0)-1)}' file.txt\nawk '{gsub(\/,$\/,\"\"); print}' file.txt<\/pre>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li>awk\u5185\u7f6e\u51fd\u6570\u7684\u4f7f\u7528 &#8211; <a href=\"http:\/\/gdcsy.blog.163.com\/blog\/static\/12734360920130241521280\/\">http:\/\/gdcsy.blog.163.com\/blog\/static\/12734360920130241521280\/<\/a><\/li>\n<li>awk\u5185\u7f6e\u51fd\u6570\u7684\u4f7f\u7528(split\/substr\/length) &#8211; <a href=\"http:\/\/www.cnblogs.com\/sunada2005\/p\/3493941.html\">http:\/\/www.cnblogs.com\/sunada2005\/p\/3493941.html<\/a><\/li>\n<li>awk\u4e4bsubstr()\u4e0elength()\u7684\u914d\u5408\u8fd0\u7528- <a href=\"http:\/\/blog.chinaunix.net\/uid-10540984-id-325914.html\">http:\/\/blog.chinaunix.net\/uid-10540984-id-325914.html<\/a><\/li>\n<li><a href=\"http:\/\/cs.canisius.edu\/ONLINESTUFF\/PL_TUTORIALS\/AWK\/awk.examples\">http:\/\/cs.canisius.edu\/ONLINESTUFF\/PL_TUTORIALS\/AWK\/awk.examples<\/a><\/li>\n<li>The AWK Manual &#8211; <a href=\"http:\/\/www.staff.science.uu.nl\/~oostr102\/docs\/nawk\/nawk_92.html\">http:\/\/www.staff.science.uu.nl\/~oostr102\/docs\/nawk\/nawk_92.html<\/a><\/li>\n<li>Awk &#8211; A Tutorial <a href=\"http:\/\/www.grymoire.com\/Unix\/Awk.html#uh-40\">http:\/\/www.grymoire.com\/Unix\/Awk.html#uh-40<\/a><\/li>\n<li><a href=\"http:\/\/www.math.utah.edu\/docs\/info\/gawk_13.html\">http:\/\/www.math.utah.edu\/docs\/info\/gawk_13.html<\/a><\/li>\n<li><a href=\"https:\/\/www.gnu.org\/software\/gawk\/manual\/html_node\/index.html\">https:\/\/www.gnu.org\/software\/gawk\/manual\/html_node\/index.html<\/a><\/li>\n<li>=<\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/14840953\/how-to-remove-a-character-at-the-end-of-each-line-in-unix\">http:\/\/stackoverflow.com\/questions\/14840953\/how-to-remove-a-character-at-the-end-of-each-line-in-unix<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/12204192\/awk-multiple-delimiter\">http:\/\/stackoverflow.com\/questions\/12204192\/awk-multiple-delimiter<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/8009664\/split-string-to-array-using-awk\">http:\/\/stackoverflow.com\/questions\/8009664\/split-string-to-array-using-awk<\/a><\/li>\n<\/ul>\n<p>==<\/p>\n<h5>6.\u5982\u4f55\u7528sed\u4e00\u6b21\u8fdb\u884c\u591a\u4e2a\u66ff\u6362\u64cd\u4f5c\uff1f<\/h5>\n<ul>\n<li><a href=\"http:\/\/www.cyberciti.biz\/faq\/howto-sed-substitute-find-replace-multiple-patterns\/\">http:\/\/www.cyberciti.biz\/faq\/howto-sed-substitute-find-replace-multiple-patterns\/<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/10565877\/how-to-replace-two-things-at-once-with-sed\">http:\/\/stackoverflow.com\/questions\/10565877\/how-to-replace-two-things-at-once-with-sed<\/a><\/li>\n<\/ul>\n<pre class=\"lang:default decode:true\">sed -e 's\/dog\/monkey\/g' -e 's\/orange\/cow\/g'\nsed -e 's\/dog\/monkey\/g; s\/orange\/cow\/g'<\/pre>\n<p>==<\/p>\n<h5>7.\u5982\u4f55\u7528sed\u5bf9\u4e00\u4e2a\u591a\u884c\u5b57\u7b26\u4e32\u8fdb\u884c\u66ff\u6362\u64cd\u4f5c\uff1f<\/h5>\n<p><a href=\"http:\/\/unix.stackexchange.com\/questions\/26284\/how-can-i-use-sed-to-replace-a-multi-line-string\" target=\"_blank\">http:\/\/unix.stackexchange.com\/questions\/26284\/how-can-i-use-sed-to-replace-a-multi-line-string<\/a><\/p>\n<p>==<\/p>\n<h5>8.\u5982\u4f55\u7528sed\u5bf9\u6362\u884c\u7b26\u2019\\n\u2019\u8fdb\u884c\u66ff\u6362\uff1f<\/h5>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/1251999\/how-can-i-replace-a-newline-n-using-sed\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/1251999\/how-can-i-replace-a-newline-n-using-sed<\/a><\/p>\n<p>Use this solution with sed:<\/p>\n<pre class=\"lang:default decode:true\">sed ':a;N;$!ba;s\/\\n\/ \/g'<\/pre>\n<p>This will read the whole file in a loop, then replaces the newline(s) with a space.Explanation:<\/p>\n<ol>\n<li>create a label via :a<\/li>\n<li>append the current and next line to the pattern space via N<\/li>\n<li>if we are before the last line, branch to the created label $!ba ($! means not to do it on the last line (as there should be one final newline)).<\/li>\n<li>finally the substitution replaces every newline with a space on the pattern space (which is the whole file).<\/li>\n<\/ol>\n<p>==<\/p>\n<h5>9.Vim\u4e2d\u2019^M\u2019\u5b57\u7b26\u7684\u66ff\u6362\u64cd\u4f5c<\/h5>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/64749\/m-character-at-end-of-lines\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/64749\/m-character-at-end-of-lines<\/a><\/p>\n<h6>\u65b9\u6cd5\u4e00\uff1a<\/h6>\n<pre class=\"lang:default decode:true\">:set fileformat=unix\n:w<\/pre>\n<h6>\u65b9\u6cd5\u4e8c\uff08\u5e38\u7528\uff09\uff1a<\/h6>\n<pre class=\"lang:default decode:true\">:%s\/^M\/\/g<\/pre>\n<p>To get the ^M hold the CTRL key, press V then M (Both while holding the control key) and the ^M will appear. This will find all occurrences and replace them with nothing.\uff08<span style=\"color: #ff0000;\"><strong>\u5982\u4f55\u5f97\u5230\u4e0a\u9762\u7684&#8217;^M&#8217;\u5b57\u7b26\uff1a\u5728\u6309\u4f4fCtrl\u952e\u7684\u540c\u65f6\u6309\u4e0bV\u548cM\u952e\u5373\u53ef<\/strong><\/span>\uff09<\/p>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/71323\/how-to-replace-a-character-for-a-newline-in-vim\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/71323\/how-to-replace-a-character-for-a-newline-in-vim<\/a><\/p>\n<p>==<\/p>\n<h5>10.Shell\u7f16\u7a0b\u4e2d\u7684\u6570\u7ec4<\/h5>\n<h6>\u641c\u7d22\u5173\u952e\u5b57\uff1a<\/h6>\n<p>bash array<\/p>\n<h6>\u53c2\u8003\u5185\u5bb9\uff1a<\/h6>\n<pre class=\"lang:default decode:true\">#!\/bin\/bash\n\nusers=\"one two three\"\narr_s=(\"\/etc\/passwd\" \"\/etc\/group\" \"\/etc\/hosts\")\nlimits=( 10,20,26,39,48 )\n\nfor ix in ${users[@]}; do\n\techo $ix\ndone\n\nfor iy in ${arr_s[@]}; do\n\techo $iy\ndone\n\nfor iz in ${limits[@]}; do\n\techo $iz\ndone<\/pre>\n<h6>bash\u4e2d\u7684\u6570\u7ec4\/\u5faa\u73af<\/h6>\n<p>\u5982\u679c\u5e0c\u671b\u5728\u4e0a\u4e00\u4e2a\u5faa\u73af\u4e2d\u5f97\u5230\u7684\u7ed3\u679c\u53ef\u4ee5\u63d0\u4f9b\u7ed9\u540e\u9762\u7684\u529f\u80fd\u7528\uff0c\u6709\u4e24\u79cd\u65b9\u6cd5\uff1a<\/p>\n<h6>1.\u6570\u7ec4<\/h6>\n<p>\u7528declare -A\u58f0\u660e<\/p>\n<p>\u7528\u5706\u62ec\u53f7\u62ec\u8d77\u6765<\/p>\n<h6>2.\u4ee5\u7a7a\u683c\u8fde\u63a5\u7684\u5b57\u7b26\u4e32{\u672c\u8d28\u4e0a\u4e5f\u662f\u4e00\u79cd\u6570\u7ec4}<\/h6>\n<pre class=\"lang:default decode:true\">users=\"one two three\"\narr_s=(\"\/etc\/passwd\" \"\/etc\/group\" \"\/etc\/hosts\")\n\necho \"Total counts of array : ${#users[*]}\"\necho \"Total counts of array : ${#arr_s[*]}\"<\/pre>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/www.cyberciti.biz\/faq\/bash-for-loop-array\/\">http:\/\/www.cyberciti.biz\/faq\/bash-for-loop-array\/<\/a><\/li>\n<li><a href=\"http:\/\/www.cyberciti.biz\/faq\/bash-iterate-array\/\">http:\/\/www.cyberciti.biz\/faq\/bash-iterate-array\/<\/a><\/li>\n<li><a href=\"http:\/\/wiki.bash-hackers.org\/syntax\/arrays\">http:\/\/wiki.bash-hackers.org\/syntax\/arrays<\/a><\/li>\n<li><a href=\"http:\/\/www.tecmint.com\/working-with-arrays-in-linux-shell-scripting\/\">http:\/\/www.tecmint.com\/working-with-arrays-in-linux-shell-scripting\/<\/a><\/li>\n<li><a href=\"http:\/\/mywiki.wooledge.org\/BashGuide\/Arrays\">http:\/\/mywiki.wooledge.org\/BashGuide\/Arrays<\/a><\/li>\n<li><a href=\"http:\/\/www.thegeekstuff.com\/2010\/06\/bash-array-tutorial\/\">http:\/\/www.thegeekstuff.com\/2010\/06\/bash-array-tutorial\/<\/a><\/li>\n<li><a href=\"http:\/\/tldp.org\/LDP\/abs\/html\/arrays.html\">http:\/\/tldp.org\/LDP\/abs\/html\/arrays.html<\/a><\/li>\n<li><a href=\"http:\/\/tldp.org\/LDP\/Bash-Beginners-Guide\/html\/sect_10_02.html\">http:\/\/tldp.org\/LDP\/Bash-Beginners-Guide\/html\/sect_10_02.html<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/8880603\/loop-through-array-of-strings-in-bash-script\">http:\/\/stackoverflow.com\/questions\/8880603\/loop-through-array-of-strings-in-bash-script<\/a><\/li>\n<\/ul>\n<p>==<\/p>\n<h5>11.\u5982\u4f55\u5728Linux\u4e0b\u521b\u5efa\u4e00\u4e2a\u201c\u4e0d\u53ef\u53d8\u66f4\u7684\u201d\u6587\u4ef6<\/h5>\n<p>\u53ef\u4ee5\u4f7f\u7528chattr\u548clsattr\u547d\u4ee4\u6765\u7ba1\u7406\u989d\u5916\u7684\u6587\u4ef6\u6807\u5fd7\u4f4d\uff0c\u6765\u907f\u514d\u6587\u4ef6\u88ab\u7be1\u6539\uff08\u610f\u5916\u6216\u8005\u5176\u4ed6\u60c5\u51b5\uff09\u3002\u4f46\u8981\u6ce8\u610f\u4f60\u4e0d\u80fd\u5c06chattr\u4f5c\u4e3a\u4e00\u4e2a\u5b89\u5168\u63aa\u65bd\uff0c\u56e0\u4e3a\u201c\u4e0d\u53ef\u53d8\u66f4\u201d\u6807\u5fd7\u4f4d\u53ef\u4ee5\u5f88\u5bb9\u6613\u88ab\u53d6\u6d88\u6389\u3002\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u4e00\u4e2a\u53ef\u80fd\u7684\u65b9\u5f0f\u662f\u9650\u5236chattr\u547d\u4ee4\u81ea\u8eab\u7684\u53ef\u7528\u6027\uff0c\u6216\u8005\u53bb\u6389CAP_LINUX_IMMUTABLE\u5185\u6838\u529f\u80fd\u3002\u5173\u4e8echattr\u4ee5\u53ca\u53ef\u7528\u7684\u6807\u5fd7\u4f4d\u7684\u66f4\u591a\u7ec6\u8282\uff0c\u8bf7\u53c2\u8003\u5b83\u7684man\u624b\u518c\u3002<\/p>\n<h6>\u641c\u7d22\u5173\u952e\u5b57\uff1a<\/h6>\n<p>Linux how to limit a command&#8217;s execute user<\/p>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"https:\/\/linux.cn\/article-5467-qqmail.html\" target=\"_blank\">\u5982\u4f55\u5728Linux\u4e0b\u521b\u5efa\u4e00\u4e2a\u4e0d\u53ef\u53d8\u66f4\u7684\u6587\u4ef6<\/a><\/li>\n<li><a href=\"http:\/\/serverfault.com\/questions\/569491\/limit-exec-commands-for-linux-users\" target=\"_blank\">http:\/\/serverfault.com\/questions\/569491\/limit-exec-commands-for-linux-users<\/a><\/li>\n<li><a href=\"http:\/\/serverfault.com\/questions\/223324\/how-do-i-limit-the-users-a-specific-user-can-run-commands-in-linux\">http:\/\/serverfault.com\/questions\/223324\/how-do-i-limit-the-users-a-specific-user-can-run-commands-in-linux<\/a><\/li>\n<li><a href=\"http:\/\/unix.stackexchange.com\/questions\/90998\/block-particular-command-in-linux-for-specific-user\">http:\/\/unix.stackexchange.com\/questions\/90998\/block-particular-command-in-linux-for-specific-user<\/a><\/li>\n<li><a href=\"http:\/\/superuser.com\/questions\/767415\/limit-user-to-execute-selective-commands-linux\">http:\/\/superuser.com\/questions\/767415\/limit-user-to-execute-selective-commands-linux<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/21498667\/how-to-limit-user-commands-in-linux\">http:\/\/stackoverflow.com\/questions\/21498667\/how-to-limit-user-commands-in-linux<\/a><\/li>\n<li><a href=\"http:\/\/www.cyberciti.biz\/faq\/protect-command-by-configuring-linux-unix-group-permissions\/\">http:\/\/www.cyberciti.biz\/faq\/protect-command-by-configuring-linux-unix-group-permissions\/<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u505a\u7684\u4e8b\u60c5\u90fd\u6bd4\u8f83\u96f6\u6563\uff0c\u4e0d\u592a\u7cfb\u7edf\uff0c\u5982\u679c\u4e0d\u53ca\u65f6\u8bb0\u5f55\u4e0b\u6765\u7684\u8bdd\uff0c\u518d\u9694\u4e00\u6bb5\u65f6\u95f4\u53bb\u770b\u7684\u8bdd\u5c31\u5bb9\u6613\u5fd8\u8bb0\u4e4b\u524d\u5904\u7406\/\u89e3\u51b3\u95ee\u9898\u65f6\u7528 [&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,7],"tags":[74,490,78,90],"class_list":["post-2080","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-linux","category-programing","tag-awk","tag-chattr","tag-sed","tag-xargs"],"views":4939,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2080","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=2080"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2080\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=2080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=2080"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=2080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}