如何在 BASH shell 脚本中在终端中获取多个文件

How to take multiple files in terminal in BASH shell scripting

...... 问题解决

要输入文件名或任何其他变量,您必须使用 $1 $2 $3 例如作为脚本的输入。如果您将它们放在特定目录中(比方说./output)并在父目录中调用不带变量的脚本,它会更灵活 - 那么就您放入其中的文件数量而言,它会更灵活,没有指控变量并捕获输入以进行代码注入 - 代码应如下所示:

for i in $(find ./output -name '*.out')
    do
        grep "enthalpy new" $i >> step.txt  
    done

目前 $I 正在引用循环的迭代,因此 1,2,3 .... grep 无法找到这些文件,因此会出现错误。

有两种方法可以解决这个问题。 $@ 包含传递给脚本的参数,因此您可以尝试:

grep "enthalpy new" "$@" >> step.txt

或者,如果您想遍历每个 parameter/file,请尝试:

for fil in "$@"
do
      grep  "enthalpy new" "$fil" >> step.txt
done