Bash:日期“%b”和日期“%h”(当然还有)日期“%B”都给出了月份的全名?

ero*_*lha 4 linux bash shell ubuntu-12.04

我正在 Ubuntu 服务器 12.04 中编写一个 shell 脚本,它应该比较日志文件中的一些数据。在日志文件中,日期的格式如下:

[Mon Apr 08 15:02:54 2013]
Run Code Online (Sandbox Code Playgroud)

如您所见,它指出Apr

根据手册页,在 bash 中为此使用的选项是bh

然而,我(在我的比较脚本中,或直接在 shell 中)使用bhB并不重要。它们都返回月份的全名。

date +"%b" #Returns april (should have returned Apr)
date +"%h" #Returns april (should have returned Apr)
date +"%B" #Returns april (correct? Should it not be capital A?)
Run Code Online (Sandbox Code Playgroud)

这当然使得根据日期进行比较变得非常困难......

有没有其他人经历过这个,并找到了解决方案?

(我不确定这是否相关,但我在安装服务器时选择了挪威语作为安装语言。)

感谢@toro2k 在这里的回答,我最终得到了一个可行的解决方案:

DATE=`LC_ALL=C date +%b" "%d" "%H`
Run Code Online (Sandbox Code Playgroud)

(这不起作用:

LC_ALL=C  
Run Code Online (Sandbox Code Playgroud)

日期=date +%b" "%d" "%H

)

tor*_*o2k 5

我已经测试过了,问题似乎是挪威本地化:

$ LC_ALL=C date +%b
Apr

$ LC_ALL=nn_NO.UTF-8 date +%b
april
Run Code Online (Sandbox Code Playgroud)

因此,当您尝试解析日志文件时,您应该将LC_ALL环境变量设置为C,即

LC_ALL=C command
Run Code Online (Sandbox Code Playgroud)

或者

export LC_ALL=C
# your script code here
export -n LC_ALL
Run Code Online (Sandbox Code Playgroud)

有关语言环境设置的更多信息,请参阅Ubuntu wiki上的语言环境页面。