gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 09:31:14 25 4
gpt4 key购买 nike

我正在尝试制作一个脚本来测试我的文件是否完全符合预期,但我之前没有使用过 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

我可以这样写吗,在调用 ./script.sh myfile pattern 之后,我的脚本将运行所有名为 pattern*.in 的文件,并检查 myfile 是否提供与 pattern*.out 和 pattern*.err 相同的文件?例如,有文件 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

% 替换运算符从变量值的末尾剥 ionic 字符串。所以如果 $infilepattern.in,那么 ${infile%.in 就是没有尾随 .in >,即 patternoutfileerrfile 赋值使用它来复制特定 .in 的第一部分(例如,pattern1)正在处理文件。

关于bash - 循环 bash 目录中具有特定名称的所有文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49456684/

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