简体   繁体   中英

newline interpretation in bash script

I wrote a little script that simulates someone typing the user input onto the screen.

It works great, as long as there isn't a newline. I can't seem to figure out how to alter my script to make this work, and I know it has to be simple.

I'm also open to a complete refactoring if someone has a better way of scripting this.

#!/bin/bash
#Displays input as if someone were typing it

read the_input_line

while [ -n "$the_input_line" ]
        do
                printf "%c" "$the_input_line"
                sleep .1
                the_input_line=${the_input_line#?}
done

Your code only reads one line. This loops over all lines.

#!/bin/bash
#Displays input as if someone were typing it

while read the_input_line
do
  while [ -n "$the_input_line" ]
  do
    printf "%c" "$the_input_line"
    sleep .1
    the_input_line=${the_input_line#?}
  done
  printf "\n"
done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM