【问题标题】:How to write multiple line string using Bash with variables?如何使用带有变量的 Bash 编写多行字符串?
【发布时间】:2011-10-24 12:23:51
【问题描述】:

如何使用 BASH 在名为 myconfig.conf 的文件中写入多行?

#!/bin/bash
kernel="2.6.39";
distro="xyz";

echo <<< EOL
line 1, ${kernel}
line 2,
line 3, ${distro}
line 4
line ...
EOL >> /etc/myconfig.conf;
cat /etc/myconfig.conf;

【问题讨论】:

    标签: linux bash bash4


    【解决方案1】:

    语法 (&lt;&lt;&lt;) 和使用的命令 (echo) 错误。

    正确的是:

    #!/bin/bash
    
    kernel="2.6.39"
    distro="xyz"
    cat >/etc/myconfig.conf <<EOL
    line 1, ${kernel}
    line 2, 
    line 3, ${distro}
    line 4 line
    ... 
    EOL
    
    cat /etc/myconfig.conf
    

    这种结构称为Here Document,可以在man --pager='less -p "\s*Here Documents"' bash 下的Bash 手册页中找到。

    【讨论】:

    • 如果你想追加它会是 cat >>
    • 这个很好用,但是你必须确保终止符EOF前面没有空格,否则它不会被识别,你会遇到一个意想不到的结尾文件错误。
    • @StevenEckhoff 这被称为heredoc。
    • 如果我需要 sudo 权限才能写入文件怎么办?
    • @gfpacheco 您可以为此使用 tee,例如 cat
    【解决方案2】:
    #!/bin/bash
    kernel="2.6.39";
    distro="xyz";
    
    cat > /etc/myconfig.conf << EOL
    line 1, ${kernel}
    line 2,
    line 3, ${distro}
    line 4
    line ...
    EOL
    

    这就是你想要的。

    【讨论】:

    • @ktf 我的打字速度并不快,但字母比你少。 ^_*
    • 你的速度更快,但还不够:))))
    • 这帮助很大!
    【解决方案3】:

    如果您不想替换变量,则需要用单引号将 EOL 括起来。

    cat >/tmp/myconfig.conf <<'EOL'
    line 1, ${kernel}
    line 2, 
    line 3, ${distro}
    line 4 line
    ... 
    EOL
    

    上一个例子:

    $ cat /tmp/myconfig.conf 
    line 1, ${kernel}
    line 2, 
    line 3, ${distro}
    line 4 line
    ... 
    

    【讨论】:

      【解决方案4】:

      heredoc 解决方案无疑是最常用的方法。其他常见的解决方案是:

      echo 'line 1, '"${kernel}"'
      line 2,
      line 3, '"${distro}"'
      line 4' > /etc/myconfig.conf
      

      exec 3>&1 # Save current stdout
      exec > /etc/myconfig.conf
      echo line 1, ${kernel}
      echo line 2, 
      echo line 3, ${distro}
      ...
      exec 1>&3  # Restore stdout
      

      printf "%s\n" "line1, ${kernel}" "line2," "line3, $distro" ...
      

      【讨论】:

      • 也许还指向printf,它引入了一些更有趣的变化。
      【解决方案5】:

      以下机制有助于将多行重定向到文件。在" 下保留完整的字符串,以便我们可以重定向变量的值。

      #!/bin/bash
      kernel="2.6.39"
      echo "line 1, ${kernel}
      line 2," > a.txt
      echo 'line 2, ${kernel}
      line 2,' > b.txt
      

      a.txt的内容是

      line 1, 2.6.39
      line 2,
      

      b.txt的内容是

      line 2, ${kernel}
      line 2,
      

      【讨论】:

        【解决方案6】:

        我正在使用 Mac OS 并在 SH 脚本 中编写多行代码,以下代码对我有用

        #! /bin/bash
        FILE_NAME="SomeRandomFile"
        
        touch $FILE_NAME
        
        echo """I wrote all
        the  
        stuff
        here.
        And to access a variable we can use
        $FILE_NAME  
        
        """ >> $FILE_NAME
        
        cat $FILE_NAME
        

        请不要忘记根据需要将 chmod 分配给脚本文件。 我用过

        chmod u+x myScriptFile.sh
        

        【讨论】:

        • 也适用于 Ubuntu 20。
        【解决方案7】:

        我通常将模板放在文件中并使用这个模板引擎:

        ### <template-file> [ARG=VALUE..]
        ## Variables are replaced only within "{{" and "}}" notation.
        ## Example:
        ##         $0 path-to-tmpl REF=master pass=xx
        ##         # The template may look like so:
        ##         #    $pass = ["user", "{{ $pass }}"];
        ##         # Resulting in:
        ##         #    $pass = ["user", "xxx"];
        ##~
        template() {
            tmpl=$1
            shift
        
            for i in $@; do
                declare $i;
            done
        
            eval "echo \"$(sed -e 's/"/\\"/g' -e 's/\$/\\$/g' -e 's/{{\s*\\\(\$\w*\)\s*}}/\1/g' $tmpl)\""
        }
        

        【讨论】:

          【解决方案8】:

          我认为另一种更简单的方法,但绝对适用于少量行

          touch myfile.txt
          echo "line1">>myfile.txt
          echo "line2">>myfile.txt
          echo "line3">>myfile.txt
          echo "line4">>myfile.txt
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2013-01-19
            • 2018-05-02
            • 1970-01-01
            • 1970-01-01
            • 2010-10-24
            • 1970-01-01
            • 2021-05-25
            相关资源
            最近更新 更多