Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I need a text banner on a screen which is projected all the time. The screen is connected to a PC with tmux on it.

Example of text "banners":

figlet $(fortune)

or

toilet "$(cowsay $(fortune))"

You get the idea.

Initially we thought this banner would need "regeneration capabilities" in case something went wrong, so we did something like this:

watch --interval 1 --no-title echo Paranoid about crashes, but I should not be

but the thing never crashed in a year! (uptime > 1yr)

So we are simplifying and deploying somewhere else too. I just want a UNIX command that does the following:

  • clear the screen (like clear)
  • shows a formatted string (like printf)
  • is modal (like an ncurses app)

I started to write something like this, pinched off of a web tutorial:

#include <ncurses.h>

int main(int argc, char** argv) {
  char* greeting = "Hello, world!";
  initscr();
  addstr(greeting);
  refresh();
  getch();
  endwin();
  return 0;
}

but this is not "modal" (with modal being: disregard all input until CTRL+C) and I may be "rewriting an existing tool".

Which UNIX tool would you use in my case?

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

The command to watch for Ctrl+C (the INT signal) is trap.

#!bin/bash

MSG="Hello, world!"
trap "clear; echo -e $MSG" SIGINT SIGTERM

while :
do
    sleep 60
done

Update - Other signals that can be caught with trap

SIGINT - Ctrl-c
SIGQUIT - Ctrl-\ (this will quit the program, but commands in trap will still be executed)

SIGSTOP (Ctrl-z) doesn't seem to be caught by trap.

trap can also catch signals issued by kill, but I'm not sure how many of them can be caught.

trap also supports some other special names:-
EXIT, DEBUG, RETURN and ERR.
Further documentation on these can be found in the bash reference manual.

share|improve this answer
    
Simple. Elegant. Trapping SIGINT is golden. Thank you! You could add a link to a list of signals to make your answer even more useful to others stumbling on it... –  Robottinosino Oct 16 '12 at 6:01
1  
I had a hunt through some man pages on my Linux box (signal, 7 signal, kill, posix, etc.), but couldn't find any info on keyboard-shortcut to signal mappings. I know a couple though; I'll add them to the answer.. –  Alex Leach Oct 17 '12 at 17:40
    
Good man... and thorough! (cit.) –  Robottinosino Oct 17 '12 at 22:20
add comment

If I understand, you want an infinite loop:

while :; do
  sleep 1
  clear
  # the command: echo, toilet, etc.
done

Maybe, the problem with watch is that the shell expansion of $(command ..) is done once before watch is called so output doesn't change ; change double quotes to single quotes

watch --interval 1 'cowsay $(fortune)'
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.