我如何重定向在bash错误到/ dev / null的?

编程入门 行业动态 更新时间:2024-10-21 12:55:08
本文介绍了我如何重定向在bash错误到/ dev / null的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我们每天运行Selenium测试,以测试我们的网站和扩展。我写了一个脚本(根据this问题)来计算的通过和失败的测试的数量。下面是脚本:

We run daily Selenium tests to test our website and extensions. I wrote a script (according to this question) to count the number of passed and failed tests. Here is the script:

#!/bin/bash today=`TZ='Asia/Tel_Aviv' date +"%Y-%m-%d"` yesterday=`TZ='Asia/Tel_Aviv' date +"%Y-%m-%d" -d "yesterday"` ... print_test_results() { declare -i passed_tests=0 declare -i failed_tests=0 declare -i total_tests=0 log_suffix="_${file_name}.log" yesterday_logs="${log_prefix}${yesterday}_[1,2]*${log_suffix}" today_logs="${log_prefix}${today}_0*${log_suffix}" for temp_file_name in $yesterday_logs $today_logs ; do total_tests+=1 if grep -q FAILED "$temp_file_name" ; then failed_tests+=1 elif grep -q OK "$temp_file_name" ; then passed_tests+=1 else failed_tests+=1 fi done echo "<tr>" echo "<td>$test_name - $today</td>" if [ $passed_tests = "0" ]; then echo "<td>$passed_tests passed</td>" echo "<td><span style=\"color: red;\">$failed_tests failed</span></td>" else echo "<td><span style=\"color: green;\">$passed_tests passed</span></td>" echo "<td>$failed_tests failed</td>" fi echo "<td>$total_tests tests total</td>" echo "</tr>" } file_name="chrome_gmail_1_with_extension_test" test_name="Chrome Gmail 1 With Extension Test" print_test_results ...

但问题是,如果文件不存在(在$ yesterday_logs $ today_logs ),我得到错误信息。我该如何重定向这些错误信息到/ dev / null的?我想他们从脚本重定向到/ dev / null的,而不是从行调用脚本 - 我想这个脚本来从来没有显示有关该文件不存在的错误消息

But the problem is, if the files are not there (in $yesterday_logs $today_logs), I get error messages. How do I redirect these error messages to /dev/null? I want to redirect them to /dev/null from the script, and not from the line calling the script - I want this script to never show error messages about files which don't exist.

推荐答案

最后,我改变了这一行:

Eventually I changed this line to:

for temp_file_name in `ls $yesterday_logs $today_logs 2>/dev/null` ; do total_tests+=1 if grep -q FAILED "$temp_file_name" ; then failed_tests+=1 elif grep -q OK "$temp_file_name" ; then passed_tests+=1 else failed_tests+=1 fi done

那么只有 LS 错误被定向到的/ dev / null的。

更多推荐

我如何重定向在bash错误到/ dev / null的?

本文发布于:2023-07-06 07:35:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047360.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重定向   错误   null   bash   dev

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!