代码之家  ›  专栏  ›  技术社区  ›  Leonora Tindall

为什么我的回音命令要写两个字?

  •  0
  • Leonora Tindall  · 技术社区  · 6 年前

    #!/bin/bash
    
    adj="$(shuf -n1 adjectives.txt)"
    noun="$(shuf -n1 nouns.txt)"
    
    echo ADJ $adj
    echo NOU $noun
    
    echo "$adj $noun"
    

    ADJ humbler
    NOU lyric
     lyricr
    

    或:

    ADJ bipinnate
    NOU lipases
     lipasese
    

    这种现象有一个腹水瘤: https://asciinema.org/a/199297

    1 回复  |  直到 6 年前
        1
  •  1
  •   FatalError    6 年前

    你的输入文件很可能有CRLF( \r\n \r (回车)将导致输出跳回当前行的第一列,因为您的连接摆脱了 \n 而不是 \右

    这是Windows/DOS使用的格式。

    您可以转换文件,也可以使用 tr ,例如:

    adj="$(shuf -n1 adjectives.txt | tr -d '\r')"
    noun="$(shuf -n1 nouns.txt | tr -d '\r')"
    

    您也可以跨文件运行此操作,例如:

    tr -d '\r' < adjectives.txt > adjectives-reformatted.txt
    
    推荐文章