LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-19-2010, 10:52 AM   #1
Arashi
LQ Newbie
 
Registered: Jul 2010
Posts: 2

Rep: Reputation: 0
Watch not interpreting escape codes in bash script


Hi there,

I'm fairly new to bash scripting and already having trouble. I need a script that can print some series of strings in colors based in the information of a file, for simplicity let's say it only does:

Code:
#!/bin/bash
printf "\e[1;31;32m%-10s\e[00m" "OK"
When you execute this in the command line it prints a bold green 'OK'. So far so good.

Now, I need to check the output of the script over time using the command watch. The problem then arises. watch seems to ignore the escape codes and just prints:
Code:
[1;31;32mOK [00m
Is there any way to fix this?

If not, how can I check inside the script if it is being executed from a command? (watch in this case) So I can print without color for those cases.

Thanks for your time in advance!
 
Old 07-19-2010, 11:52 AM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Seems this `watch` thing is really finicky, and all I've found on the net about getting it to print colors, involves `cat -v` but I couldn't get that to print the colors either.

Here's a hacky way of not printing colors when `watch` is running, but printing colors when running the script alone. Note that this does not determine if `watch` is watching the very script we're executing here, it ONLY checks to see if `watch` is running at all. So, if you happen to be running `watch` somewhere else on the machine for some other task, then THIS program will still not print in color.

I could not figure out a way to determine if BOTH `watch` was running AND was watching this very program. There's probably a way, but I haven't found it yet.

Code:
#!/bin/bash

if [ "$(ps -C watch --no-headers)" ] ; then
  printf "watch is running, so not in color"
else
  printf "\e[1;31;32m%-10s\e[00m" "no watch running, so using color"
fi
 
Old 07-19-2010, 01:09 PM   #3
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 250Reputation: 250Reputation: 250
Try putting the OK inside the first set of quotes.
Code:
printf "\e[1;31;32m%-10s\e[00mOK"

or

echo -e "\E[0;32mOK"; tput sgr0
It doesn't fix the watch problem though.

Quote:
man watch:

BUGS
...
Non-printing characters are stripped from program output. Use
"cat -v" as part of the command pipeline if you want to see
them.
Unfortunately, cat -v displays ALL the non-printing characters and still no colour.

Code:
[smoker@kids ~]$ watch -n 10 ./tput.sh | cat -v
^[[?1049h^[[1;24r^[[m^[(B^[[4l^[[?7h^[[H^[[2JEvery 10.0s: ./tput.sh^[[1;57HMon Jul 19 19:46:14 2010^[[3;1H[1;31;32mOK^[[3;20H[00m^M^[[4d[0;32mOK^M^[[5d[m(B^[[24;80H[smoker@kids ~]$

Last edited by smoker; 07-19-2010 at 01:48 PM.
 
1 members found this post helpful.
Old 07-19-2010, 01:50 PM   #4
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
It's possible to bypass the watch command by sending the script output directly to the terminal.
This works on my system
Code:
#!/bin/bash
printf "\r\e[1;31;32m%-10s\e[00m" "OK $(date)" > /dev/tty
The carriage return '\r' is to keep the output at the start of the line.
The $(date) is added just to show it's working every 2 seconds.
This method can sometimes interfere with the watch command header line; the header can be suppressed to give neater output by using the '-t' option:
watch -t script.sh
 
Old 07-27-2010, 08:31 AM   #5
Arashi
LQ Newbie
 
Registered: Jul 2010
Posts: 2

Original Poster
Rep: Reputation: 0
Ups... forgot to update this post!

Everyone, thanks for your answers, they helped me solving the problem.

This is how I ended up doing it.

Code:
n=`ps aux | grep 'watch' | grep 'script.sh'`	
if [ "$n" ]; then
	printf "Printing without colors..."
fi
Whit this the script can check if there is a watch calling it and, in that case, print with no colors.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
escape string in bash script so it can be used in command line BuckRogers01 Linux - Software 15 08-12-2010 09:38 AM
BASH - printf printing escape codes even though I have %0b defined DevonB Linux - Newbie 5 12-23-2009 01:42 PM
[SOLVED] printer escape codes smeezekitty Programming 6 10-13-2009 11:52 PM
capturing error codes in bash script nikaudio Linux - General 2 04-09-2008 08:56 AM
Escape Codes for Console Color trainpic Linux - General 1 03-13-2006 07:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration