我如何重定向在bash错误到/ dev / null的? [英] How do I redirect errors to /dev/null in bash?
问题描述
我们每天运行Selenium测试,以测试我们的网站和扩展。我写了一个脚本(根据<一个href=\"http://stackoverflow.com/questions/31832193/how-can-i-count-the-number-of-files-containing-a-specific-string-but-not-contain\">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的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!