Advertisement
metalx1000

Improved Typewriter Script with Standard Input

Jan 9th, 2025
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.72 KB | None | 0 0
  1. #!/bin/bash
  2. ######################################################################
  3. #Copyright (C) 2025 Kris Occhipinti
  4. #https://filmsbykris.com
  5. #
  6. #This program is free software: you can redistribute it and/or modify
  7. #it under the terms of the GNU General Public License as published by
  8. #the Free Software Foundation version 3 of the License.
  9.  
  10. #This program is distributed in the hope that it will be useful,
  11. #but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. #GNU General Public License for more details.
  14.  
  15. #You should have received a copy of the GNU General Public License
  16. #along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17. ######################################################################
  18.  
  19. #prints give file one character at a time
  20. #like a typewriter
  21.  
  22. # fatal uses SIGUSR1 to allow clean fatal errors
  23. trap "exit 1" 10
  24. PROC=$$
  25.  
  26. function error() {
  27.   red=$(echo -en "\e[31m")
  28.   normal=$(echo -en "\e[0m")
  29.  
  30.   echo -e "${red}$@${normal}" >&2
  31.   #  exit 1
  32.   kill -10 $PROC
  33. }
  34.  
  35. #check if there is an file givin or standard input passed
  36. [[ -z "$1" ]] && [[ -t 0 ]] && error "Input Needed."
  37.  
  38. #if no file then read standard input
  39. [[ $1 ]] || read -r -d '' text
  40.  
  41. # if file is given and exists set it's contact as text
  42. [[ $1 ]] && [[ -f $1 ]] && text="$(<$1)"
  43. [[ $text ]] || error "No Text Given"
  44.  
  45. function typetext() {
  46.   #Read each file one line at a time
  47.   while read l; do
  48.     #read each line one Character at a time
  49.     for ((i = 0; i < ${#l}; i++)); do
  50.       echo -n "${l:$i:1}"
  51.       sleep .01
  52.     done
  53.  
  54.     #add new line char
  55.     echo ""
  56.   done
  57.   exit
  58. }
  59.  
  60. #pass text to type function
  61. echo "$text" | typetext
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement