{"id":1625,"date":"2014-11-30T04:28:33","date_gmt":"2014-11-30T04:28:33","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=1625"},"modified":"2014-11-30T04:28:33","modified_gmt":"2014-11-30T04:28:33","slug":"bash%e7%9a%84shopt%e5%91%bd%e4%bb%a4%e5%ad%a6%e4%b9%a0","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/1625.html","title":{"rendered":"Bash\u7684shopt\u547d\u4ee4\u5b66\u4e60"},"content":{"rendered":"<p>\u4e4b\u524d\u7a0d\u5fae\u4e86\u89e3\u8fc7shopt\u547d\u4ee4\uff0c\u4f46\u7528\u7684\u8f83\u5c11\uff0c\u6240\u4ee5\u5370\u8c61\u4e0d\u6df1\uff0c\u8fd9\u6b21\u65e0\u610f\u4e2d\u78b0\u5230\u4e86\uff0c\u521a\u597d\u5b66\u4e60\u5b66\u4e60\u3002<\/p>\n<h6>\u600e\u6837\u904d\u5386\u79fb\u9664\u9879\u76ee\u4e2d\u7684\u6240\u6709 .pyc \u6587\u4ef6 \uff1f<\/h6>\n<p>\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662flinux\u6216\u8005mac\uff0c\u5728\u7ec8\u7aef\u6267\u884c\u8fd9\u6837\u7684\u64cd\u4f5c\uff1a<\/p>\n<pre class=\"lang:default decode:true\">find \/tmp -name \"*.pyc\" | xargs rm -rf<\/pre>\n<p>\u5982\u679c\u63d0\u793aPermission denied\uff0c\u5219\u4f7f\u7528\uff1a<\/p>\n<pre class=\"lang:default decode:true\">sudo find \/tmp -name \"*.pyc\" | xargs rm -rf<\/pre>\n<p>\u5c06\u4e0a\u9762\u7684\/tmp\u66ff\u6362\u6210\u5de5\u7a0b\u76ee\u5f55\u5373\u53ef\u3002\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u904d\u5386\u5220\u9664\u5de5\u7a0b\u76ee\u5f55\uff08\u542b\u5b50\u76ee\u5f55\uff09\u4e0b\u7684pyc\u6587\u4ef6\u3002<\/p>\n<p>==<\/p>\n<pre class=\"lang:default decode:true\">rm **\/*.pyc<\/pre>\n<p>\u8865\u5145\u8bf4\u660e\uff1a\u5728bash\u4e0b\u8981\u5728~\/.bashrc\u4e2d\u6dfb\u52a0shopt -s globstar<\/p>\n<p>==<\/p>\n<p>\u5728\u6253\u5305\u65f6\u5ffd\u7565 .pyc \u6587\u4ef6\u6216\u8bb8\u662f\u4e2a\u66f4\u65b9\u4fbf\u7684\u529e\u6cd5\u3002<br \/>\ntar\u548czip\u90fd\u53ef\u4ee5\u52a0\u4e0a &#8211;exclude=*.pyc \u53c2\u6570\u6765\u6392\u9664 pyc \u6587\u4ef6<\/p>\n<p>===<\/p>\n<pre class=\"lang:default decode:true\">find -iname \"*.pyc\" -exec rm -f {} ;<\/pre>\n<p>===<\/p>\n<pre class=\"lang:default decode:true\">find . -type f -name \"*.py[co]\" -delete<\/pre>\n<p>===<\/p>\n<p>\u4e0a\u9762\u7684\u51e0\u4e2afind\u547d\u4ee4\u7684\u793a\u4f8b\u5176\u5b9e\u6211\u90fd\u77e5\u9053\uff0c\u8fd9\u91cc\u611f\u5174\u8da3\u7684\u4e3b\u8981\u5728\u4e8e\uff1a<br \/>\nrm **\/*.pyc<br \/>\n\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u8865\u5145\u8bf4\u660e\u6709\u201c\u5728bash\u4e0b\u8981\u5728~\/.bashrc\u4e2d\u6dfb\u52a0shopt -s globstar\u201d<\/p>\n<p>\u521a\u597d\u524d\u6bb5\u65f6\u95f4\u770b\u5230\u8fc7\u4e00\u7bc7\u6587\u7ae0\uff1a<\/p>\n<p>\u9700\u6c42\uff1a<br \/>\n\u4e00\u4e2a\u65e5\u5fd7\u76ee\u5f55\uff0c\u91cc\u5934\u5b58\u7740\u5f88\u591a 00, 01, 02 &#8230; 31 \u7684\u65e5\u671f\u76ee\u5f55\uff0c\u5982\u4f55\u5217\u793a\u51fa00\u523012\u53f7\u4e4b\u95f4\u7684\u6240\u6709\u76ee\u5f55\uff1f\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u4f55\u5728\u6587\u4ef6\u540d\u4e0a\u9762\uff0c\u4f7f\u7528\u6b63\u5219\uff1f<\/p>\n<p>\u89e3\u6cd5\uff1a<\/p>\n<pre class=\"lang:default decode:true\"># shopt -s extglob\n# ls -al +(0[0-9]|1[0-3])<\/pre>\n<p>\u8bf4\u660e\uff1a<br \/>\n\u00d8 \u5e73\u65f6shell\u7684\u901a\u914d\u7b26\uff0c\u53ea\u662f\u901a\u914d\u8bed\u4e49\uff0c\u4e0d\u662f\u6b63\u5219\u8bed\u4e49<br \/>\n\u00d8 \u52a0\u4e0a\u8fd9\u4e2a extglob \u4e4b\u540e\uff0c\u624d\u80fd\u662f\u6b63\u5219\u8bed\u4e49<br \/>\n\u00d8 \u8bed\u6cd5\u683c\u5f0f\u662f +(\u6b63\u5219)<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p>\u53bbUbuntu\u4e0a\u770b\u770b\uff08Bash\u7684\u7248\u672c\u4e3a\uff1aGNU bash, version 4.3.11(1)-release (i686-pc-linux-gnu)\uff09\uff1a<\/p>\n<pre class=\"lang:default decode:true\"># shopt -s\nautocd                   execfail                 lastpipe\ncdable_vars              expand_aliases           lithist\ncdspell                  extdebug                 login_shell\ncheckhash                extglob                  mailwarn\ncheckjobs                extquote                 nocaseglob\ncheckwinsize             failglob                 nocasematch\ncmdhist                  force_fignore            no_empty_cmd_completion\ncompat31                 globasciiranges          nullglob\ncompat32                 globstar                 progcomp\ncompat40                 gnu_errfmt               promptvars\ncompat41                 histappend               restricted_shell\ncompat42                 histreedit               shift_verbose\ncomplete_fullquote       histverify               sourcepath\ndirexpand                hostcomplete             xpg_echo\ndirspell                 huponexit\ndotglob                  interactive_comments<\/pre>\n<h6>\u641c\u7d22<span style=\"color: #ff0000;\">\u5173\u952e\u5b57<\/span>\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/search.aol.com\/aol\/search?q=shell+extglob\" target=\"_blank\">http:\/\/search.aol.com\/aol\/search?q=shell+extglob<\/a><\/li>\n<li><a href=\"http:\/\/search.aol.com\/aol\/search?q=shell+globstar\" target=\"_blank\">http:\/\/search.aol.com\/aol\/search?q=shell+globstar<\/a><\/li>\n<\/ul>\n<h6>\u627e\u5230\u7684\u6709\u6548\u94fe\u63a5{<span style=\"color: #ff0000;\">extglob<\/span>}\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/unix.stackexchange.com\/questions\/23583\/what-does-the-syntax-on-this-shell-command-with-extglob-mean\" target=\"_blank\">scripting &#8211; What does the syntax on this shell command with extglob mean? &#8211; Unix &amp; Linux Stack Exchange<\/a><\/li>\n<li><a href=\"http:\/\/wiki.bash-hackers.org\/syntax\/pattern\" target=\"_blank\">Patterns and pattern matching [Bash Hackers Wiki]<\/a><\/li>\n<li><a href=\"http:\/\/mywiki.wooledge.org\/glob\" target=\"_blank\">glob &#8211; Greg&#8217;s Wiki<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/216995\/how-can-i-use-inverse-or-negative-wildcards-when-pattern-matching-in-a-unix-linu\" target=\"_blank\">bash &#8211; How can I use inverse or negative wildcards when pattern matching in a unix\/linux shell? &#8211; Stack Overflow<\/a><\/li>\n<\/ul>\n<p>GNU\u5b98\u65b9\u8bf4\u660e\uff1a<a href=\"http:\/\/www.gnu.org\/software\/bash\/manual\/html_node\/The-Shopt-Builtin.html\" target=\"_blank\">Bash Reference Manual: The Shopt Builtin<\/a><\/p>\n<h6>\u53e6\u4e00\u4e2ashopt\u7684\u9009\u9879{<span style=\"color: #ff0000;\">globstar<\/span>}\uff1a<\/h6>\n<p><a href=\"http:\/\/shellrunner.com\/better-simpler-searching-and-scripting-with-bash-globstar\/\" target=\"_blank\">better, simpler searching and scripting with bash globstar \u00ab ShellRunner<\/a><\/p>\n<p>Globstar is a feature not typically enabled, but present in bash 4+, and it allows you to do more easily select files in bash, using a double star **.<\/p>\n<p>For example, if you want to every .text file, in all subdirectories, you no longer even need to use the find command.<\/p>\n<pre class=\"lang:default decode:true\">ls **\/*.text<\/pre>\n<p>The ** here will traverse any number of directories, not just the current directory. Here\u2019s another very useful, easy to remember example. Ever want to find a line of text somewhere in a huge pile of files, but you know some part of the file name? You can do a recursive grep to easily find it, without some abstruse find command.<\/p>\n<pre class=\"lang:default decode:true\">grep -r needle **\/*haystack*<\/pre>\n<p>To begin using globstar, you need to enable it. It should be the default, if you ask me, but enabling is simple. From the terminal, type:<\/p>\n<pre class=\"lang:default decode:true\">shopt -s globstar<\/pre>\n<p>You\u2019ll probably want to add that line to your ~\/.bashrc file, too, so it\u2019s enabled every time you open a terminal or login.<\/p>\n<p>\u5728\u6211\u7684VPS\u4e0aglobstar\u9ed8\u8ba4\u662f\u5173\u95ed\u7684\uff1a<\/p>\n<pre class=\"lang:default decode:true\"># shopt globstar\nglobstar off<\/pre>\n<p>\u800c\uff0cextglob\u9ed8\u8ba4\u5374\u662f\u5f00\u542f\u7684\uff1a<\/p>\n<pre class=\"lang:default decode:true\"># shopt extglob\nextglob on<\/pre>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/793715\/unable-to-enable-globstar-in-bash-4\" target=\"_blank\">bash4 &#8211; Unable to enable globstar in Bash 4 &#8211; Stack Overflow<\/a><\/li>\n<li><a href=\"http:\/\/www.linuxjournal.com\/content\/globstar-new-bash-globbing-option\" target=\"_blank\">Globstar: New Bash Globbing Option | Linux Journal<\/a><\/li>\n<\/ul>\n<p>\u5728Stackoverflow\u4e0a\u6709\u4e00\u4e2a\u95ee\u9898\u5bf9\u6211\u800c\u8a00\u5f88\u6709\u542f\u53d1\u610f\u4e49\uff1a<br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/17191622\/why-would-i-not-leave-extglob-enabled-in-bash\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/17191622\/why-would-i-not-leave-extglob-enabled-in-bash<\/a><\/p>\n<p>\u63d0\u95ee\u8005\u7ed9\u51fa\u4e86\u4e00\u4e2a\u601d\u8003\uff1a<\/p>\n<blockquote><p>All the answers that used &#8216;shopt -s extglob&#8217; also mentioned &#8216;shopt -u extglob&#8217; to turn it off. Why would I want to turn something so useful off? Indeed why isn&#8217;t it on by default? Presumably it has the potential for giving some nasty surprises. What are they?<br \/>\n\u6240\u6709\u7684\u7b54\u6848\u90fd\u544a\u8bc9\u6211\u4f7f\u7528\u201cshopt -s extglob\u201d\u6253\u5f00extglob\uff0c\u4f7f\u7528\u201cshopt -u extglob\u201d\u5173\u6389extglob\uff0c\u4f46\u662f\u4e3a\u4ec0\u4e48\u6211\u8fd8\u662f\u60f3\u5c06\u8fd9\u4e48\u6709\u7528\u7684\u4e00\u4e2a\u9009\u9879\u5173\u6389\u5462\uff1f\u4e3a\u4ec0\u4e48\u8fd9\u4e48\u6709\u7528\u7684\u9009\u9879\u5728\u9ed8\u8ba4\u72b6\u6001\u4e0b\u6ca1\u6709\u88ab\u5f00\u542f\uff1f\u96be\u9053\u662f\u56e0\u4e3a\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e00\u4e9b\u7cdf\u7cd5\u7684\u610f\u5916\uff1f\u5982\u679c\u662f\u7684\u8bdd\uff0c\u4f1a\u5bfc\u81f4\u54ea\u4e9b\u95ee\u9898\u5462\uff1f<\/p><\/blockquote>\n<p>&#8212;<br \/>\n\u56de\u7b54\u7b80\u6d01\u660e\u4e86\uff1a<\/p>\n<blockquote><p>No nasty surprises &#8212; default-off behavior is only there for compatibility with traditional, standards-compliant pattern syntax.<br \/>\n\u9ed8\u8ba4\u72b6\u6001\u4e0b\u6ca1\u6709\u5f00\u542f\u4e0d\u4ee3\u8868\u8fd9\u662f\u4e0d\u597d\u7684\u7279\u6027\uff0c\u4ec5\u4ec5\u53ea\u662f\u517c\u5bb9\u6027\u7684\u9700\u8981\u800c\u5df2\u3002<\/p><\/blockquote>\n<p>\u67e5\u770bshopt\u7684\u9009\u9879\u90fd\u662f\u4e9b\u4ec0\u4e48\u72b6\u6001\uff0c\u76f4\u63a5\u8f93\u5165\uff1ashopt\u7136\u540e\u56de\u8f66\u5373\u53ef<\/p>\n<pre class=\"lang:default decode:true \"># shopt\nautocd          off\ncdable_vars     off\ncdspell         off\ncheckhash       off\ncheckjobs       off\ncheckwinsize    on\ncmdhist         on\ncompat31        off\ncompat32        off\ncompat40        off\ncompat41        off\ncompat42        off\ncomplete_fullquote      on\ndirexpand       off\ndirspell        off\ndotglob         off\nexecfail        off\nexpand_aliases  on\nextdebug        off\nextglob         on\nextquote        on\nfailglob        off\nforce_fignore   on\nglobstar        off\nglobasciiranges off\ngnu_errfmt      off\nhistappend      on\nhistreedit      off\nhistverify      off\nhostcomplete    off\nhuponexit       off\ninteractive_comments    on\nlastpipe        off\nlithist         off\nlogin_shell     on\nmailwarn        off\nno_empty_cmd_completion off\nnocaseglob      off\nnocasematch     off\nnullglob        off\nprogcomp        on\npromptvars      on\nrestricted_shell        off\nshift_verbose   off\nsourcepath      on\nxpg_echo        off<\/pre>\n<p>&nbsp;<\/p>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<p><a href=\"http:\/\/segmentfault.com\/q\/1010000002179781\" target=\"_blank\">\u600e\u6837\u904d\u5386\u79fb\u9664\u9879\u76ee\u4e2d\u7684\u6240\u6709 .pyc \u6587\u4ef6<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e4b\u524d\u7a0d\u5fae\u4e86\u89e3\u8fc7shopt\u547d\u4ee4\uff0c\u4f46\u7528\u7684\u8f83\u5c11\uff0c\u6240\u4ee5\u5370\u8c61\u4e0d\u6df1\uff0c\u8fd9\u6b21\u65e0\u610f\u4e2d\u78b0\u5230\u4e86\uff0c\u521a\u597d\u5b66\u4e60\u5b66\u4e60\u3002 \u600e\u6837\u904d\u5386\u79fb\u9664\u9879\u76ee\u4e2d\u7684 [&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],"tags":[398,399,400],"class_list":["post-1625","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-linux","tag-extglob","tag-globstar","tag-shopt"],"views":3179,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1625","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=1625"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1625\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=1625"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=1625"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=1625"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}