Sunday, May 19, 2024
7
rated 0 times [  7] [ 0]  / answers: 1 / hits: 4656  / 3 Years ago, fri, october 22, 2021, 2:55:31

The code below will output whatever in file word by word on the screen. For example:



Hello will be displayed for 1 second and disappear. Then, the next word in the sentence will appear for a second and disappear and so on.



How do I output whatever is being displayed in the middle of the screen?



awk '{i=1; while(i<=NF){ print $((i++)); system("sleep 1; clear") }}' file

More From » command-line

 Answers
3

Here you're a very robust bash script:



#!/bin/bash

## When the program is interrupted, call the cleanup function
trap "cleanup; exit" SIGHUP SIGINT SIGTERM

## Check if file exists
[ -f "$1" ] || { echo "File not found!"; exit; }

function cleanup() {
## Restores the screen content
tput rmcup

## Makes the cursor visible again
tput cvvis
}

## Saves the screen contents
tput smcup

## Loop over all words
while read line
do
## Gets terminal width and height
height=$(tput lines)
width=$(tput cols)

## Gets the length of the current word
line_length=${#line}

## Clears the screen
clear

## Puts the cursor on the middle of the terminal (a bit more to the left, to center the word)
tput cup "$((height/2))" "$((($width-$line_length)/2))"

## Hides the cursor
tput civis

## Prints the word
printf "$line"

## Sleeps one second
sleep 1

## Passes the words separated by a newline to the loop
done < <(tr ' ' '
' < "$1")

## When the program ends, call the cleanup function
cleanup

[#20840] Saturday, October 23, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
polcomposte

Total Points: 421
Total Questions: 92
Total Answers: 109

Location: Uzbekistan
Member since Mon, Jul 20, 2020
4 Years ago
polcomposte questions
Wed, Dec 14, 22, 02:45, 1 Year ago
Sat, Apr 9, 22, 01:36, 2 Years ago
Mon, Dec 26, 22, 01:52, 1 Year ago
Fri, May 20, 22, 12:03, 2 Years ago
;