如何在逃离前报pressions扩展路径扩展模式? [英] How to escape extended pathname expansion patterns in quoted expressions?

查看:96
本文介绍了如何在逃离前报pressions扩展路径扩展模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在除了基本的 * [...] 模式,Bash shell中提供了扩展的模式匹配运营商如!(模式列表)(匹配所有除定的模式之一)。在 extglob shell选项需要被设置为使用它们。举个例子:

 〜$的mkdir测试; CD试验;触摸文件1文件2文件3
〜/测试$回声*
文件1文件2文件3
〜/测试$禁用了javascript -s extglob#确保extglob设置
〜/测试$回声!(文件2)
文件1文件3

如果我通过外壳前pression到执行它在一个子shell程序,操作导致错误。下面是直接运行一个子shell测试(我在这里从另一个目录执行,以确保扩展不会发生prematurely):

 〜/测试$ CD ..
〜$的bash -cCD测试;回声*
文件1文件2文件3
!CD测试;回声(文件2)〜$ bash的-c#预期的输出:文件1文件3
庆典:-c:0行:语法错误附近意外的标记'('
庆典:-c:0行:`CD试验;回音!(文件2)

我试过各种逃避的,但没有我来了已经工作正常。我也怀疑 extglob 不在一个子shell设置,但事实并非如此:

 〜$的bash -c禁用了javascript -s extglob; CD测试;!回声(文件2)
庆典:-c:0行:语法错误附近意外的标记'('
庆典:-c:0行:`CD试验;回音!(文件2)

任何解决方案AP preciated!


解决方案


$庆典-O extglob -c'回声!(文件2)
文件1文件3

In addition to the basic *, ? and [...] patterns, the Bash shell provides extended pattern matching operators like !(pattern-list) ("match all except one of the given patterns"). The extglob shell option needs to be set to use them. An example:

~$ mkdir test ; cd test ; touch file1 file2 file3
~/test$ echo *
file1 file2 file3
~/test$ shopt -s extglob  # make sure extglob is set
~/test$ echo !(file2)
file1 file3

If I pass a shell expression to a program which executes it in a sub-shell, the operator causes an error. Here's a test which runs a sub-shell directly (here I'm executing from another directory to make sure expansion doesn't happen prematurely):

~/test$ cd ..
~$ bash -c "cd test ; echo *"
file1 file2 file3
~$ bash -c "cd test ; echo !(file2)"  # expected output: file1 file3
bash: -c: line 0: syntax error near unexpected token `('
bash: -c: line 0: `cd test ; echo !(file2)'

I've tried all kinds of escaping, but nothing I've come up with has worked correctly. I also suspected extglob is not set in a sub-shell, but that's not the case:

~$ bash -c "shopt -s extglob ; cd test ; echo !(file2)"
bash: -c: line 0: syntax error near unexpected token `('
bash: -c: line 0: `cd test ; echo !(file2)'

Any solution appreciated!

解决方案

$ bash -O extglob -c 'echo !(file2)'
file1 file3

这篇关于如何在逃离前报pressions扩展路径扩展模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆