0

I need to modify simple bash script to go through a files and add some text to the end of each file with name of the file variable. There's an example of Nagios .cfg file...so far I got something like this:

#!/bin/bash

list=$(ls /root/user/test/ | sed "s/\.cfg//")

for servery in `/root/user/test/*.cfg`; do
    for $list; do
        echo `
        
        define service {
               host_name                       $list
               service_description             All_fs_free_space
               check_command                   check_nrpe!check_all_disks!3% 1%
               use                             user-disk
        }` >> /etc/nagios3/conf.d/servers/test/$servery
    done
done

which unfortunately didn't work, because of quotation marks...? Is there better solution how to achieve that? Thank you.

Debug output for Paul Hodges solution:

    root@bla:~/blabla# ./add2.sh
    ++ fmt='
    
        define service {
               host_name                       %s
               service_description             All_fs_free_space
               check_command                   check_nrpe!check_all_disks!3%% 1%%
               use                             user-disk
        }'
    ++ for file in '/root/user/test/*.cfg'
    ++ cfg='*.cfg'
    ++ printf '
    
        define service {
               host_name                       %s
               service_description             All_fs_free_space
               check_command                   check_nrpe!check_all_disks!3%% 1%%
               use                             user-disk
        }' '*'
    ./add2.sh: line 14: /root/blabla/test/servers/$cfg: ambiguous redirect
Jan Ves.
  • 15
  • 4
  • Does this answer your question? [Difference between single and double quotes in Bash](https://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash) – John Kugelman Jul 09 '20 at 19:40
  • Run your script through [Shell Check](https://shellcheck.net/). You've got several simple syntax errors. – John Kugelman Jul 09 '20 at 19:41
  • Pretty sure you don't want backticks around `/root/user/test/*.cfg` or your output. – Paul Hodges Jul 09 '20 at 20:38
  • `++ for file in '/root/user/test/*.cfg'` and `++ cfg='*.cfg'` mean there are no filenames that end with `cfg` in `/root/user/test/` for it to loop over. If you want that to skip the loop with no error (or any other useful output), set `shopt -s nullglob`. Otherwise, explicitly test for the existence of `/root/blabla/test/servers/$cfg` before processing, and fail out if it isn't there. See below. – Paul Hodges Jul 27 '20 at 13:48

1 Answers1

1

Abstract it a bit more.

Update -

Checking for existence of file, as per comments above.

#!/bin/bash
fmt='

    define service {
           host_name                       %s
           service_description             All_fs_free_space
           check_command                   check_nrpe!check_all_disks!3%% 1%%
           use                             user-disk
    }'

for file in /root/user/test/*.cfg # assuming there are *.cfg files here
do if [[ -e "/root/blabla/test/servers/$cfg" ]]
   then cfg=${file##*/}                # stripping the path
        printf "$fmt\n" "${cfg%.cfg}" >> /etc/nagios3/conf.d/servers/test/$cfg
   else echo "File not found: /root/blabla/test/servers/$cfg" >&2
   fi
done

Making some assumptions about what you intended.
I'd back everything up first, and then edit to only match one file for a test before using wildcards...

Paul Hodges
  • 13,382
  • 1
  • 17
  • 36
  • Yes, but this will probably do only part of the job. At the place "%s" ($list in my example) I need variable which contain name of the configuration file (host name of a server exactly) without cfg suffix. Skript with one iteration puts content of $fmt at the end of the file and changes variable $list like name of the file which will be modified. – Jan Ves. Jul 13 '20 at 22:03
  • `%s` is a string replacement indicator in the `"$fmt"` format string of the `printf`, which is why there's another argument - `cfg=${file##*/}` strips the leading path info off, so `"${cfg%.cfg}"` is just the name of the configuration file without the cfg suffix. Did you try it? – Paul Hodges Jul 14 '20 at 13:33
  • Oh ok sorry, I didn't know, bash magic. But anyway I got "line 13: /etc/nagios3/conf.d/servers/test/$cfg: ambiguous redirect" – Jan Ves. Jul 14 '20 at 15:55
  • Put a `set -x` at the top to see what's actually being parsed. Odd that the `$cfg` isn't being expanded first. – Paul Hodges Jul 14 '20 at 19:36
  • `use user-disk }' '*'` this is the two last lines of code before error with `set -x` – Jan Ves. Jul 15 '20 at 16:45
  • I created dirs and cfg files and ran this script fine. (I edited a newline into the printf format...) Can you add a section to your OP showing the new code and the full output with the error? – Paul Hodges Jul 15 '20 at 19:01