Bash脚本if语句有多个条件

时间:2015-03-09 15:42:42

标签: bash vagrant puppet librarian

我正在尝试为vagrant puppet dev env创建一个简单的bash shell脚本配置器,它检查/ tmp / puppet-modules-up-to-date是否存在并且是在特定时间范围内创建的,准确地说是14天。我正在努力用这个简单的脚本进步。任何帮助表示赞赏。

#!/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

只是给我以下错误:

==> 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'

1 个答案:

答案 0 :(得分:1)

问题似乎在这里:

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

因为这并没有真正捕获find/grep的输出,你可以使用$(find...)命令。

但是,您可以使用grep -q更好地处理此情况:

find /tmp/.puppet-modules-up-to-date -mtime -14 | grep -vq puppet