模式匹配在bash脚本中不起作用 [英] Pattern match does not work in bash script

查看:75
本文介绍了模式匹配在bash脚本中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用模式匹配!("file1")在bash脚本中不起作用,但可以在命令行中使用.

Using the pattern match !("file1") does not work within a bash script but will work on the command line.

例如:

ls  !("file1"|"file2")

这将列出目录中除file1file2之外的所有文件.

This will list all files in directory except file1 and file2.

在脚本中执行该行时,将显示此错误:

When that line is executed in a script this error is displayed:

./script.sh: line 1: syntax error near unexpected token `('
./script.sh: line 1: ` ls  !("file1"|"file2") ' 

无论使用什么rm -v !("file1").发生相同的错误.这是怎么回事,为什么这在脚本中不起作用?

Regardless what is used rm -v !("file1"). The same error takes place. What is going on here why does this not work in a script?

推荐答案

您尝试使用的扩展glob语法在默认情况下处于关闭状态;您必须在每个要使用它的脚本中分别启用它.

The extended glob syntax you are trying to use is turned off by default; you have to enable it separately in each script where you want to use it.

shopt -s extglob

脚本不应该使用ls ,尽管我想您在这里只是将其用作占位符.

Scripts should not use ls though I imagine you were using it merely as a placeholder here.

这篇关于模式匹配在bash脚本中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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