【发布时间】:2021-08-25 22:45:37
【问题描述】:
我想将此 bash 命令转换为 shell 脚本。
BASH
输入:
date --date="Wed Aug 25 22:37:44 +0900 2021" +"%s"
输出:
1629898664
SHELL
tmp.sh:
function time(a, b, c, d, e) { return date --date="a b c d +0900 e" +"%s" }
{print time($1, $2, $3, $4, $5}
时间线:
Wed Aug 25 22:37:44 2021
命令:
awk -f tmp.sh timeline
输出:
awk: tmp.sh:1: function cvtTime(w) { return date --date="Thu May 14 23:40:52 +0900 2020" +"%s" }
awk: tmp.sh:1: ^ syntax error
时间线文件有多行怎么办?喜欢:
Wed Aug 25 22:37:44 2021 JACK
Wed Aug 26 22:37:44 2021 EMILY
Wed Aug 27 22:37:44 2021 SAM
我试过了:
#!/bin/bash
while read -r line; do
date --date="${1} ${2} ${3} ${4} +0900 ${5}" +"%s"
done
想要:
1629898664 JACK
1629985064 EMILY
1630071464 SAM
但它不起作用:(
【问题讨论】:
-
你知道
awk是做什么的吗? -
Shell 函数没有命名参数。
-
您似乎混淆了 shell 脚本和 awk 脚本。
-
另外,您在
time的调用结束时缺少) -
我明白了。它解决了!!!