1

I have a script that reads a text file that has all the nodes listed in there:

node1
node2
node3
.
.
.

It creates a ".conf" file for each node in the /etc/icinga2/zones.d/master/hosts/new/ directory

Copies the content of the file name linux-template into each new conf file.

Everything worked as I expected, but I also get errors for each node:

Can anyone please help?

Thanks

This is my script:

#!/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 linux-template.conf "$f.conf"
   chown icinga:icinga "$f.conf"

done < linux-list.txt

Once everything got copied, I get these errors below (for all the nodes, ie. node 1):

cp: cannot stat ‘node1’: No such file or directory 
chown: cannot access ‘node1’: No such file or directory
Irina I
  • 31
  • 1
  • 5
  • 2
    Also see [How to use Shellcheck](https://github.com/koalaman/shellcheck), [How to debug a bash script?](https://unix.stackexchange.com/q/155551/56041) (U&L.SE), [How to debug a bash script?](https://stackoverflow.com/q/951336/608639) (SO), [How to debug bash script?](https://askubuntu.com/q/21136) (AskU), [Debugging Bash scripts](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html), etc. – jww Jul 18 '18 at 21:25

1 Answers1

1

It looks like it's complaining because there isn't a file called "node1" in your directory and you have verbose mode on.

This script looks like it will also cause undesired behavior if you're not located in the /etc/icinga2/zones.d/master/hosts/new/ directory when you run it.

The script is saying:

  1. Copy files node1,node2,... in my current directory and place the copy here: /etc/icinga2/zones.d/master/hosts/new/"$f.conf"
  2. Copy linux-template.conf from the current directory and name it "node[1-9].conf" in the current directory.
  3. Chown the "node[1-9].conf" in the current directory.

I suggest using absolute paths and I'm not quite sure why the first cp is necessary. If you're intending to copy linux-template.conf into each node[1-9].conf that you created in step 1, the second copy will create and overwrite the file anyway and step 1 would not be needed.

Joaquin
  • 2,013
  • 3
  • 14
  • 26
Bobby Tables
  • 191
  • 2
  • 15
  • Thanks for your reply. I didn't paste this line, but I have a line in there that cd into that absolute path: cd /etc/icinga2/zones.d/master/hosts/new Is there any way we can do both copies in one line? I would like the script to create one config file per node and copies the content of the template to each newly created conf file. Thank you – Irina I Jul 18 '18 at 21:14
  • cp will create the file if it doesn't exist and overwrite it if it does. This behavior can be changed with flags to copy but if all you want to do is create node[1-9].conf with the contents of linux-template.conf then replace lines 1 and 2 with: `cp linux-template.conf /etc/icinga2/zones.d/master/hosts/new/"$f.conf"` – Bobby Tables Jul 18 '18 at 21:28