gpt4 book ai didi

linux - 模式匹配在 bash 脚本中不起作用

转载 作者:IT王子 更新时间:2023-10-29 00:26:41 25 4
gpt4 key购买 nike

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

例如:

ls  !("file1"|"file2")

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

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

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

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

最佳答案

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

shopt -s extglob

Scripts should not use ls尽管我想您在这里只是将它用作占位符。

关于linux - 模式匹配在 bash 脚本中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55546727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com