WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Script to launch aterm in background and retrieve TTY  (Read 2102 times)

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11516
Script to launch aterm in background and retrieve TTY
« on: May 26, 2020, 01:54:20 AM »
The attached file shows how to launch a terminal in the background, wait for the shells PID to show up, and retrieve its TTY.
You can then use it to display text and send ANSI escape sequences to control color and cursor position. The script includes
variables to set the size, position, and font for the terminal. It sets up a variable called TTY that you can echo text into.
You can send formated text (fixed width, left justify, right justify, etc.) to TTY using printf.

To use it you can
1. Source it as is into a script. In that case it will run automatically and set TTY.
2. Define it as a function to be called only if you need it.
3. Add your own code to the end of the file.

Defining it as a function can make for a handy debugging option. For instance, calling it NewTerminal and it will set up
a variable called TTY.

Code: [Select]
# Redirect unwanted output to $Null instead of /dev/null.
Null="/dev/null"
# Set DEBUG to 1 to enable debugging output.
DEBUG=0
if [ "$DEBUG" == 1 ]
then
# Launch a new terminal and find out which TTY its connected to.
NewTerminal
# Redirect Null to the TTY.
Null=$TTY

# Turn on script debugging and redirect it.
-x
exec 1>$TTY
exec 2>%1
fi

Examples of adding code to the end of the file:
Script to create a real barebones desktop clock
http://forum.tinycorelinux.net/index.php/topic,13794.msg150397.html#msg150397

Script to display remaining battery charge
http://forum.tinycorelinux.net/index.php/topic,23880.msg150398.html#msg150398

    [EDIT]: Corrected the contents inside the code tags. Corrected shebang in the attachment.  Rich.
« Last Edit: May 26, 2020, 11:12:40 PM by Rich »

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11516
Re: Script to launch aterm in background and retrieve TTY
« Reply #1 on: May 26, 2020, 11:23:46 PM »
I made a couple of corrections.

Changed one line in the code tags from:
Code: [Select]
TTY=$(NewTerminal)
To:
Code: [Select]
NewTerminal
Changed the shebang in the attachment from:
Code: [Select]
#!/bin/bash
To
Code: [Select]
#!/bin/sh