【问题标题】:newline interpretation in bash scriptbash 脚本中的换行符解释
【发布时间】:2011-05-08 15:14:15
【问题描述】:

我编写了一个小脚本,模拟某人在屏幕上输入用户输入。

只要没有换行符,它就可以很好地工作。我似乎不知道如何改变我的脚本来完成这项工作,我知道它必须很简单。

如果有人有更好的脚本编写方法,我也愿意进行完整的重构。

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

【问题讨论】:

  • 我应该澄清一下 - 我认为是换行符搞砸了。如果您执行以下操作(该程序称为打字机),它会在换行符处中断: ls -la |打字机

标签: bash newline printf


【解决方案1】:

您的代码只读取一行。这会循环所有行。

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

【讨论】:

  • 是的。就是这么简单。我尝试了很多奇怪的技巧,但没想到会做这样的嵌套循环。非常酷,非常感谢老兄。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-06
  • 2013-01-15
相关资源
最近更新 更多