Detecting the output stream type of a shell script

前端 未结 4 1332
隐瞒了意图╮
隐瞒了意图╮ 2021-02-07 10:13

I\'m writing a shell script that uses ANSI color characters on the command line.

Example: example.sh

#!/bin/tcsh
printf \"\\033[31m Succ         


        
4条回答
  •  萌比男神i
    2021-02-07 10:29

    See this previous SO question, which covers bash. Tcsh provides the same functionality with filetest -t 1 to see if standard output is a terminal. If it is, then print the color stuff, else leave it out. Here's tcsh:

    #!/bin/tcsh
    if ( -t 1 ) then
            printf "\033[31m Success Color is awesome!\033[0m"
    else
            printf "Plain Text is awesome!"
    endif
    

提交回复
热议问题