#!/bin/bash ########################################################################## # File Name : test.sh # Encoding : utf-8 # Author : We-unite # Email : weunite1848@gmail.com # Created Time : 2023-10-06 ########################################################################## # 如果未出现段错误就重定向输出到res.txt,否则输出到err.txt,并在屏幕上显示出错的年份 # 设置要测试的可执行文件 executable="main" echo "" > res.txt start_time=$(date +%s) for year in {1800..2200}; do echo "==================测试年份 $year=====================" output=$(./$executable "$year" 2>&1) exit_code=$? if [ $exit_code -eq 0 ]; then echo -e "$year:\n$output" >> res.txt else # 执行异常,将错误信息重定向到err.txt echo -e "$year:\n$output" >> err.txt echo "执行异常(退出码: $exit_code)" exit 1 fi done end_time=$(date +%s) echo "测试成功!" echo "总用时:$((end_time - start_time))秒"