奇怪的 zsh grep -P 行为

奇怪的 zsh grep -P 行为

我从 Windows 使用 PuTTY 连接到服务器。

我有很长一段时间没有接触过(我想)本地管理配置:

$ cat /opt/jira/.subversion/config
...    
### must be enabled, which is typically done by setting the
### 'enable-auto-props' option.
# *.c = svn:eol-style=native
# *.cpp = svn:eol-style=native
...

想看看是否有以 not 开头的行#

$ cat /opt/jira/.subversion/config | grep -P "$[^#]"
zsh: bad math expression: operand expected at `^#'
$
[1]    broken pipe  cat /opt/jira/.subversion/config

$ cat /opt/jira/.subversion/config | grep -P "\$[^#]"

$ cat /opt/jira/.subversion/config | grep -P "\$#"

$ cat /opt/jira/.subversion/config | grep -P "$#"
### details, see Debian Bug #413102):
# global-ignores = *.o *.lo *.la *.al .libs *.so *.so.[0-9]* *.a *.pyc *.pyo

谁能解释为什么第二个和第三个输出相同为空,第四个输出是什么?

UPD:这里的文件是:

### This file configures various client-side behaviors.
###
### The commented-out examples below are intended to demonstrate
### how to use this file.

### Section for authentication and authorization customizations.
[auth]
### Set store-passwords to 'no' to avoid storing passwords in the
### auth/ area of your config directory.  It defaults to 'yes'.
### Note that this option only prevents saving of *new* passwords;
### it doesn't invalidate existing passwords.  (To do that, remove
### the cache files by hand as described in the Subversion book.)
# store-passwords = no
### Set store-auth-creds to 'no' to avoid storing any subversion
### credentials in the auth/ area of your config directory.
### It defaults to 'yes'.  Note that this option only prevents
### saving of *new* credentials;  it doesn't invalidate existing
### caches.  (To do that, remove the cache files by hand.)
# store-auth-creds = no

### Section for configuring external helper applications.
[helpers]
### Set editor to the command used to invoke your text editor.
###   This will override the environment variables that Subversion
###   examines by default to find this information ($EDITOR,
###   et al).
# editor-cmd = editor (vi, emacs, notepad, etc.)
### Set diff-cmd to the absolute path of your 'diff' program.
###   This will override the compile-time default, which is to use
###   Subversion's internal diff implementation.
# diff-cmd = diff_program (diff, gdiff, etc.)
### Set diff3-cmd to the absolute path of your 'diff3' program.
###   This will override the compile-time default, which is to use
###   Subversion's internal diff3 implementation.
# diff3-cmd = diff3_program (diff3, gdiff3, etc.)
### Set diff3-has-program-arg to 'true' or 'yes' if your 'diff3'
###   program accepts the '--diff-program' option.
# diff3-has-program-arg = [true | false]
### Set merge-tool-cmd to the command used to invoke your external
### merging tool of choice. Subversion will pass 4 arguments to
### the specified command: base theirs mine merged
# merge-tool-cmd = merge_command

### Section for configuring tunnel agents.
[tunnels]
### Configure svn protocol tunnel schemes here.  By default, only
### the 'ssh' scheme is defined.  You can define other schemes to
### be used with 'svn+scheme://hostname/path' URLs.  A scheme
### definition is simply a command, optionally prefixed by an
### environment variable name which can override the command if it
### is defined.  The command (or environment variable) may contain
### arguments, using standard shell quoting for arguments with
### spaces.  The command will be invoked as:
###   <command> <hostname> svnserve -t
### (If the URL includes a username, then the hostname will be
### passed to the tunnel agent as <user>@<hostname>.)  Here we
### redefine the built-in 'ssh' scheme to avoid an unfortunate
### interaction with the "ControlMaster auto" feature (for
### details, see Debian Bug #413102):
ssh = $SVN_SSH ssh -o ControlMaster=no
### If you wanted to define a new 'rsh' scheme, to be used with
### 'svn+rsh:' URLs, you could do so as follows:
# rsh = rsh
### Or, if you wanted to specify a full path and arguments:
# rsh = /path/to/rsh -l myusername
### On Windows, if you are specifying a full path to a command,
### use a forward slash (/) or a paired backslash (\\) as the
### path separator.  A single backslash will be treated as an
### escape for the following character.

### Section for configuring miscelleneous Subversion options.
[miscellany]
### Set global-ignores to a set of whitespace-delimited globs
### which Subversion will ignore in its 'status' output, and
### while importing or adding files and directories.
### '*' matches leading dots, e.g. '*.rej' matches '.foo.rej'.
# global-ignores = *.o *.lo *.la *.al .libs *.so *.so.[0-9]* *.a *.pyc *.pyo
#   *.rej *~ #*# .#* .*.swp .DS_Store
### Set log-encoding to the default encoding for log messages
# log-encoding = latin1
### Set use-commit-times to make checkout/update/switch/revert
### put last-committed timestamps on every file touched.
# use-commit-times = yes
### Set no-unlock to prevent 'svn commit' from automatically
### releasing locks on files.
# no-unlock = yes
### Set mime-types-file to a MIME type registry file, used to
### provide hints to Subversion's MIME type auto-detection
### algorithm.
# mime-types-file = /path/to/mime.types
### Set preserved-conflict-file-exts to a whitespace-delimited
### list of patterns matching file extensions which should be
### preserved in generated conflict file names.  By default,
### conflict files use custom extensions.
# preserved-conflict-file-exts = doc ppt xls od?
### Set enable-auto-props to 'yes' to enable automatic properties
### for 'svn add' and 'svn import', it defaults to 'no'.
### Automatic properties are defined in the section 'auto-props'.
# enable-auto-props = yes
### Set interactive-conflicts to 'no' to disable interactive
### conflict resolution prompting.  It defaults to 'yes'.
# interactive-conflicts = no

### Section for configuring automatic properties.
[auto-props]
### The format of the entries is:
###   file-name-pattern = propname[=value][;propname[=value]...]
### The file-name-pattern can contain wildcards (such as '*' and
### '?').  All entries which match (case-insensitively) will be
### applied to the file.  Note that auto-props functionality
### must be enabled, which is typically done by setting the
### 'enable-auto-props' option.
# *.c = svn:eol-style=native
# *.cpp = svn:eol-style=native
# *.h = svn:eol-style=native
# *.dsp = svn:eol-style=CRLF
# *.dsw = svn:eol-style=CRLF
# *.sh = svn:eol-style=native;svn:executable
# *.txt = svn:eol-style=native
# *.png = svn:mime-type=image/png
# *.jpg = svn:mime-type=image/jpeg
# Makefile = svn:eol-style=native

答案1

在双引号内,字符$"\`仍然是特殊的。您可能会将它们与单引号混淆:在单引号内,所有字符都按字面意思解释,除了'它本身结束字符串文字。

$ cat /opt/jira/.subversion/config | grep -P "$[^#]"
zsh: bad math expression: operand expected at `^#'

$[…]是算术表达式的已弃用语法,可以$((…))像在 POSIX shell 中一样编写。^#不是有效的算术表达式; shell 需要一个操作数,例如数字或变量名。

[1]    broken pipe  cat /opt/jira/.subversion/config

由于管道中的第二个命令在消耗所有输出之前中止cat(它甚至还没有开始读取),因此第一个命令 ( cat) 收到信号管道

$ cat /opt/jira/.subversion/config | grep -P "\$[^#]"
$ cat /opt/jira/.subversion/config | grep -P "\$#"

反斜杠告诉 shell 按字面意思解释下一个字符,因此grep看到了模式$[^#]$#。这些模式分别表示“行尾后跟除任何字符之外的字符#”和“行尾后跟”。#这些模式都无法匹配任何内容。

$ cat /opt/jira/.subversion/config | grep -P "$#"

$#是位置参数的数量($1, $2, ...,统称为$@),即在 shell 命令行上传递的参数,或者如果在函数内部则传递给函数的参数。在交互式 shell 中,通常没有位置参数,因此grep看到了模式0

您正在寻找的模式是^[^#](^匹配行的开头)。除非您打算在模式中包含 shell 变量的值或命令的输出,否则请使用单引号(这里并不重要,但对于某些模式很重要,尤其是包含反斜杠或 的模式$)。您不需要,因为此模式在基本正则表达式 (plain )、扩展正则表达式 ( ) 和 perl 正则表达式 ( )-P中以相同的方式编写。grepgrep -Egrep -P

$ </opt/jira/.subversion/config grep '^[^#]'

答案2

我无法确切地告诉您在每种情况下得到的输出会发生什么。但是,您所做的事情存在两个问题:

  1. -P标志(用于与 Perl 兼容的正则表达式)被认为是实验性的,并且对于您想要执行的操作来说不是必需的。

  2. $是正则表达式中的行尾字符。您应该使用行开头字符^


cat /opt/jira/.subversion/config | grep "^[^#]"

或者更简洁地说,

grep "^[^#]" /opt/jira/.subverison/config

应该做你想做的事。

相关内容