下面是一个示例脚本来执行此任务:
#!/bin/bash
# Check if file exists
if [ ! -f "$1" ]; then
echo "File does not exist"
exit 1
fi
# Check header record has all columns
header_columns=$(head -n 1 "$1" | tr ',' ' ' | wc -w)
expected_columns=5
if [ "$header_columns" != "$expected_columns" ]; then
echo "Header record does not have all columns"
exit 1
fi
# Check trailer record exists
trailer=$(tail -n 1 "$1")
if [ -z "$trailer" ]; then
echo "Trailer record does not exist"
exit 1
fi
echo "File validation successful"
运行脚本时,您需要提供要验证的文件的路径作为参数。示例文件应如下所示:
col1,col2,col3,col4,col5
data1,data2,data3,data4,data5
data1,data2,data3,data4,data5
EOF
脚本首先检查文件是否存在。然后,它提取标题记录中的列数,如果该列数不等于预期列数,则脚本将输出错误消息并退出。接下来,脚本检查尾部记录是否存在。如果不存在,则脚本将输出错误消息并退出。如果两个检查都通过,则脚本将输出成功消息并退出。