【问题标题】:tr and while/until commands in shell scriptshell 脚本中的 tr 和 while/until 命令
【发布时间】:2018-03-14 07:59:59
【问题描述】:

我正在做的是对一个距离其当前位置 13 个位置的字符进行编码。 例如,如果我输入welcome,它应该回显jrypbzr。

这是我写的:

read words

echo $words | tr '[A-Za-z]' '[????]' (Please ignore the ???? part.)

这成功解决了编码问题,但是我需要输入多次,而我写的代码只能读取一次。谁能告诉我如何多次输入?

谢谢!

【问题讨论】:

    标签: linux shell tr


    【解决方案1】:

    首先,将您的输入保存在文本文件中。那么

    while read words
    do
    
      # here, do whatever you want with words
    
    done < your-input-file.txt
    

    说明:您将输入文件的内容提供给 while 循环,该循环逐行读取并以单词的形式存储。

    如果要使用换行符以外的分隔符,可以使用:

    while IFS=";" read words
    

    并在 IFS=" " 中放置您喜欢的任何分隔符。

    【讨论】:

    • 非常感谢乔!我这里还有一个问题,如何让shell脚本通过输入一个空行来退出用户输入?
    • 如果 [ "$words" -eq "" ] 则中断
    猜你喜欢
    • 2014-11-03
    • 1970-01-01
    • 1970-01-01
    • 2014-11-21
    • 2016-11-14
    • 2013-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多