Integer Expression expected; error when moving files

Viewed 549

To give context to my issue. I am trying to move a list of files, where if the resolution is greater than 3.00, then move to a directory, Above3.

#!/bin/bash

files=`ls *.ent`
Above3=/mnt/d/Documents/Research/PhD/PhD/Research/Results/PDB/New/XRD/Above3
for f in $files; do
        resolution=`cat $f | grep "REMARK   2 RESOLUTION." | awk '{print $4}' | tr -d ' '`
        if [ $resolution -gt 3.00 ] ; then
        mv $f $Above3
        fi
done

The error I am getting is "integer expression expected" which I am not sure how to fix. Any suggestions on a solution will be greatly appreciated.

1 Answers

3.00 is a float value. Bash cannot process floating point values, only integers, hence the error message.

You could treat it as a string and compare it alphabetically if you can get your number in a fixed format (e.g. with two digits after the decimal point).

But I'd rather advise you use the command bc which is specifically made for calculations:

if [ $(bc <<< "$resolution > 3.00") == 1 ] ; then
    mv $f $Above3
fi

What does the script (as a summary):

What does the script?

  • it takes all the files corresponding to *.ent in the current directory

  • it takes all the lines containing "REMARK 2 RESOLUTION." from all these files ; it takes the 4th word of these lines ; and puts the result into bash variable $resolution

  • if $resolution is greater than 3.00 it moves the corresponding file (one of the *.ent files) into the directory /mnt/d/Documents/Research/PhD/PhD/Research/Results/PDB/New/XRD/Above3

  • then it loops to proceed with the next file whose name matches *.ent

Related