开发者

How do I make a bash script terminate on new line

This is for learning purposes. I wrote a script that will simulate typing.

The usage is:

$ typewriter (insert some text here)

Then the script will echo it in a random way that looks like someone is typing. Fine, but the problem is, if the input includes a semicolon ( ; ) it breaks.

For instance:

$ typewriter hello; world

I imagine this is a simple fix. I just cannot figure it out.

T开发者_如何学Pythonhanks in advance!

CODE:

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

RANGE=4
the_input=$*
if [ x$* = "x" ] || [ x$* = "xusage" ] || [ x$* = "xhelp" ] || [ x$* = "x--help" ];
then
        echo "Usage: typewriter <some text that you want to look like it's typed>"
        exit 1

fi
  while [ -n "$the_input" ]
  do
    number=$RANDOM
    let "number %= RANGE"
    printf "%c" "$the_input"
    sleep .$number
    the_input=${the_input#?}
  done
  printf "\n"


Not really: ; signals the end of the command. You'll have a similar issue with pipes and input/output redirection (|, <, >), symbols that have meaning.

Your only alternative is to put the argument in quotes.

typewriter "some; text<>| that should be displayed"


You can also modify your script to read from stdin:

the_input=`cat`

The cat command will assign to the_input all the input from the user until a ^D is typed.

The advantages are that the user can type more than one line and the spacing within the line will be preserved.

Neat script!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜