2

I'm creating a script to more easily allow my organization to add workstations to our Nagios monitoring system.

Each time the script runs, I'd like to be able to input the hostname, alias,and address, ( as variables input by user ) and have those variables called WITHIN the sed command so as to "create" a new host much faster then typing it all out.

I'm attempting to add the entire string AFTER sed finds " address of the host }"

which i assumed was /a ( append after it finds said string ) Still, using double quotes hasn't allowed the variables to be called.

#!bin/bash

# Prompt for hostname, alias, and IP address of new host
# take user input has variable to be used in sed 

# Takes user input for hostname
echo host_name : 
read var_hostname    
# Takes user input for alias
echo alias : 
read var_alias
# Takes user input for ip address
echo address : 
read var_address
# searches for the top-most instance "address of the host" within the     windows.cfg file
# after said instance is found, appends the new string below, which calls     for the variables received 
# previously by user
sed -i -e  " address of the host } /a 
define host{
    use             windows-server  ; Inherit default values from a     template
    host_name       $var_hostname       ; The name we are giving to this     host
    alias           $var_alias       ; A longer name associated with the host
    address         $var_address     ; IP address of the host
    }" 
    windows.cfg        

I want the string "define host{}" to be written to the file with the variables the user put in. Instead

  • Variables are always substituted inside double quotes, I can't see any reason why it wouldn't work here. Does it work if you replace the variables with literal strings? – Barmar Jan 31 '19 at 00:53
  • `address of the host} a` should be `/address of the host/a` – Barmar Jan 31 '19 at 00:54
  • 1
    I attempetd to use the /option/ but that negated the variables. The variables are now being called...but i recieve this when i run the script : expression #1, char 28: extra characters after command – James Little Jan 31 '19 at 02:35
  • @JamesLittle Did my solution help you out? – Allan Jan 31 '19 at 02:46
  • 1
    If you want to see what a shell script is doing put `set -x` at the beginning. It will then show all the commands as they're executing, with variables filled in. – Barmar Jan 31 '19 at 04:14
  • The duplicate is about replacing rather than replacing, but the crucial part is the observation that multi-line strings in `sed` need to be escaped or perhaps otherwise encoded if your `sed` dialect supports it. – tripleee Jan 31 '19 at 05:08
  • 1
    @Allan yes, your explanation was really well written and helped clarify allot. I've also done the -x command as suggested, to see what the command is actually doing as well which is useful. The script runs without errors...but there is not change to the windows.cfg file. – James Little Feb 01 '19 at 21:44
  • @Allan, I acutally figured out what was wrong. in the cfg file, there was an " enter key" after "host" so the command wasn't picking up the "}" in the actual .cfg file. Thanks for the help! – James Little Feb 01 '19 at 22:31
  • @Allan is there a way to have sed somehow search for "ip address of the host (next line) } " instaed of just a string on one line – James Little Feb 02 '19 at 00:44
  • what do you mean? – Allan Feb 02 '19 at 07:36

1 Answers1

1

File to modify windows.cfg

 cat windows.cfg 
#some settings
#more settings
test
abc

lot of stuff

test2

backup

 address of the host }

Sed script:

 cat sed.sh 
#!/bin/bash

# Prompt for hostname, alias, and IP address of new host
# take user input has variable to be used in sed

# Takes user input for hostname
echo host_name :
read var_hostname
# Takes user input for alias
echo alias :
read var_alias
# Takes user input for ip address
echo address :
read var_address
# searches for the top-most instance "address of the host" within the     windows.cfg file
# after said instance is found, appends the new string below, which calls     for the variables received
# previously by user
sed -e  '/address of the host }/r'<(
echo "define host{"
echo "   use             windows-server  ; Inherit default values from a template"
echo "   host_name       $var_hostname       ; The name we are giving to this host"
echo "   alias           $var_alias       ; A longer name associated with the host"
echo "   address         $var_address     ; IP address of the host"
echo "   }") -i -- windows.cfg

Execution:

 ./sed.sh 
host_name :
allan.com
alias :
allan
address :
123.123.123.123

output:

 cat windows.cfg 
#some settings
#more settings
test
abc

lot of stuff

test2

backup

 address of the host }
define host{
   use             windows-server  ; Inherit default values from a template
   host_name       allan.com       ; The name we are giving to this host
   alias           allan       ; A longer name associated with the host
   address         123.123.123.123     ; IP address of the host
   }

Explanations:

Instead of using a I have decided to go for r command in sed (to read the content of a file in order to insert it). Then I use the notation <() to have the output of all the echo commands manipulated by sed as if it was a file. This allow to avoid ambiguities for sed with { char and this gives better readability.

Process substitution: http://tldp.org/LDP/abs/html/process-sub.html

Allan
  • 12,117
  • 3
  • 27
  • 51