加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash – 如何在引用的表达式中转义扩展路径名扩展模式?

发布时间:2020-12-15 22:16:05 所属栏目:安全 来源:网络整理
导读:除了基本*,? […]模式,Bash shell提供扩展的模式匹配运算符,如!(pattern-list)(“匹配除了给定模式之外的所有模式”).需要将extglob shell选项设置为使用它们.一个例子: ~$mkdir test ; cd test ; touch file1 file2 file3~/test$echo *file1 file2 file3
除了基本*,? […]模式,Bash shell提供扩展的模式匹配运算符,如!(pattern-list)(“匹配除了给定模式之外的所有模式”).需要将extglob shell选项设置为使用它们.一个例子:

~$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

如果我将shell表达式传递给在子shell中执行它的程序,则运算符会导致错误.这是一个直接运行子shell的测试(这里我从另一个目录执行以确保不会过早地进行扩展):

~/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)'

我已经尝试过各种各样的逃避,但我没想到的任何东西都能正常工作.我还怀疑extglob没有设置在子shell中,但事实并非如此:

~$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)'

任何解决方案升值

解决方法

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读