Linux 如何在shell脚本中查找存储在两个字符串中的时间差

Linux 如何在shell脚本中查找存储在两个字符串中的时间差,linux,bash,shell,Linux,Bash,Shell,我正在编写一个shell脚本,其中有一个变量t_1=“10:51:45”,我想找出这个时间(假设它总是在过去)和当前时间之间的时差 我试过这么做 curr_time=`date | cut -d " " -f 5` A1=`date +%H:%M:%S -d $t_1` C1=`date +%H:%M:%S -d $curr_time` echo "$((C1 - A1))" 低于错误(当前时间=23:10:03) 表达式中出现语法错误(错误标记为“:

我正在编写一个shell脚本,其中有一个变量
t_1=“10:51:45”
,我想找出这个时间(假设它总是在过去)和当前时间之间的时差

我试过这么做

curr_time=`date | cut -d " " -f 5`
A1=`date +%H:%M:%S -d $t_1`
C1=`date +%H:%M:%S -d $curr_time`

echo "$((C1 - A1))"
低于错误(当前时间=23:10:03)

表达式中出现语法错误(错误标记为“:10:03-10:51:45”)您需要将日期转换为历元格式。这可以在GNU awk中完成:

awk -v etim="2021 02 08 23 27 00" '{ print strftime("%c",mktime(etim)-strftime("%s"),1) }' <<< /dev/null

好答案。通过使用
curr\u time=$(date+%T)
t_1="10:51:45"
curr_time=`date | cut -d " " -f 5`

A1=`date +%s -d $t_1`
C1=`date +%s -d $curr_time`

echo "${C1} : ${A1}"
echo "$((C1 - A1))"