diff options
Diffstat (limited to 'test.sh')
-rwxr-xr-x | test.sh | 47 |
1 files changed, 47 insertions, 0 deletions
@@ -0,0 +1,47 @@ | |||
1 | #!/bin/bash | ||
2 | |||
3 | ########################################################################## | ||
4 | # File Name : test.sh | ||
5 | # Encoding : utf-8 | ||
6 | # Author : We-unite | ||
7 | # Email : weunite1848@gmail.com | ||
8 | # Created Time : 2023-10-06 | ||
9 | ########################################################################## | ||
10 | |||
11 | # 写一个脚本,测试已经生成的main的功能 | ||
12 | # 输入年份为自1976年至2076年的任意年份 | ||
13 | # 如果未出现段错误就重定向输出到res.txt,否则输出到err.txt,并在屏幕上显示出错的年份 | ||
14 | |||
15 | # 设置要测试的可执行文件 | ||
16 | executable="main" | ||
17 | |||
18 | # 清空res.txt和err.txt | ||
19 | echo "" > res.txt | ||
20 | |||
21 | # 循环测试年份,计算总用时为多少秒 | ||
22 | start_time=$(date +%s) | ||
23 | for year in {1900..2100}; do | ||
24 | echo "==================测试年份 $year=====================" | ||
25 | |||
26 | # 执行可执行文件,并捕获标准输出和标准错误 | ||
27 | output=$(./$executable "$year" 2>&1) | ||
28 | |||
29 | # 获取命令的退出状态码 | ||
30 | exit_code=$? | ||
31 | |||
32 | if [ $exit_code -eq 0 ]; then | ||
33 | # 执行正常,将输出重定向到res.txt | ||
34 | echo -e "$year:\n$output" >> res.txt | ||
35 | # echo "执行正常" | ||
36 | else | ||
37 | # 执行异常,将错误信息重定向到err.txt | ||
38 | echo -e "$year:\n$output" >> err.txt | ||
39 | echo "执行异常(退出码: $exit_code)" | ||
40 | # 停止执行 | ||
41 | exit 1 | ||
42 | fi | ||
43 | done | ||
44 | end_time=$(date +%s) | ||
45 | |||
46 | echo "测试成功!" | ||
47 | echo "总用时:$((end_time - start_time))秒" | ||