0

So I need to write a script to automate calculations done by a few different softwares. The overall script works, whereby in the attached scripts, the top one works in my overall script.

The problem is that in the first calculation, if the calculation works, it prints out a file called "Test.FChk" as well as "run.log". If the calculation fails, it only prints "run.log" and not the "Test.FChk". So currently what happens is that my current script tests for the "Test.FChk", but if the calculation fails it stays in an infinite loop, so I want the script to test that if the run.log file is found, but not the Test.FChk, the script ends but if it finds both, it proceeds to the next part of the script, as well as whilst the calculation is going, waiting for the files to be created, it prints out that its "checking job status"

###script that works###
while [ ! -f "Test.FChk" ]
do
    echo "Checking job status of Gaussian: $JOBNAME..."
    sleep 5
done

###script that doesnt work###
while true; do
    if [[ ! -f "Test.FChk" && -f "run.log" ]]; then
        echo "Checking job status..."
        sleep 5
        printf "Calculation failed, script ended"
        return
    fi
    break
done
3
  • Seems to me that you are not controlling the condition but if it finds both anywhere. Commented Jun 16, 2019 at 19:58
  • elif [[ -f "Test.FChk" && -f "run.log" ]]; then break Commented Jun 16, 2019 at 20:06
  • @guillermo chamorro sorry but where do I add that? Im still learning bash so I'm a little confused Commented Jun 16, 2019 at 20:49

2 Answers 2

0

You have four possibilities: file a exists but file b does not, the inverse, both files exist, or neither do. You just controlled the first possibility, but not the other three. But as you stated, you want to do something if both files exist, so you have to control it.

 while true; do
    if [[ ! -f "Test.FChk" && -f "run.log" ]]; then
        echo "Checking job status..."
        sleep 5
        printf "Calculation failed, script ended"
        return
    # here you control if both files exist
    elif [[ -f "Test.FChk" && -f "run.log" ]]; then 
        break # break the loop, the next code will continue
    fi 
    ### this will just stop the loop at the first iteration!
    break ### not something you need
    ###
done
0
0

This solution keeps running until at least run.log is present and exits the script if Test.FChk is missing. If both files are present the script continues.

#!/bin/bash

while true; do
    if [ -f "run.log" ]; then
        if [ -f "Test.FChk" ]; then
            # calculation finished, break loop
            break
        fi
        echo "Calculation failed, exiting." >&2
        exit 1
    fi
    echo "Checking job status..."
    sleep 5
done

echo "Calculation successfully finished!"
# do something

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .