0

For some reason my if condition for a file and a directory not existing is not processing correctly. The script thinks that the file and directory exist when they do not.

#!/usr/bin/bash
lockdir=/opt/someOtherDis/qa_test_lock
puppetlock=/var/opt/puppetLockDirs/agent_catalog_run.lock

if [ ! -f "$puppetlock" ] && [ ! -d "$lockdir"]; then
    #Get a url and grep it to get stuff
else
     su -c "echo ls puppetlock results: $(ls -lrt $puppetlock) and qa lock results: $(ls -lrt $lockdir) >> /var/log/dirStuff/test-qa-run.log" -m "someUser"
    su -c "echo $(date +\"%m-%d-%T\") Puppet or QA tests are running. Did not attempt to update artifact version. >> /var/log/dirStuff/test-qa-run.log" -m "someUser"
fi

I added the output of the ls to make sure I can see if puppet or the qa lock exists.

Here is the output of the log file, first check was when I run puppet manually so I could make sure things worked.

02-09-08:16:01 Puppet or QA tests are running. Did not attempt to update artifact version. ls puppetlock results: -rw-r--r-- 1 root root 5 Feb 9 08:27 /var/opt/lib/pe-puppet/state/agent_catalog_run.lock and qa lock results:

02-09-08:28:01 Puppet or QA tests are running. Did not attempt to update artifact version.

02-09-08:30:02 app_versions.json has not been modified in the last 30 minutes ls puppetlock results: and qa lock results:

02-09-08:30:02 Puppet or QA tests are running. Did not attempt to update artifact version. ls puppetlock results: and qa lock results:

02-09-08:32:01 Puppet or QA tests are running. Did not attempt to update artifact version.

2
  • 4
    There should be a space between "$lockdir" and ], no? Commented Feb 9, 2017 at 14:46
  • @AlexP You are correct. No wonder I could not see it. Issue is fixed Commented Feb 9, 2017 at 15:52

1 Answer 1

1

Hmm, seems like you meant to write:

if [ ! -f "$puppetlock" ] || [ ! -d "$lockdir" ]; then

Which (IMO) is more clearly written:

if ! test -f "$puppetlock" -a -d "$lockdir"; then

or

if ! { test -f "$puppetlock" && test -d "$lockdir"; } then 

( Note that !a || !b is equivalent to !(a && b), but not the same as !a && !b)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.