bash:使用 <<< not working on macos

问题描述 投票:0回答:1

这个脚本在 Linux (bash) 上运行得很好,但尝试转换到 Unix (MacOS) 时却很困难。

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)

MacOS 上的输出是

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’

从我在其他帖子中读到的内容来看,使用

gdate
而不是
date
,但不确定是这个还是
while read
导致了问题。 我尝试在其他分隔符中使用
-d '\n'
但仍然不起作用。

bash date while-loop
1个回答
0
投票

您需要另一种语法:

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)

进程替换

>(command ...)
<(...)
被临时文件名替换。写入或读取该文件会导致字节通过管道传输到内部命令。通常与文件重定向结合使用:
cmd1 2> >(cmd2)

参见 http://mywiki.wooledge.org/ProcessSubstitution http://mywiki.wooledge.org/BashFAQ/024

© www.soinside.com 2019 - 2024. All rights reserved.