1

I'm trying to create a simple bash shell script provisioner for a vagrant puppet dev env which checks that /tmp/puppet-modules-up-to-date exists and was created within a certain timeframe, 14 days to be precise. I'm struggling to progress with this simple script. Any help appreciated.

#!/bin/bash

if [[ ! -f /tmp/.puppet-modules-up-to-date ]] && [[ ! (find /tmp/.puppet-modules-up-to-date -mtime -14 | grep puppet) ]]; then

    puppet_dir=/etc/puppet

    echo 'Copying Puppetfile into place'
    cp /vagrant/puppet/Puppetfile $puppet_dir

    cd $puppet_dir

    echo 'Installing puppet modules into place'
    librarian-puppet install

    echo 'Updating previously installed puppet modules'
    librarian-puppet update 

    touch /tmp/.puppet-modules-up-to-date
fi

Simply gives me the following error:

==> default: /tmp/vagrant-shell: line 3: conditional binary operator expected
==> default: /tmp/vagrant-shell: line 3: expected `)'
==> default: /tmp/vagrant-shell: line 3: syntax error near `/tmp/.puppet-modules-up-to-date'
==> default: /tmp/vagrant-shell: line 3: `if [[ ! -f /tmp/.puppet-modules-up-to-date ]] && [[ ! (find /tmp/.puppet-modules-up-to-date -mtime -14 | grep puppet) ]]; then'
Agent Rich
  • 57
  • 6

1 Answers1

1

Problem seems to be here:

[[ ! (find /tmp/.puppet-modules-up-to-date -mtime -14 | grep puppet) ]];

Since this is not really catching output of find/grep which you could do using $(find...) command.

However you can handle this condition better using grep -q:

find /tmp/.puppet-modules-up-to-date -mtime -14 | grep -vq puppet
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Ah yes missed the dollar sign oops... Good solution with inverting the output of grep by the way. Like it. – Agent Rich Mar 09 '15 at 15:53
  • Thinking about it I can strip the entire test for the existence of the file as find will pick this up anyway right – Agent Rich Mar 09 '15 at 15:54
  • Probably `find` can be used as `find /tmp/.puppet-modules-up-to-date -mtime -14 ! -name '*puppet*' – anubhava Mar 09 '15 at 15:55