I have three input files. I will read the first line of each the input file and I will compare them to a reference number. All values (a1,a2 and a3) I read from different input files should smaller than my "absolute reference value (aref)". My script gives the error for the reference value: line ..: -2.13: No such file or directory. How can I fix my code? And my if condition is correct?
我有三个输入文件。我将读取每个输入文件的第一行,我将它们与参考编号进行比较。从不同输入文件读取的所有值(a1,a2和a3)应小于我的“绝对参考值(aref)”。我的脚本给出了参考值的错误:line ..:-2.13:没有这样的文件或目录。我该如何修复我的代码?我的条件是否正确?
#!/bin/bash -f
a1=$(head -n 1 input1.xvg)
a2=$(head -n 1 input2.xvg)
a3=$(head -n 1 input3.xvg)
aref=-2.13
if [a1 < $aref && a2 < $aref && a3 < $aref] ; then
echo "It's good"
else
echo "It isn't good"
fi
2 个解决方案
#1
As well as your syntax errors (you need spaces after [
and before ]
in a test and you're missing some $
from your variable names), it looks like you're trying to compare floating point numbers, which is not supported by bash. One option would be to use bc
, inside an arithmetic context instead:
除了你的语法错误(在测试中[和之前]需要空格并且你从变量名中遗漏了一些$),看起来你正在尝试比较bash不支持的浮点数。 。一种选择是在算术上下文中使用bc:
if (( $(bc <<<"$a1 < $aref && $a2 > $aref && $a3 > $aref") )); then
echo "It's good"
else
echo "It isn't good"
fi
The output of the command substitution $( ... )
, is 1
or 0
, depending on whether the conditions are true or false. The arithmetic context (( ... ))
returns success if the output is returns 1 and failure if it returns 0.
命令替换$(...)的输出为1或0,具体取决于条件是true还是false。如果输出返回1,则算术上下文((...))返回成功,如果返回0则返回失败。
If you would like to work with the absolute values of your variables, one way to do so would be to use ${a1#-}
, ${aref#-}
, etc. which removes the leading -
from each variable.
如果你想使用变量的绝对值,一种方法是使用$ {a1# - },$ {aref# - }等来删除每个变量的前导值。
#2
Use [[ instead of [ in your if statement. Because you're using [ the shell thinks you are passing a file, not comparing to values.
使用[[而不是[在你的if语句中]。因为您正在使用[shell认为您正在传递文件,而不是与值进行比较。
Try to write your if like this
尝试写这样的if
if [[ $a1 < $aref && $a2 > $aref && $a3 > $aref ]] ; then
To use the absolute value of aref use this:
要使用aref的绝对值,请使用:
let "aref=$aref<0?-$aref:$aref"
#1
As well as your syntax errors (you need spaces after [
and before ]
in a test and you're missing some $
from your variable names), it looks like you're trying to compare floating point numbers, which is not supported by bash. One option would be to use bc
, inside an arithmetic context instead:
除了你的语法错误(在测试中[和之前]需要空格并且你从变量名中遗漏了一些$),看起来你正在尝试比较bash不支持的浮点数。 。一种选择是在算术上下文中使用bc:
if (( $(bc <<<"$a1 < $aref && $a2 > $aref && $a3 > $aref") )); then
echo "It's good"
else
echo "It isn't good"
fi
The output of the command substitution $( ... )
, is 1
or 0
, depending on whether the conditions are true or false. The arithmetic context (( ... ))
returns success if the output is returns 1 and failure if it returns 0.
命令替换$(...)的输出为1或0,具体取决于条件是true还是false。如果输出返回1,则算术上下文((...))返回成功,如果返回0则返回失败。
If you would like to work with the absolute values of your variables, one way to do so would be to use ${a1#-}
, ${aref#-}
, etc. which removes the leading -
from each variable.
如果你想使用变量的绝对值,一种方法是使用$ {a1# - },$ {aref# - }等来删除每个变量的前导值。
#2
Use [[ instead of [ in your if statement. Because you're using [ the shell thinks you are passing a file, not comparing to values.
使用[[而不是[在你的if语句中]。因为您正在使用[shell认为您正在传递文件,而不是与值进行比较。
Try to write your if like this
尝试写这样的if
if [[ $a1 < $aref && $a2 > $aref && $a3 > $aref ]] ; then
To use the absolute value of aref use this:
要使用aref的绝对值,请使用:
let "aref=$aref<0?-$aref:$aref"