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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15882670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:08:18  来源:igfitidea点击:

Bash: Both date "%b" and date "%h" (and of course) date "%B" gives full name of month?

linuxbashshellubuntu-12.04

提问by erolha

I am writing a shell-script in Ubuntu server 12.04 which is supposed to compare some data in a log file. In the log file, the date is given in the format:

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

[Mon Apr 08 15:02:54 2013]

As you can see, it states Apr.

如您所见,它指出Apr

According to the man-page, the option to be used in bash for this is bor h.

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

However it doesn't matter wether I (in my comparing script, or directly in shell) use b, hor B. They all return the full name of the month.

然而,我(在我的比较脚本中,或直接在 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?)

This of course makes it very difficult to do comparisons based on the date...

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

Has anyone else experienced this, and found a solution?

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

(I'm not sure if this is relevant, but I chose Norwegian as installation language when I installed the server.)

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

Thank's to the answer of @toro2k here, I ended up with a working solution:

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

DATE=`LC_ALL=C date +%b" "%d" "%H`

(This did not work:

(这不起作用:

LC_ALL=C  

DATE=date +%b" "%d" "%H

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

)

)

回答by toro2k

I've tested it and the problem seems to be the norwegian localization:

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

$ LC_ALL=C date +%b
Apr

$ LC_ALL=nn_NO.UTF-8 date +%b
april

therefore when you try to parse the log file you should set the LC_ALLenvironment variable to C, i.e.

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

LC_ALL=C command

or

或者

export LC_ALL=C
# your script code here
export -n LC_ALL

For more informations on locales setting see the Localepage on Ubuntu wiki.

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