Bash 带有多个条件的 if 语句脚本
Bash script if statement with multiple conditions
我正在尝试为 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'
问题似乎出在这里:
[[ ! (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
我正在尝试为 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'
问题似乎出在这里:
[[ ! (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