Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

bash: while read -r using <<< not working on macos

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading