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

I am writing a shell-script on an Ubuntu 12.04 server that should compare some data in a log file. In the log file, the date is indicated in the format:

[Mon Apr 08 15:02:54 2013] 

As you can see, it indicates Apr.

According to the man page, the parameter that will be used in bash for this, b or h .

However, it does not matter, for me (when comparing the script or directly in the shell) use b , h or B. They all return the full name of the month.

 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?) 

It is of course very difficult to make 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 the Norwegian language for installation when I installed the server.)

Thank you answer from @ toro2k here, I ended up with a working solution:

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

(This did not work:

 LC_ALL=C 

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

)

+4
source share
1 answer

I tested it, and the problem seemed to be Norwegian localization:

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

therefore, when you try to LC_ALL log file, you must set the LC_ALL environment LC_ALL to C , i.e.

 LC_ALL=C command 

or

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

For more information on configuring locales, see the Locale page on the Ubuntu wiki.

+4
source

All Articles