我必须将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
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 ,输出似乎符合您的预期格式。 我不确定为什么它在那里。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。