简体   繁体   English

使用 awk 读取今天日期的 bash 变量并将其传递回 bash 脚本

[英]Reading a bash variable for todays date using awk and passing it back to bash script

I am attempting to loop through the contents of a regular file which contains three things: 1) Description of the file 2) Naming convention of file in linux server 3) Linux server path我试图遍历包含三件事的常规文件的内容:1) 文件描述 2) linux 服务器中文件的命名约定 3) Linux 服务器路径

There will be about 20 or so lines with a similar format as below and if the file exists it will be put in to a csv file for a daily report.将有大约 20 行左右的格式与下面类似,如果该文件存在,它将被放入一个 csv 文件中作为每日报告。

Test File,TEST.$(date+'%Y-%m-%d'),/received/completed/$(date+'%Y-%m-%d')

There are multiple lines in similar format above with different file paths, file names etc.上面有多个类似格式的行,具有不同的文件路径、文件名等。

The regular file is broken up into those 3 parts as described above, and I need to use a wildcard for todays date because thats the folder structure in the linux environment for completed files.常规文件被分解为如上所述的那 3 个部分,我需要对今天的日期使用通配符,因为这是 linux 环境中完整文件的文件夹结构。 Below is the code for reference.下面是代码供参考。 It runs throughout the day to check for file paths using the file above as an argument.它全天运行以使用上面的文件作为参数检查文件路径。

## 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

I am having two issues here.我在这里有两个问题。 1) The awk command isnt reading the date as a variable as I want it to. 1) awk 命令没有像我想要的那样将日期作为变量读取。 The ls command in the check_files is not able to find the path (I know they exist for todays date). check_files的 ls 命令无法找到路径(我知道它们存在于今天的日期)。 I believe its because the awk command isnt actually running the $(date) variable as it should.我相信这是因为 awk 命令实际上并没有像它应该的那样运行$(date)变量。

ls: cannot access /done/$DATE_FORMAT: No such file or directory

2)Most recently i started getting this error for the time as well: ./file_exists.sh: line 75: [[: 0931: value too great for base (error token is "0931") 2)最近我也开始收到这个错误: ./file_exists.sh: line 75: [[: 0931: value too great for base (error token is "0931")

I run the script like this ./file_exist regular_text_file我像这样运行脚本./file_exist regular_text_file

Any advice or pointers would be appreciated!任何建议或指示将不胜感激!

Quotes should be the issue.报价应该是问题所在。 In bash if you are using the same type of quotes within the quotes will cause error.在 bash 中如果在引号内使用相同类型的引号会导致错误。 To avoid this we need to escape the internal quotes.为了避免这种情况,我们需要转义内部引号。 In your case echo "$(date): Recieved "$2" in $LIST" which should be echo "$(date): Recieved \\"$2\\" in $LIST"在你的情况下echo "$(date): Recieved "$2" in $LIST"应该是echo "$(date): Recieved \\"$2\\" in $LIST"

Like wise for the awk you are using就像你正在使用的 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

You need to try like你需要尝试像

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM