Bash 替换输出中的字符

Bash 替换输出中的字符

我需要替换脚本输出中的字符,但它们不在最后打印的行中。它们位于输出的中间。

例如

XX----------------------------------------------------------XX
|XXX                                                        |XXX
|  XX                                                       |   XXX
|   XX------------------------------------------------------+-----XXX
|    |                                                              +
|    |     +----------------------------------------------------+   |
|    |     |                                                    |   |
|    |     |                                                    |   |
|    |     |                                                    |   |
|    |     |                                                    |   |
|    |     |                                                    |   |
|    |     |            12:34:56  <------+  Characters to replace!  |
|    |     |                                                    |   |
|    |     |     <--------------------------+                   |   |
|    |     |                                                    |   |
|    |     |                                                    |   |
|    |     |                                                    |   |
+XX--+     |                                                    |   |
  XX |     +----------------------------------------------------+   |
   XXX                                                              |
     +--------------------------------------------------------------+

代码:

#!/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

我将如何在不弄乱整个布局的情况下替换时间值?

答案1

如果你已经ncurses安装了,你可以使用tput命令将光标移动到终端上的某个地方,在那里你可以使用任何打印命令。

例子:

clear
for i in $(seq 1 40)
do
    echo '##                                                                    ##'
done
for i in $(seq 1 100)
do
    tput cup 20 5
    date
    sleep 1
done

不过,在退出该函数之前,您可能希望将光标移动到安全的地方。例如使用

tput cup $(tput lines) 0

这会将其移动到终端的最后一行。

答案2

对于支持它的终端,您可以使用保存光标恢复光标转义序列:

#! /bin/sh -
save_cursor=$(tput sc) restore_cursor=$(tput rc)
text="\
XX----------------------------------------------------------XX
|XXX                                                        |XXX
|  XX                                                       |   XXX
|   XX------------------------------------------------------+-----XXX
|    |                                                              +
|    |     +----------------------------------------------------+   |
|    |     |                                                    |   |
|    |     |                                                    |   |
|    |     |                                                    |   |
|    |     |                                                    |   |
|    |     |                                                    |   |
|    |     |            $save_cursor$(date +%T)  <------+  Characters to replace!  |
|    |     |                                                    |   |
|    |     |     <--------------------------+                   |   |
|    |     |                                                    |   |
|    |     |                                                    |   |
|    |     |                                                    |   |
+XX--+     |                                                    |   |
  XX |     +----------------------------------------------------+   |
   XXX                                                              |
     +--------------------------------------------------------------+"

printf '%s\n' "$text"

while sleep 1; do
  printf %s "$restore_cursor$(date +%T)"
done

答案3

也许这样的事情可以解决这个问题:

#!/bin/bash                                                                                                                                                                    

function printThing(){

    local timeVar=$(date +"%T")
    local lines="\                                                                                                                                                             
XX----------------------------------------------------------XX                                                                                                                 
|XXX                                                        |XXX                                                                                                               
|  XX                                                       |   XXX                                                                                                            
|   XX------------------------------------------------------+-----XXX                                                                                                          
|    |                                                              +                                                                                                          
|    |     +----------------------------------------------------+   |                                                                                                          
|    |     |                                                    |   |                                                                                                          
|    |     |                                                    |   |                                                                                                          
|    |     |                                                    |   |                                                                                                          
|    |     |                                                    |   |                                                                                                          
|    |     |                                                    |   |                                                                                                          
|    |     |                       $timeVar                     |   |                                                                                                          
|    |     |                                                    |   |                                                                                                          
|    |     |                                                    |   |                                                                                                          
|    |     |                                                    |   |                                                                                                          
|    |     |                                                    |   |                                                                                                          
|    |     |                                                    |   |                                                                                                          
+XX--+     |                                                    |   |                                                                                                          
  XX |     +----------------------------------------------------+   |                                                                                                          
   XXX                                                              |                                                                                                          
     +--------------------------------------------------------------+"

    for i in "${lines[@]}"
    do
        echo "$i"
    done
}

printf "\033c"

while :
do
    timeVar=$(date +"%T")
    printThing
    sleep 1
    printf "\033c"
done

相关内容