DS-V
DS-V

Reputation: 123

My code keeps getting stuck in this while loop stating directory does not exist when it does, why is this?

I have the following code that is designed to carry out a simulation. Once the simulation is finished it will remove text from the output file and use the new edited output file as the input for a new simulation. What happens is I get stuck in a while loop regarding the existence of the GROPATH file. I get the following loop printed to the log file:

Directory path exists
GROPATH does not exist
waiting sleep 10800
GROPATH does not exist
waiting sleep 10800
GROPATH does not exist
waiting sleep 10800
GROPATH does not exist
waiting sleep 10800
GROPATH does not exist
waiting sleep 10800
GROPATH does not exist
waiting sleep 10800
GROPATH does not exist
waiting sleep 10800
GROPATH does not exist
waiting sleep 10800
GROPATH does not exist
waiting sleep 10800
GROPATH does not exist
waiting sleep 10800
GROPATH does not exist
waiting sleep 10800
GROPATH does not exist
waiting sleep 10800
GROPATH does not exist
...

it continually goes on like this.

I'm confused as the GROPATH does exist I can see the exact file in the directory. What am I doing wrong that is causing this endless loop? Should I use an if loop instead? I've checking my filepath mapping and it seems correct to me.

#!/bin/bash


#set value of i
i=`k=$(ls md*gro -v1 | tail -n1) ; echo ${k//.gro/} | sed "s/md//"`

# while more than 20 waters ...
while (grep -c SOL  md$i.gro > 20); do

    DIRECTORY=~/scratch/desolvation/iteration_$i
    GROPATH=~/scratch/desolvation/iteration_$i/md$i.gro
    ERRORPATH=~/scratch/desolvation/log_gpu.dat

#does the directory exist?
        if [ -d "$DIRECTORY" ]; then
                echo "Directory path exists"

# does gro file exist?
                while [ ! -d "$GROPATH" ]; do
                        echo "GROPATH does not exist"
                        if grep -q 'error' "$ERRORPATH"; then
                                echo "error in gromacs"; exit
                        else echo "waiting" sleep 10800
                        fi
                done

I expect the code to recognise the file does exist and move on to the next part of the program (which I have not included inorder to stay concise) which edits the GROPATH file so it can be used as input for the next simulation.

Upvotes: 0

Views: 149

Answers (1)

Gordon Davisson
Gordon Davisson

Reputation: 125798

The test [ ! -d "$GROPATH" ] checks for a directory by that name, not a file. To check for a file, use -f instead of -d (or just use -e, which'll check for anything by that name).

Other problems: while (grep -c SOL md$i.gro > 20); do doesn't do what you want; the () create a subshell which runs the grep command with its output redirected to a file named "20". Use while [ "$(grep -c SOL md$i.gro)" -gt 20 ]; do instead.

Also, you have echo "waiting" sleep 10800 as a single command, so it prints the "sleep 10800" instead of executing it as a command; just move it to a separate line.

And the way you select i is a mess; try this instead:

i=$(printf '%s\n' md*.gro | sed 's/^md//; s/[.]gro$//' | sort -rg | head -n1)

General suggestion: run your scripts through shellcheck.net to spot common problems.

Upvotes: 2

Related Questions