我正在尝试遍历包含三个内容的常规文件的内容:1)文件说明2)Linux服务器中文件的命名约定3)Linux服务器路径
将有大约20行以下格式类似的行,如果文件存在,它将被放入csv文件中以作每日报告。
Test File,TEST.$(date+'%Y-%m-%d'),/received/completed/$(date+'%Y-%m-%d')
上面有类似格式的多行,带有不同的文件路径,文件名等。>
如上所述,常规文件分为这3个部分,我需要在今天使用通配符,因为这就是Linux环境中完整文件的文件夹结构。以下是供参考的代码。它全天运行,以上面的文件作为参数来检查文件路径。
## Frequency in seconds to check the path RECHECK_WAIT=5 ## Start and end time to know when we should start ## and stop checking START_TIME=0000 END_TIME=2359 LIST=$1 DATE_FORMAT=$(date +'%Y-%m-%d') DATE_FORMAT1=$(date +'%Y%m%d') REPORT_FILE="report_$(date +%b-%d-%y).csv" check_if_report_exists() { ## Check to see if a report file already exists ## If not, use base template as starting point if [[ ! -f $REPORT_FILE ]] then base_report=template.csv else base_report=$REPORT_FILE fi } update_csv_file_recv() { echo "$(date): Recieved "$2" in $LIST" ## Update .csv field for timestamp of recieved time and mark as Recieved awk -v date="$(date +"%b-%d-%y %H:%M:%S")" -v type="$1" -v OFS=, -F, '{ if($2 ~ type) $3=date; if($2 ~ type) $6="Recieved"} 1' $base_report > tmp_$REPORT_FILE cp tmp_$REPORT_FILE $REPORT_FILE 2>/dev/null } update_csv_file_processed(){ echo "$(date): "$2" was processed and no longer exists" ## Update .csv for timestamp of completed time and mark as Completed awk -v date="$(date +"%b-%d-%y %H:%M:%S")" -v type="$1" -v OFS=, -F, '{ if($2 ~ type) $4=date; if($2 ~ type) $6="Completed"} 1' $base_report > tmp_$REPORT_FILE cp tmp_$REPORT_FILE $REPORT_FILE 2>/dev/null } run_checks(){ ## Check if file exists in path check_if_exists="$(ls $1 | grep "$2")" ## Check .csv file to see if item is already marked as Received check_already_recv="$(cat report_$(date +%b-%d-%y).csv 2>/dev/null | grep "$1" | awk -F, '{ print $6 }' | grep "Recieved")" ## Check .csv file to see if item is already marked as Completed check_completed="$(cat report_$(date +%b-%d-%y).csv 2>/dev/null | grep "$1" | awk -F, '{ print $6 }' | grep "Completed")" } check_files() { cat $LIST | while read line do TYPE="$(echo $line | awk -F, '{ print $1 }')" EXPECTED="$(echo $line | awk -F, '{ print $2 }')" path="$(echo $line | awk -F, '{ print $3 }')" check_if_report_exists ## Check if files were recieved/processed run_checks "$path" "$EXPECTED" ## If not tagged as completed in .csv perform if [[ -z $check_completed ]] then ## Check if file does not exist and not already recieved if [[ ! -z $check_if_exists ]] && [[ -z $check_already_recv ]] then update_csv_file_recv "$TYPE" "$EXPECTED" fi ## Check if file no longer exists, but was previously recieved if [[ -z $check_if_exists ]] && [[ ! -z $check_already_recv ]] then update_csv_file_processed "$TYPE" "$EXPECTED" fi fi done ## Wait a set amount of time before checking again sleep $RECHECK_WAIT ## Update timestamp to see if we should no longer execute check_time="$(date +%H%M)" } ## Loop forever while true do ## Get current time check_time="$(date +%H%M)" ## Check if current time is between our parameters while [[ $check_time -ge $START_TIME ]] && [[ $check_time -lt $END_TIME ]] do ### Main function used to execute checks and updates check_files done ### If its not within timeframe it will wait and try again in n-seconds sleep $RECHECK_WAIT done
我在这里有两个问题。1)awk命令不会按照我想要的那样将日期作为变量读取。
check_files
中的ls命令无法找到路径(我知道它们存在于今天的日期)。我相信这是因为awk命令实际上并没有按预期运行$(date)
变量。
ls: cannot access /done/$DATE_FORMAT: No such file or directory
2)最近,我也开始同时出现此错误:./file_exists.sh: line 75: [[: 0931: value too great for base (error token is "0931")
我这样运行脚本./file_exist regular_text_file
任何建议或指示,将不胜感激!
我正在尝试遍历包含三个内容的常规文件的内容:1)文件说明2)linux服务器中文件的命名约定3)Linux服务器路径...
echo "$(date): Recieved "$2" in $LIST"
应该是echo "$(date): Recieved \"$2\" in $LIST"