簡體   English   中英

使用 awk 讀取今天日期的 bash 變量並將其傳遞回 bash 腳本

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM