0

I have to following lines:

execute "hostname -f >> /etc/mail/local-host-names" do command "hostname -f >> /etc/mail/local-host-names" end

I would like to add a condition, in case the hostname already exists in the file and the command should not add again the hostname in the file.

The /etc/mail/local-host-names might contain other hostname, overwriting is not a solution.

Thank you!

VGM
  • 69
  • 14

1 Answers1

0

You should use a bash script and the grep command. Grep command returns 0 if the name was found, 1 if it is not found. (# declares a comment in bash). In addition you must set the variable $HOSTNAME, to the desired hostname you want to look for each time

#!bin/sh


if grep -Fxq "$HOSTNAME" /etc/mail/local-host-names
then
    # execute "hostname -f >> /etc/mail/local-host-names" do command "hostname -f >> /etc/mail/local-host-names" end
else
    #do nothing?
fi
sestus
  • 1,875
  • 2
  • 16
  • 16
  • thank you, sestus. but this is not a bash script. it is very useful, but less for me now. The lines have to respect cookbook recipe syntax. – VGM Jun 19 '14 at 10:46