![파이프 컬 컬러 출력](https://linux55.com/image/45094/%ED%8C%8C%EC%9D%B4%ED%94%84%20%EC%BB%AC%20%EC%BB%AC%EB%9F%AC%20%EC%B6%9C%EB%A0%A5.png)
스크립트 색상입니다
#!/bin/bash
ESC_SEQ="\x1b["
COL_RESET=$ESC_SEQ"39;49;00m"
case $1 in
red ) COL_START=$ESC_SEQ"31;01m"
;;
green) COL_START=$ESC_SEQ"32;01m"
;;
yellow ) COL_START=$ESC_SEQ"33;01m"
;;
blue ) COL_START=$ESC_SEQ"34;01m"
;;
mage ) COL_START=$ESC_SEQ"35;01m"
;;
cyan ) COL_START=$ESC_SEQ"36;01m"
;;
esac
echo -ne $COL_START
while read text;
do
echo $text
done
echo -ne $COL_RESET
내가 할 때
curl localhost:8080/
일반적인 컬 출력 HOOOOORAY로 응답합니다!
내가 할 때
curl localhost:8080/ | color red
예상한 대로 출력 색상이 지정되지 않습니다... "CRY"
나는이 상황을 올바르게 부르는 것이 아니라고 생각합니다.
답변1
귀하의 질문에 요청한대로 정확하게 작동합니다. 탈출이 올바른지 확인해야 할 수도 있습니다. $TERM에 대한 올바른 이스케이프가 아닐 수 있습니다. 이는 다음과 같이 tput을 사용하여 올바른 이스케이프를 쿼리하여 쉽게 수정할 수 있습니다.
COL_RESET=$(tput sgr0)
...
red ) COL_START=$(tput setaf 1)
green ) COL_START=$(tput setaf 2)
blue ) COL_START=$(tput setaf 4)
...
이것을 사용한 또 다른 예여기에서 찾을 수 있습니다.
간섭을 방지하기 위해 에코를 호출하는 방식을 변경할 수도 있습니다. echo에 대한 매뉴얼 페이지에서는 다음을 제안합니다.
Due to shell aliases and built-in `echo' command, using an unadorned `echo'
interactively or in a script may get you different functionality than that
described here. Invoke it via `env' (i.e., `env echo ...') to avoid
interference from the shell.