오류를 표준 출력으로 리디렉션

오류를 표준 출력으로 리디렉션

표준 출력과 동일한 파일에 오류를 저장할 수 있습니다. 나는 다음 방법을 사용했습니다. 문제는 오류 출력이 "항상" 위에 나타난다는 것입니다. 아래 제공된 예에서 오류는 "india" 값을 저장할 수 없는 두 번째 sql 명령과 관련되어 있습니다. 오류 메시지는 파일 상단이 아닌 명령문 옆에 나타나야 합니다.

# cat import.txt
drop table if exists testme;
create table testme (id int , name varchar(255));
insert into testme values (1, 'abc');
insert into testme values (2, 'abc', 'india');
insert into testme values (3, 'xyz');

# mysql test -vvf  < import.txt  >standard.txt 2>&1

# cat standard.txt
ERROR 1136 (21S01) at line 5: Column count doesn't match value count at row 1
--------------
drop table if exists testme
--------------

Query OK, 0 rows affected

--------------
create table testme (id int , name varchar(255))
--------------

Query OK, 0 rows affected

--------------
insert into testme values (1, 'abc')
--------------

Query OK, 1 row affected

--------------
insert into testme values (2, 'abc', 'india')
--------------

--------------
insert into testme values (3, 'xyz')
--------------

Query OK, 1 row affected

예상되는 출력은 다음과 같습니다.

# mysql test -vvf  < import.txt
--------------
drop table if exists testme
--------------

Query OK, 0 rows affected

--------------
create table testme (id int , name varchar(255))
--------------

Query OK, 0 rows affected

--------------
insert into testme values (1, 'abc')
--------------

Query OK, 1 row affected

--------------
insert into testme values (2, 'abc', 'india')
--------------

ERROR 1136 (21S01) at line 4: Column count doesn't match value count at row 1
--------------
insert into testme values (3, 'xyz')
--------------

Query OK, 1 row affected

오류 출력은 관련된 명령문 바로 옆에 배치되어야 합니다. 그렇지 않으면 리디렉션된 출력 파일이 쓸모가 없습니다.

답변1

이는 실제로 쉘과 아무 관련이 없으며 mysql명령줄 유틸리티의 "기능"입니다.

기본적으로 mysql은 출력이 터미널로 전송되지 않는 것을 감지하면 출력 버퍼링을 활성화합니다. 이렇게 하면 성능이 향상됩니다. 그러나 프로그램은 분명히 성공 출력을 STDOUT으로 보내고 오류 출력을 STDERR로 보내고(이것은 의미가 있음) 각 출력에 대해 별도의 버퍼를 유지합니다.

해결책은 간단합니다. -nmysql 명령 매개변수에 추가하면 됩니다. -n(또는) 옵션은 --unbuffered출력 버퍼링을 비활성화합니다.

예를 들어:

mysql test -nvvf  < import.txt  >standard.txt 2>&1

관련 정보