Saturday, August 13, 2022

[FIXED] How to stop echoing on terminal while script is in loop

Issue

I made a script which has allot of loops hence it has short timedelay to Output results. but while the script is in loop, whenever i enter any key on my keyboard it just mess the beauty of Outputs.

A short part of my script goes like this~

#!/usr/bin/bash

echo -e "Moved File to /bin"

## until this loop ends, whenever i INPUT anything from my KEYBOARD it just print on terminal
while true; do
    sleep 1
    [[ -f *.txt ]] && rm *.txt 2>/dev/null || break 
done

echo -e "Done"

Now here The Terminal Must Output this~

Moved File to /bin
Done

But if I Press UP-ARROW on my KEYBOARD While The Script Still in Loop, The Terminal...~

Moved File to /bin
^[[A^[[A^[[A^[[A^[[A^[[A
Done

Hope Your got my Problem.

A way to disable USER INPUT while a Script is running or any other alternative method to accomplish the same would be helpful. Thankyou


Solution

In my piu-piu project I'm switching user input on and off like this:

...
COF='\e[?25l' #Cursor Off
CON='\e[?25h' #Cursor On

cursor () {
    case $1 in
         on) stty  echo; printf "$CON";;
        off) stty -echo; printf "$COF";;
    esac
}
...

cursor on
cursor off
...


Answered By - Ivan
Answer Checked By - Cary Denson (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.