2011-05-08 115 views
0

我写了一个小脚本来模拟某人在屏幕上输入用户输入。bash脚本中的换行解释

只要没有换行符,它效果很好。我似乎无法弄清楚如何改变我的脚本来完成这项工作,而且我知道它必须简单。

如果有人有更好的脚本编写方式,我也可以进行完整的重构。

#!/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 
+0

我应该澄清一下 - 我认为这是一个让它搞砸的换行符。如果你做了类似的事情(该程序称为打字机),它会在换行符上打断:ls -la |打字机 – Kevin 2011-05-08 15:17:23

回答

4

您的代码只读取一行。这遍历所有行。

#!/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 
+0

是的。这很简单。我尝试了很多奇怪的黑客,但并没有想到要这样做一个嵌套循环。非常酷,感谢很多家伙。 – Kevin 2011-05-08 15:30:39