TEST_RESULTS := ./result/* failures.txt: $(TEST_RESULTS) @rm -f $@ @for file in $^; do \ if grep -q 'This case is failed' $$file; then \ echo "Processing $$file..."; \ grep '{.*}' $$file >> failures.txt; \ fi; \ done执行这个命令的时候,提示make: execvp: /bin/sh: 参数列表过长 make: *** [Makefile:310:failures.txt] 错误 127怎么解决,请在此基础上做出更改,并给出完整脚本代码
时间: 2023-11-22 09:56:59 浏览: 34
这个错误通常是因为命令参数列表过长导致的,可以将参数列表拆分成多个小块来解决这个问题。下面是修改后的完整脚本代码:
```
TEST_RESULTS := ./result/*
failures.txt: $(TEST_RESULTS)
@rm -f $@
@for file in $^; do \
if grep -q 'This case is failed' $$file; then \
echo "Processing $$file..."; \
grep '{.*}' $$file >> failures.tmp; \
fi; \
done
@cat failures.tmp | xargs echo >> failures.txt
@rm -f failures.tmp
```
这个脚本会将所有的测试结果文件按照指定文件路径 `./result/*` 匹配,然后逐一进行处理。如果文件中包含 "This case is failed",则会将该文件中所有符合正则表达式 `{.*}` 的内容追加到 `failures.tmp` 文件中。最后,将 `failures.tmp` 中的内容通过 `xargs` 命令合并成一行,并追加到 `failures.txt` 文件中,最后删除临时文件 `failures.tmp`。
相关问题
TEST_RESULTS := ./result/* failures.txt: $(TEST_RESULTS) @rm -f $@ @for file in $^; do \ if grep -q 'This case is failed' $$file; then \ echo "Processing $$file..."; \ grep '{.*}' $$file >> failures.txt; \ fi; \ done执行这个命令的时候,提示make: execvp: /bin/sh: 参数列表过长 make: *** [Makefile:310:failures.txt] 错误 127怎么解决,请给出完整的命令更改
可以使用 xargs 命令来避免参数列表过长的问题,将命令改为如下:
```
TEST_RESULTS := ./result/*
failures.txt: $(TEST_RESULTS)
@rm -f $@
@echo "Processing test results..."
@grep -h '{.*}' $(TEST_RESULTS) | xargs -I {} sh -c 'if echo {} | grep -q "This case is failed"; then echo {} >> failures.txt; fi;'
```
这里使用 xargs 命令将多个文件的内容合并到一起,然后再使用 grep 进行筛选。注意,这里使用了 -h 参数来隐藏文件名,并使用 -I 参数指定占位符为 {},然后在 sh 命令中使用 if 条件语句来判断是否是失败的测试用例,如果是则将结果写入到 failures.txt 文件中。
TEST_RESULTS := ./result/* failures.txt: $(TEST_RESULTS) @rm -f $@ @for file in $^; do \ if grep -q 'This case is failed' $$file; then \ echo "Processing $$file..."; \ grep '{.*}' $$file >> failures.txt; \ fi; \ done执行这个命令的时候,提示make: execvp: /bin/sh: 参数列表过长 make: *** [Makefile:310:failures.txt] 错误 127怎么解决
这个错误通常是由于参数列表过长导致的,可以尝试分批次执行命令,或者使用 xargs 命令来处理。可以将命令改为:
```
find ./result/ -type f -name "*.txt" -exec grep -q 'This case is failed' {} \; -print0 | xargs -0 grep '{.*}' >> failures.txt
```
这个命令使用 find 命令查找所有的 ".txt" 文件,并在每个文件中查找是否包含 "This case is failed" 字符串,如果包含则将文件名通过 xargs 命令传递给第二个 grep 命令进行匹配,最终结果输出到 failures.txt 文件中。这样可以避免参数列表过长的问题。