Skip to main content
improve script legibility
Source Link
Stéphane Chazelas
  • 585.2k
  • 96
  • 1.1k
  • 1.7k
#!/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
#!/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
#!/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
deleted 1 character in body; edited tags
Source Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 265

I need to replace charaterscharacters in the output of a script but they are NOT in the last printed line. They are in the middle of the output.

How would iI go about replacing the time value without messing up the whole layout?

I need to replace charaters in the output of a script but they are NOT in the last printed line. They are in the middle of the output.

How would i go about replacing the time value without messing up the whole layout?

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.

How would I go about replacing the time value without messing up the whole layout?

Source Link

Bash replace characters in the output

I need to replace charaters 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?