0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

perlとロケール

Last updated at Posted at 2016-12-05

外部コマンドをガシガシ実行するスクリプトで、
日本語でインストールしたCentOSで、コマンド実行時だけ、
実行結果は全部英語で受け取りたい(受け取った文字列をチェックしたりするとして、、、)

実行後に$ENVで設定を変えておけば、とりあえず英語で結果を受け取れるけど、、、
runuserで別ユーザーでコマンド実行、とかすると、ダメだったので、
-cで実行するコマンドの最初に、exportで環境変数を設定した。。。

a.pl
# ! /usr/bin/perl -w

use strict;
use warnings;

# CentOS6を、日本語でインストールした環境

# これは日本語で結果が出る。
system(qq(date));

$ENV{'LC_ALL'} = 'C';
$ENV{'LANG'} = 'C';

# 環境変数を変えてコマンドを実行

# これは英語で結果が出る。
system(qq(date));

# runuserで別ユーザーでコマンド実行、とかすると、
# 環境変数が変わるので、呼び出し元で環境変数いじってても、意味がない。
# これは日本語で結果が出る。。。
system(qq(runuser -l postgres -c 'date'));

# 仕方ないので、-cで実行するコマンドの最初に、
# exportあたりで、ロケールの設定を変えてから、
# コマンドを実行する。
# これは英語で結果が出る。
system(qq(runuser -l postgres -c 'export LC_ALL=C LANG=C; date'));

[root@localhost ~]#
[root@localhost ~]# ./a.pl
2016年 12月  5日 月曜日 16:17:11 JST
Mon Dec  5 16:17:11 JST 2016
2016年 12月  5日 月曜日 16:17:11 JST
Mon Dec  5 16:17:11 JST 2016
[root@localhost ~]#
[root@localhost ~]#
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?