Bash 从文本文件读取并创建配置文件的脚本

Bash 从文本文件读取并创建配置文件的脚本,bash,shell,Bash,Shell,我有一个名为lists.txt的文本文件,其中列出了所有节点。文件位于/etc/icinga2/zones.d/master/hosts/中,包含以下内容: node1 node2 node3 . . . 我还有另一个文件名为template.conf 我想写一个剧本 从lists.txt文件读取 通过将template.conf的内容复制为node1.conf 创建一个名为new:/etc/icinga2/zones.d/master/hosts/new的新目录 chmod icinga:i

我有一个名为
lists.txt
的文本文件,其中列出了所有节点。文件位于
/etc/icinga2/zones.d/master/hosts/
中,包含以下内容:

node1
node2
node3
.
.
.
我还有另一个文件名为
template.conf

我想写一个剧本

  • lists.txt文件读取
  • 通过将
    template.conf
    的内容复制为
    node1.conf
  • 创建一个名为new:
    /etc/icinga2/zones.d/master/hosts/new的新目录
  • chmod icinga:icinga node1.conf
  • git add node1.conf
  • 这就是我到目前为止的想法:


    你能帮我完成吗?提前谢谢


    @威廉·珀塞尔, 谢谢你的回复。这是脚本,它运行并抛出一些错误,但确实创建了文件,但没有.conf后缀:

    #!/bin/bash
    cd /etc/icinga2/zones.d/master/hosts/new
    while read f; do
       cp -v "$f" /etc/icinga2/zones.d/master/hosts/new/"${f%}.conf"
       cp template.conf "${f%.conf}"
       chown icinga:icinga "${f%}.conf"
    #   git add "${f%.conf}"
    #   git commit -m "Add $f"
    done < list.txt
    
    #/bin/bash
    cd/etc/icinga2/zones.d/master/hosts/new
    同时读取f;做
    cp-v“$f”/etc/icinga2/zones.d/master/hosts/new/“${f%}.conf”
    cp template.conf“${f%.conf}”
    chown icinga:icinga“${f%}.conf”
    #git添加“${f%.conf}”
    #git提交-m“添加$f”
    完成
    它运行并创建名为node1、node2等的新文件。。。但是没有后缀.conf。不知道为什么。然后在所有模板中复制模板的内容,但也会引发以下错误:

    cp:cannotstat'node1':没有这样的文件或目录chown:cannot access'node2':没有这样的文件或目录

    我试图将其添加为注释,但格式设置使其无法阅读

    感谢您在阅读f时

    ;做
    
    while read f; do 
       cp -v "$f" /etc/icinga2/zones.d/master/hosts/new/"${f%.conf}"
       cp template.conf "${f%.conf}"
       chmod icinga:icinga "${f%.conf}"
       git add "${f%.conf}"
       git commit -m "Add $f"
    done < list.txt
    
    cp-v“$f”/etc/icinga2/zones.d/master/hosts/new/“${f%.conf}” cp template.conf“${f%.conf}” chmod icinga:icinga“${f%.conf}” git添加“${f%.conf}” git提交-m“添加$f” 完成

    请注意,如果您的任何文件名包含换行符,这将完全失败,但如果您将文件名存储在一个文件中,则这种情况实际上并不好。

    那么您的程序在哪里失败?您似乎已经编写了所需的所有功能!看看这个解释如何逐行读取文件。尝试实施它并使用它来帮助您。祝你好运谢谢你的回复。这是最后一个脚本,它运行并创建名为node1、node2等的新文件。。。但是没有后缀.conf。不知道为什么。然后复制所有模板中的内容,但也会引发以下错误:[CODE]cp:cannotstat'node1':没有这样的文件或目录chown:cannotaccess'node2':没有这样的文件或目录[/CODE]CODE#/bin/bash cd/etc/icinga2/zones.d/master/hosts/new,同时读取f;do cp-v“$f”/etc/icinga2/zones.d/master/hosts/new/“${f%}.conf”cp template.conf“${f%.conf}”chown-icinga:icinga“${f%}.conf”#git add“${f%.conf}”#git commit-m“add$f”donecodeconf后缀丢失,因为您用
    ${f%.conf}/code>
    
    while read f; do 
       cp -v "$f" /etc/icinga2/zones.d/master/hosts/new/"${f%.conf}"
       cp template.conf "${f%.conf}"
       chmod icinga:icinga "${f%.conf}"
       git add "${f%.conf}"
       git commit -m "Add $f"
    done < list.txt