尝试分配给非变量(错误标记为“= 0”)

尝试分配给非变量(错误标记为“= 0”)

对此非常陌生,在这里我感到很困惑。非常感谢您的帮助。这是我的错误:

/home/rdmorgan0001/bin/dfchkr1.sh: line 12: -1922376 = 0 : attempted assignment to non-variable (error token is "= 0 ")
[rdmorgan0001@cset2 bin]$ 

这是我的脚本-

#!/bin/bash
#
#
#You need to create a dflog1.txt file from the /dev/sda1 folder before running this script...df | grep "home2" | awk '{print $3}' > dflog1.txt
#
#
x=($(cat dflog1.txt))                          #x will be the files size found in dflog1.txt which is our initial snapshot.
y=($(df | grep "home2" | awk '{print $3}'))                      #y will equal the current disk usage
z=100
echo $(( $x-$y )) > xy/xy.txt                     #make a file called xy.txt in the folder xy
w=($(cat xy/xy.txt))                         #w will now be equal to the number contained in xy.txt                   
if $(( $w = 0 ))                              #if w = 0, then there were no changes.
 then
   echo "There are no changes greater than 100MB at this time."
      exit
elif
   $(( $w != 0 ))                         #if it's not equal to zero then there were changes
  then
   $(( $w -lt 0 ))                           #if the change represented by w is a negative number
     $((  -1 * $w )) > absolute/wabs.txt     #then multiply it by -1 to get the absolute value of w
       a=($(cat absolute/wabs.txt))            #a is now equal to the absolute value of w
elif
   $(( $a -ge 100 ))
      then echo $w > dfchanges/dfchanges1$(date "+%d%m%y%H:%M").txt
        echo "Changes greater than 100MB have been detected.  Check the dfchanges1(date).txt file."
         df | grep "home2" > dflog1.txt       #remake our base comparison file since there were changes.
          exit
elif
   $(( $w -ge 100 ))  #changes greater than 100
    then echo $w > dfchanges/dfchanges1$(date "+%d%m%y%H:%M").txt
      echo "Changes great thatn 100MB have been detected.  Check the dfchanges1(date).txt file for more info"
        df | grep "home2" > dflog1.txt     #remake our base comparison file since there were chagnes.
elif
   $(( $a -lt $z ))                                 #if it's less than 100 we will disregard it in the next line
       then
         echo "There are no changes greater than 100MB at this time."
             exit
fi

答案1

(( ... ))算术评估中,=赋值运算符不是一个逻辑比较运算符。$(($w = 0))取消引用变量w,然后尝试将值分配0给其价值

您的意图可能是if $(($w == 0))。但是 - 尽管语法正确 - 参数扩展语法$w在此上下文中不是必需的,因此您可以将其简化为if ((w == 0)),对于$(( $w != 0 ))等也类似。从ARITHMETIC EVALUATION部分man bash

Shell  variables  are  allowed as operands; parameter expansion is per‐
formed before the expression is evaluated.  Within an expression, shell
variables  may  also  be referenced by name without using the parameter
expansion syntax.

还要注意-le-gt运算符用于在括号内进行算术比较[ ... ][[ ... ]]测试括号;在(( ... ))括号内(用于算术评估仅有的),则应使用<=>等等。

相关内容