I need to replace characters in the output of a script but they are NOT in the last printed line. They are in the middle of the output.
E.g.
XX----------------------------------------------------------XX
|XXX |XXX
| XX | XXX
| XX------------------------------------------------------+-----XXX
| | +
| | +----------------------------------------------------+ |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | 12:34:56 <------+ Characters to replace! |
| | | | |
| | | <--------------------------+ | |
| | | | |
| | | | |
| | | | |
+XX--+ | | |
XX | +----------------------------------------------------+ |
XXX |
+--------------------------------------------------------------+
the code:
#!/bin/bash
function printThing(){
local timeVar=$(date +"%T")
local lines=(
' XX----------------------------------------------------------XX'
' |XXX |XXX'
' | XX | XXX'
' | XX------------------------------------------------------+-----XXX'
' | | +'
' | | +----------------------------------------------------+ |'
' | | | | |'
' | | | | |'
' | | | | |'
' | | | | |'
' | | | | |'
" | | | $timeVar <------+ Characters to replace! |"
' | | | | |'
' | | | <--------------------------+ | |'
' | | | | |'
' | | | | |'
' | | | | |'
' +XX--+ | | |'
' XX | +----------------------------------------------------+ |'
' XXX |'
' +--------------------------------------------------------------+' )
for i in "${lines[@]}"
do
echo "$i"
done
while :
do
local timeVar=$(date +"%T")
#Replace the time in the strings printed above
sleep 1
done
}
printThing
How would I go about replacing the time value without messing up the whole layout?
Best Answer
If you have
ncurses
installed, you can use thetput
command to move the cursor to some place on the terminal, where you can use any printing command.Example:
You may want to move the cursor someplace safe before you exit the function, though. For example using
Which will move it to the last line of the terminal.