I am trying to extract the file date from the output of a gsutil ls -l command and compare it to other dates. However, the comparison appears to be not working.
lstr=$(gsutil ls -l gs://input-bucket/my_file_PAS_EXTRACT_INBOUND.xml)
prcs_date=$(echo ${lstr} | awk '{print $2}' | awk -F"T" '{print $1}')
date_frmt=$(date -d "$prcs_date" +"%Y-%m-%d")
cmpr_date=$(date -d "2024-05-01" +"%Y-%m-%d")
high_date=$(date -d "2025-07-09" +"%Y-%m-%d")
if [[ "${date_frmt}" -ge "${cmpr_date}" && "${date_frmt}" -lt "${high_date}" ]]
then
fname=$(echo "$lstr" | awk '{print $3}')
gsutil cp "${fname}" gs://output-dev/
if [ $? -eq 0 ]
then
echo "1 File Successfully Copied to Target Location"
else
echo "Error in copying file - ${fname}"
fi
else
echo "Skipping File ${fname} as it falls outside date range"
fi
The code fails to copy the file saying "Skipping File gs://input-bucket/my_file_PAS_EXTRACT_INBOUND.xml as it falls outside date range" even though the file date falls witin the range mentioned in the code above.
The output of the gsutil ls -l command is :
$ gsutil ls -l gs://input-bucket/my_file_PAS_EXTRACT_INBOUND.xml
13461 2024-10-11T07:45:15Z gs://input-bucket/my_file_PAS_EXTRACT_INBOUND.xml
TOTAL: 1 objects, 13461 bytes (13.35 KiB)
Can someone please help where I may be going wrong ?
date .... +%sfor your numeric comparisons-geand-lt?prcs_date=$(awk -F'[ :T]+' '{print $2}' <<< "$lstr")-geand-lecomparisons compare numbers, not general strings. When they get something like "2024-05-01", they'll treat it as an arithmetic expression (2024 minus 5 minus 1, which comes out to 2018), which isn't what you want at all.