bash如何在extglob中内部实现反模式匹配?

huangapple go评论88阅读模式
英文:

How bash inverse pattern matching works internally in extglob?

问题

  1. 当前目录中存在以下文件。
  2. ```plaintext
  3. -rw-r--r-- 1 kazama kazama 0 Feb 16 08:50 london_july_2001_001.jpeg
  4. -rw-r--r-- 1 kazama kazama 0 Feb 16 08:50 london_march_2004_002.png
  5. -rw-r--r-- 1 kazama kazama 0 Feb 16 08:50 newyork_dec_2012_005.jpeg
  6. -rw-r--r-- 1 kazama kazama 0 Feb 16 08:50 paris_sept_2007_003.jpg

我想过滤掉除以"paris"开头的所有图像。

我尝试了下面的命令,符合我的期望。

  1. ls -l !(paris*)

但是,我不明白为什么以下两个命令不能给我预期的输出。在这两种情况下,它都显示了所有4个文件。

ls -l !(paris)*.*(jpeg|jpg|png)

ls -l !(paris)*

Bash如何在内部解释这种扩展的通配符语法?它首先尝试匹配!(paris),然后尝试匹配(jpeg|jpg|png)吗?

  1. <details>
  2. <summary>英文:</summary>
  3. Below files are present in current directory.

-rw-r--r-- 1 kazama kazama 0 Feb 16 08:50 london_july_2001_001.jpeg
-rw-r--r-- 1 kazama kazama 0 Feb 16 08:50 london_march_2004_002.png
-rw-r--r-- 1 kazama kazama 0 Feb 16 08:50 newyork_dec_2012_005.jpeg
-rw-r--r-- 1 kazama kazama 0 Feb 16 08:50 paris_sept_2007_003.jpg

  1. I want to filter all images except which starts with &quot;paris&quot; text.
  2. I have tried below command which works as per my expectation.

ls -l !(paris*)

  1. But, I do not understand why below 2 commands do not give me expected output. In both of the cases it shows all the 4 files.
  2. `ls -l !(paris)*.*(jpeg|jpg|png)`
  3. `ls -l !(paris)*`
  4. How bash interprets this extended globbing syntax internally ?Is it first trying to match !(paris) then it tries to match (jpeg|jpg|png) ?
  5. </details>
  6. # 答案1
  7. **得分**: 1
  8. `!(paris)` 匹配除了 `paris` 之外的任何内容,包括 `paris_`, `pari`, `par`, `pa`, `p`,甚至是空字符串。Bash 会找到与 `paris` 不匹配的部分,然后尝试将路径名的其余部分与模式的其余部分匹配。请参考以下示例:
  9. ``` none
  10. $ echo !(paris)london*
  11. london_july_2001_001.jpeg london_march_2004_002.png
  12. $ echo !(paris)_*
  13. london_july_2001_001.jpeg london_march_2004_002.png newyork_dec_2012_005.jpeg paris_sept_2007_003.jpg
  14. $ echo !(paris)_*_*_*
  15. london_july_2001_001.jpeg london_march_2004_002.png newyork_dec_2012_005.jpeg
英文:

!(paris) matches anything but paris, which includes paris_, pari, par, pa, p, and even the empty string. Bash will find something that doesn't match paris and try to match the rest of the pathname against the rest of the pattern. See:

  1. $ echo !(paris)london*
  2. london_july_2001_001.jpeg london_march_2004_002.png
  3. $ echo !(paris)_*
  4. london_july_2001_001.jpeg london_march_2004_002.png newyork_dec_2012_005.jpeg paris_sept_2007_003.jpg
  5. $ echo !(paris)_*_*_*
  6. london_july_2001_001.jpeg london_march_2004_002.png newyork_dec_2012_005.jpeg

huangapple
  • 本文由 发表于 2023年2月16日 17:19:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75470089.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定