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
4
  • Does this answer your question? Difference between single and double quotes in Bash Commented Jul 9, 2020 at 19:40
  • Run your script through Shell Check. You've got several simple syntax errors. Commented Jul 9, 2020 at 19:41
  • Pretty sure you don't want backticks around /root/user/test/*.cfg or your output. Commented Jul 9, 2020 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. Commented Jul 27, 2020 at 13:48

1 Answer 1

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...

Sign up to request clarification or add additional context in comments.

6 Comments

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.
%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?
Oh ok sorry, I didn't know, bash magic. But anyway I got "line 13: /etc/nagios3/conf.d/servers/test/$cfg: ambiguous redirect"
Put a set -x at the top to see what's actually being parsed. Odd that the $cfg isn't being expanded first.
use user-disk }' '*' this is the two last lines of code before error with set -x
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.