失败的是 cmd 变量。
awk 'function computeDuration(start, end) {
print start;
print end;
cmd="date --date=\"start\" +%s";print cmd;
cmd | getline startDate;
cmd="date --date=\"end\" +%s";print cmd;
cmd | getline endDate;
print startDate;
print endDate;
return endDate-startDate;
} BEGIN {computeDuration("2012-7-16 13:20","2012-7-16 13:30")}'
OUTPUT:
2012-7-16 13:20
2012-7-16 13:30
date --date="$start" +%s
date --date="$end" +%s
1342389600
1342389600
$ 符号不应该在那里,您需要在变量周围加上额外的引号
awk 'function computeDuration(start, end) {
print start;
print end;
cmd="date --date=\""start"\" +%s";print cmd;
cmd | getline startDate;
cmd="date --date=\""end"\" +%s";print cmd;
cmd | getline endDate;
print startDate;
print endDate;
return endDate-startDate;
} BEGIN {print computeDuration("2012-7-16 13:20","2012-7-16 13:30")}'
OUTPUT:
2012-7-16 13:20
2012-7-16 13:30
date --date="2012-7-16 13:20" +%s
date --date="2012-7-16 13:30" +%s
1342437600
1342438200