EGS
EGS

Reputation: 445

Language in the output of a shell script is changing

I wrote a script that lists all the packages installed on a machine, and then compares it with another list located in /tmp/pacotes_iniciais.log. For some reason, the language of the output changes sometimes. It alternates between English and Portuguese.

The first time I run the script, it outputs Mon (Monday), but in the second run the date is Seg, which is the diminutive for Segunda (Monday in Portuguese).

Also, when in English, the words are getting cutted at the 14º character.

This is problematic because totaly unvalidates the comparison.

Below is an excerpt of the script that creates the list and performs the comparison:

dpkg -l | awk '{print $2}' >> /tmp/lista_pacotes.log

echo "\nPackages added until" $(date) "\n" >> /tmp/diferencas.log

grep -F -x -v -f /tmp/pacotes_iniciais.log /tmp/lista_pacotes.log >> /tmp/diferencas.log

The content of /tmp/diferencas.log

Second run:

Packages added until Seg Nov 14 08:48:22 BRT 2016 

sl

First run:

Packages added until Mon Nov 14 08:24:17 BRT 2016 

Name
acpi-support-b
apt-listchange
aptitude-commo
aptitude-doc-e
bash-completio
ca-certificate
console-setup-

[...]

What could be causing this?

Note: I am running this script on a 64-bit Debian Jessie OS, and accessing this machine through SSH.

Upvotes: 1

Views: 151

Answers (1)

Ruslan Osmanov
Ruslan Osmanov

Reputation: 21492

From the info page for date:

Invoking ‘date’ with no FORMAT argument is equivalent to invoking it with a default format that depends on the ‘LC_TIME’ locale category.

Examples

LC_TIME=en_US.UTF-8 date
LC_TIME=ru_RU.UTF-8 date

Sample Output

Mon Nov 14 20:27:59 +07 2016
Пн ноя 14 20:28:04 +07 2016

So the behavior you described is due to your locale settings, namely the LC_TIME environment variable. Adjust it somewhere before running the script (on the target machine!), e.g.:

export LC_TIME=C

The other issues are most likely due to the locale settings as well. So adjust the rest of the locale environment variables in similar manner. LANG and LC_NUMERIC variables are particularly important. Check out the info page: info bash LANG.

Upvotes: 1

Related Questions