bash - 在 bash 脚本中添加 '(' 字符的 bash 语法错误
问题描述
我在 Linux 命令行上运行以下命令
cp !(non_*).txt some_folder/
它按预期工作(即,将当前文件夹中以“non_”开头的文件除外的所有 *.txt 文件复制到 some_folder。)
但是当将同一行添加到脚本文件并执行它时
./script.sh
它抛出以下错误。
./script.sh: line 1: syntax error near unexpected token `('
./script.sh: line 1: ` cp !(oam_cfg*).txt kk/'
这里有什么更正?
仅供参考:我正在尝试将当前文件夹中以“non_”开头的文件除外的所有 *.txt 文件复制到 some_folder。
解决方案
正如 Jetchisel 一小时前评论的那样,您需要打开扩展通配符。
一个例子:
$: touch foo.txt bar.txt # create a file
$: echo +(foo*txt) # use an extended glob, which fails on syntax
bash: syntax error near unexpected token `('
$: shopt -s extglob # turn extended globbing *ON*
$: echo +(foo*txt) # same command now succeeds
foo.txt
$: echo !(foo).txt # negative works as well
bar.txt
推荐阅读
- prolog - 将谓词应用于输入列表 Prolog
- python-3.x - 使用 GET 请求和 Beautiful Soup 进行网络抓取的问题
- android - 如何通过删除所有视图来修复动态创建和添加的视图的内存泄漏?
- c - 函数不在 C 中同时运行
- spring-boot - 通过服务调用时文件上传失败,即restTemplate.postForEntity
- jmeter - PerfMon 指标收集器图显示最多 4 分钟的信息,但脚本执行的实际时间为 10 分钟
- python - 如何在ORACLE表中插入json数据?
- sql - 循环所有表并执行存储过程
- javascript - PHP将文件发送到Web服务器无法访问的浏览器
- jquery - 当您在每个循环中修改 jQuery 集合时,是包含修改后的集合还是仅包含原始集合?