Kanu Thakor
Kanu Thakor

Reputation: 63

write a shell script to Display any string at given x, y position and Display current terminal number in Unix?

How can I do these two programs in Unix/Linux shellscript

  1. Display your name at given x, y position
  2. Display your terminal number

Upvotes: 1

Views: 1686

Answers (2)

r3mainer
r3mainer

Reputation: 24587

2D cursor control is a platform-specific feature, but ANSI escape codes are often supported. This ought to work:

echo -e "\033[20;10HHello World" # prints "Hello World" at x=20, y=10

In case you're wondering, \033 corresponds to an escape character (ASCII 27) when using the -e flag with echo.

Your username should be in the $USER environment variable:

echo $USER

And if by "terminal number" you're referring to the tty terminal identifier for your current shell, that's available from the command line if you type in who am i. This will also print your user name and login date/time, but you can extract just the terminal info with awk:

TTY=`who am i | awk '{ print $2 }'`
echo $TTY

So you can put this all together to print your username and tty identifier in bold text at the top left of your console window as follows:

TTY=`who am i | awk '{ print $2 }'` ; echo -e "\033[1m\033[1;1H$USER ($TTY)\033[0m"

Upvotes: 1

GooseDeveloper
GooseDeveloper

Reputation: 137

Here is a simple C program to move the cursor:

#include<stdio.h>
#include<stdlib.h>
#include<curses.h>

int main(int argc, char** argv)
{
  int x;
  int y;

  if(argc < 2) return EXIT_FAILURE;

  x = atoi(argv[1]);
  y = atoi(argv[2]);

  move(x, y);
}

This program uses the curses library to move the cursor to a given location. As far as your second question, I do not know what you mean by "Terminal Number". Please edit your answer to clarify...

Upvotes: 0

Related Questions