문제

When you use git it seems to magically know whether standard out is going through a pipe or into a file vs when it is being displayed to the console. For example, if you have colors enabled and you do

git status

it will colorize the output for different categories of files being listed. However, if you do

git status | less

or

git status > status.txt

it removes the linux color formatting and you only see plain, uncolored text.

How does git detect whether the output of its commands are going to file vs going to the terminal?

도움이 되었습니까?

해결책

isatty(int fd) will check whether the fd refers to a terminal or something else. It's part of unistd.h in the GNU C library.

Man page: http://linux.die.net/man/3/isatty

As an aside: if you want to read from a program using another program, but you want to fool isatty into thinking that your program is a human, there is a way to do that. You can use a pseudo-terminal (pty). This technique is used by expect, for example.

다른 팁

This is a C Code to demonstrate how to detect if standard output is redirected:

int main(int argc, char **argv){
    if (!isatty(fileno(stdout))){
      fprintf(stdout, "argv, argc, someone is redirecting me elsewhere...\n");
      return 1;
    }
    /* rest of C code here... */
}

That is how git knows whether the output is going to the terminal or to a file.

이는 관리되는 메타 데이터와 함께 있어야하는 방법입니다.

관리되는 메타 데이터를 사용하면 용어를 해제하고 한 번만 발생할 수 있습니다 (재사용).

이 문제에 직면 할 때마다 "재료"와 "민사"를 별도의 세트로 분할하여 두 의 조합으로 항목에 태그를 지정할 수 있도록해야합니다.

파일 시스템의 디렉토리 트리와 대비하여 "용어"(연도, 클래스, 재료 또는 다른 메타 데이터)가 동일하게 될 수 있으며 트리에서 여러 번 발생할 수 있습니다. 뿐만 아니라 그러한 조직에서도 의미가 있습니다). 그러나 메타 데이터가있는 이는 동일한 용어가있는 것으로 의미가 없을 것입니다. 엄청나게 계층 구조의 여러 위치가 배치되지 않습니다 ( "콘크리트"는 "재료"그룹에만 속하며, 이는 분류 학적 계층 구조의 다른 그룹에 동시에 속할 수 없음)

하나의 가능한 해결책이지만, 귀하의 경우에 좋은 품목이 동일한 속성, 예를 들어 동일한 속성에 대해 비특이적으로 작성하는 것이 아닙니다.

Civils
-- Port Design
----- Concrete Port Design
-- Bridges
----- Concrete Bridges
.

그러나 동일한 재료 ( "콘크리트")로 모든 항목을 필터링 할 수있는 목적을 물리 칠 것입니다.

From a shell script, use the -t test flag applied to the file descriptor 0 (standard input).

Examples:

# Any Bourne-style shell
[ -t 0 ] && echo This is a terminal

# Modern interactive shells: ksh, bash, zsh
[[ -t 0 ]] && echo This is a terminal
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top