diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/linux_development:detecting_output_type.ascii | 44 |
1 files changed, 36 insertions, 8 deletions
diff --git a/src/linux_development:detecting_output_type.ascii b/src/linux_development:detecting_output_type.ascii index a2954ec..792ee3d 100644 --- a/src/linux_development:detecting_output_type.ascii +++ b/src/linux_development:detecting_output_type.ascii @@ -19,19 +19,21 @@ For example, a script like this... ---- #!/usr/bin/env bash -echo -e "[\e[32mINFO\e[0m] This is a useful info message." -echo -e "[\e[33mWARN\e[0m] This is an ominous warning message." -echo -e "[\e[31mERROR\e[0m] This is a scary error message." +echo -e "[ \e[32mINFO\e[0m ] This is a useful info message." +echo -e "[ \e[33mWARN\e[0m ] This is an ominous warning message." +echo -e "[ \e[31mERROR\e[0m ] This is a scary error message." ---- Outputs something like this in the terminal [role="terminal"] -____ -\[[green]#INFO#] This is a useful info message. -\[[yellow]#WARN#] This is an ominous warning message. -\[[red]#ERROR#] This is a scary error message. -____ +==== +[ [green]#INFO# ] This is a useful info message. + +[ [yellow]#WARN# ] This is an ominous warning message. + +[ [red]#ERROR# ] This is a scary error message. +==== ...But something like this when piped to less @@ -44,6 +46,32 @@ ____ ---- +As you can see, the escape codes are sent verbatim to the program on the other +side of the pipe. If it doesn't properly render them by default (which newer +versions of gnu less do now), you'll see those escape codes on your output. +This is particularly annoying in the cases of log files. The same behavior +comes out if the output stream is redirected into a file. + +So how do we detect that the end user has redirected output with a pipe or a +redirection so we can remove the escape codes and just ouput plain text? + + +The Solution +------------ + +Bash provides this really useful 'if switch', *-t*. From the bash man page... + +[quote, Bash(1)] +____ + -t fd True if file descriptor fd is open and refers to a terminal. +____ + +Let's give this a try. + + + + + :revdate: June 29, 2016 [role="datelastedit"] |