سؤال

I have a script that will find the distances between two atoms in pdb.

bash does not recognize decimals so I have put printf script to round the decimals.

and echo $b works fine and gives me a integer value.

but the if line for my filtering system does not work.

I get and error stating

 [: -ge: unary operator expected

below is part of the script that I am working on.

 a=$(awk '$2=='91'{x1=$6;y1=$7;z1=$8} $2=='180' {x2=$6;y2=$7;z2=$8} END{print sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) + (z1-z2)*(z1-z2))}' ${names}.pdb.$i)
b= printf %.0f $a
echo $b
if [ $b -ge 1 ] &&[ $b -le 9 ]; then

any help will be greatly appreciated. Thank you in advanced.

هل كانت مفيدة؟

المحلول

b= printf %.0f $a

This line sets the value of b to nothing for the duration of the printf command, which sends its output to stdout

echo $b

This prints a blank line.

You must not put whitespace around the = in an assignment, and to store the output of a command into a variable, you use this syntax:

b=$( printf %.0f $a )

You're getting the error because $b is empty, and this is what bash sees:

if [  -ge 1 ] &&[  -le 9 ]; then

-ge is expecting operands on both the left and the right, and it doesn't see one.

With bash, you should (almost) always prefer [[ ... ]] over [ ... ] -- the double bracket form is not fooled by variables containing empty strings.

You should always quote your "$variables" -- unless you know exactly when to not quote them.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top