如何在bash中将两个date时间的date格式转换?

如何在bash中将两个date时间的date格式转换?,第1张

概述如何在bash中将两个date时间的date格式转换?

我必须将date时间从"Apr 10 16 07:03:04"格式转换为"[10/12/16 07:03:04 BST]"格式。 我正在使用以下function。

convert_date () { local months=( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ) local i for (( i=0; i<11; i++ )); do [[ $1 = ${months[$i]} ]] && [[ $5 = ${months[$i]} ]] && break done printf "[%2d/%02d/%02d $4 BST]n" $2 $(( i+1 )) $3 printf "[%2d/%02d/%02d $8 BST]n" $6 $(( i+1 )) $7 } d=$( convert_date $1 $2 $3 $4 ) e=$( convert_date $5 $6 $7 $8 ) echo $d echo $e

我的意见将在哪里

./date.sh Apr 10 16 07:03:04 May 12 16 08:11:09

和我的输出应该是

[10/12/16 07:03:04 BST] [12/12/16 08:11:09 BST]

但是我得到如下的输出,

保持活着ssh会话与脚本bash / expect

在linux上,完全清空目录而不删除它的正确方法是什么?

Bash等待命令,等待超过1个PID完成执行

如何在bash中redirect所有stderr?

Bash在OS Xterminal提示中断

[10/12/16 07:03:04 BST] [12/00/00 BST] [12/12/16 08:11:09 BST] [12/00/00 BST]

如何得到确切的输出? 请帮忙。

你如何将文本从windows中的firefox复制到Unix中的bash shell?

在bash中,如何进行控制 – 删除平均杀词?

如何在while循环读取行内读取用户?

复制每个目录中的png,如果png> 600×600?

windows CMD命令对应于linux命令

我会写这样的功能:

function convert_date() { # First sanitize input to something the date command can understand # The problem here is just the 16 which should be 2016 input="$(awk -v year="$(date +%Y)" '{$3=year}1' <<< ${1})" # Now use the date command to reformat the string date -d"${input}" +'[%d/%m/%y %H:%M:%s %Z]' }

像这样调用它:

convert_date 'Apr 10 16 07:03:04'

输出(如果您的当地时间是BST):

[10/04/16 07:03:04 BST]

你的方法看起来很不寻常,我可能会用date而不是脚本。 但是,如果您删除第二个printf ,输出似乎符合您的预期格式。 我不确定为什么它在那里。

总结

以上是内存溢出为你收集整理的如何在bash中将两个date时间的date格式转换?全部内容,希望文章能够帮你解决如何在bash中将两个date时间的date格式转换?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/langs/1228721.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-05
下一篇2022-06-05

发表评论

登录后才能评论

评论列表(0条)

    保存