在bash的目录中循环所有具有特定名称的文件

在bash的目录中循环所有具有特定名称的文件,bash,Bash,我正在尝试制作一个脚本来测试我的文件是否完全符合要求,但我以前从未使用过bash: #!/bin/bash ./myfile <test.in 1>>test.out 2>>testerror.out if cmp -s "test.out" "pattern.out" then echo "Test matches pattern" else echo "Test does not match pattern"

我正在尝试制作一个脚本来测试我的文件是否完全符合要求,但我以前从未使用过bash:

 #!/bin/bash
./myfile <test.in 1>>test.out 2>>testerror.out
if cmp -s "test.out" "pattern.out"
    then
        echo "Test matches pattern"
    else
        echo "Test does not match pattern"
    fi
if cmp -s "testerror.out" "pattern.err"
    then
        echo "Errors matches pattern"
    else
        echo "Errors does not match pattern"
    fi
#/bin/bash
./myfile>test.out 2>>testerror.out
如果cmp-s“测试输出”模式输出
然后
echo“测试匹配模式”
其他的
echo“测试与模式不匹配”
fi
如果cmp-s“testerror.out”模式为“pattern.err”
然后
回显“错误匹配模式”
其他的
回显“错误与模式不匹配”
fi
我是否可以这样编写:在调用./script.sh myfile pattern之后,我的脚本将运行所有名为pattern*.in的文件,并检查myfile是否提供与pattern*.out和pattern*.err相同的文件?e、 g有文件pattern1、pattern2、pattern4,我想对它们运行测试,但不想对不存在的pattern3运行测试

我可以随便创建新文件吗?(假设我不需要它们)如果我是从命令行执行的,我会使用类似

< pattern.in ./myfile | diff -s ./pattern.out
但我不知道如何在脚本文件中编写它以使其工作


或者我应该每次都使用rm?

如果我理解正确:

for infile in pattern*.in ; do  
    outfile="${infile%.in}.out"
    errfile="${infile%.in}.err"

    echo "Working on input $infile with output $outfile and error $errfile"
    ./myfile <"$infile" >>"$outfile" 2>>"$errfile"
    # Your `if`..`fi` blocks here, referencing infile/outfile/errfile
done
用于填充图案*.in;做
outfile=“${infle%.in}.out”
errfile=“${infle%.in}.err”
echo“使用输出$outfile和错误$errfile处理输入$infle”
./myfile>“$outfile”2>“$errfile”
#您的'if`..`fi`块位于此处,参考infle/outfile/errfile
完成

%
替换运算符从变量值的末尾去除一个子字符串。因此,如果
$infle
模式,那么
${infle%.in}
是没有尾随
.in
的模式,即
模式。
输出文件
错误文件
分配使用它来复制正在处理的文件中特定
的第一部分(例如,
模式1
)。

使用
for
循环来迭代与模式匹配的所有文件。
对于模式中的文件*./myfile
欢迎访问该网站!查看和,了解有关提问的更多信息,这些问题将吸引高质量的答案。您可以选择包含更多信息。您的意思是希望能够将
myfile
pattern
作为命令行参数提供给
script.sh