[英]Reading a bash variable for todays date using awk and passing it back to bash script
我试图遍历包含三件事的常规文件的内容: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
任何建议或指示将不胜感激!
报价应该是问题所在。 在 bash 中如果在引号内使用相同类型的引号会导致错误。 为了避免这种情况,我们需要转义内部引号。 在你的情况下echo "$(date): Recieved "$2" in $LIST"
应该是echo "$(date): Recieved \\"$2\\" in $LIST"
就像你正在使用的 awk 一样明智
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
你需要尝试像
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.