当前位置: 动力学知识库 > 问答 > 编程问答 >

vagrant - Bash script if statement with multiple conditions

问题描述:

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'

网友答案:

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
分享给朋友:
您可能感兴趣的文章:
随机阅读: