I'm encountering an issue where I am trying to get the size of a terminal by using scripts. Normally I would use the command tput cols
inside the console, however I want to be able to accomplish this feature by strictly using scripts.
As of now I am able to detect the running console and get its file path. However I'm struggling to use this information to get the console's width. I've attempted using the command tput
, but I'm fairly new to Linux/scripts so therefore don't really know what to do.
The reason for doing this is I want to be able to setup a cron entry that notifies the console of its width/columns every so often.
This is my code so far:
tty.sh
#!/bin/bash
#Get PID of terminal
#terminal.txt holds most recent PID of console in use
value=$(</home/test/Documents/terminal.txt)
#Get tty using the PID from terminal.txt
TERMINAL="$(ps h -p $value -o tty)"
echo $TERMINAL
#Use tty to get full filepath for terminal in use
TERMINALPATH=/dev/$TERMINAL
echo $TERMINALPATH
COLUMNS=$(/home/test/Documents/get_columns.sh)
echo $COLUMNS
get_columns.sh
#!/usr/bin/env bash
echo $(/usr/bin/tput cols)
The normal output of TERMINAL
& TERMINALPATH
are pts/terminalnumber
and /dev/pts/terminalnumber
, for example pts/0
& /dev/pts/0
cron
jobs had controlling terminals.