【发布时间】:2022-01-21 01:55:41
【问题描述】:
我正在尝试做与这篇文章类似的事情:Execute command on all files in a directory
我在一个目录中有一堆测试文件,我需要使用带有多个选项的特定运行命令来运行每个测试。
- 我尝试使用 shell 脚本中的 for 循环,但出现以下错误:
for:找不到命令。 做:找不到命令。 f:未定义的变量。
脚本:
#!/bin/csh
set FILES = ../../test_files/*.out
for f in $FILES; do
runcmd -option1 -option2 -option3 -option4 "$f" >> results.log
done
- 我也尝试使用 find -exec 命令,但我收到以下错误:
发现:`-exec' 缺少参数
我的印象是我收到了这个错误,因为我在尝试运行的命令中有多个选项。
脚本:
#!/bin/csh
find ../../test_files -name "*.out" -exec run_cmd -option1 -option2 -option3 -option4 -- echo {} \ > results.log
在尝试运行脚本之前,我对脚本执行了 chmod 777。而且我还尝试在 bash 中使用运行相同的脚本(通过在我的 .sh 脚本中使用 #!/bin/bash 并在终端中输入 bash),但是当我使用 find -exec 时出现相同的错误,并且脚本没有'当我使用 for 循环时什么都不做。
有人可以帮我弄清楚为什么我的脚本不起作用吗?或者告诉我另一种方法来对目录中的所有测试文件执行运行命令?
【问题讨论】:
-
您的
for循环使用 bash (/ksh/zsh/dash/just plain sh) 语法,这在 csh (/tcsh) 中不起作用。在 csh 中,您将改用foreach。至于find -exec,您需要一个转义或引用;来结束-exec部分(看起来您在那里有一个转义空格,是否缺少分号?)。
标签: bash shell for-loop scripting