This script works perfectly well on Linux (bash), but trying to convert to Unix (MacOS) it’s struggling.
while read -r key date
do
age=$(gdate -d "$date" +%s)
#do stuff with $key and $date
# $key should be 'expiresOn' and $date should be eg '2024-03-02T00:00:00Z'
done <<<$(cat $file |grep expiresOn)
The output on MacOS is
gdate: invalid date ‘2024-03-02T00:00:00Z expiresOn: 2024-06-30T00:00:00Z expiresOn: 2024-07-01T00:00:00Z expiresOn: 2024-07-01T00:00:00Z expiresOn: 2024-05-30T00:00:00Z expiresOn: 2024-06-30T00:00:00Z’
From what I’ve read on other posts, gdate is used instead of date, but not sure if it’s this or the while read that’s causing issues.
I tried using -d '\n' among other delimiters but still not working.
>Solution :
You need another syntax:
while read -r key date
do
age=$(gdate -d "$date" +%s)
#do stuff with $key and $date
# $key should be 'expiresOn' and $date should be eg '2024-03-02T00:00:00Z'
done < <(cat "$file" |grep expiresOn)
Process Substitution >(command ...) or <(...) is replaced by a temporary filename. Writing or reading that file causes bytes to get piped to the command inside. Often used in combination with file redirection: cmd1 2> >(cmd2).
See http://mywiki.wooledge.org/ProcessSubstitution http://mywiki.wooledge.org/BashFAQ/024