【问题标题】:[: missing `]' error in Unix | shell Script [closed][: missing `]' error in Unix | shell Script [closed]
【发布时间】:2020-06-24 18:28:35
【问题描述】:

我在 shell 脚本中遇到 [: missing `]' 错误

memuse=$ free -m                                                                      
if [ $memuse >80]
then                                                               
echo "Attention: memory utilisation is high on $(hostname) at $(date)" 

这是我的代码,我得到了类似的错误

[tono@webminal.org ~]$sh ./memory_alert.sh                                   
              total        used        free      shared  buff/cache   available       
Mem:          14522        2128        4004        1845        8389       10096       
Swap:             0           0           0                                           
./memory_alert.sh: line 2: [: missing `]' 

【问题讨论】:

  • 使用此链接进行调试:shellcheck.net
  • 一个其他人没有提到的问题是你在[ ... ]中没有使用>进行数值比较。如果$memuse 包含一个整数值(它肯定没有),则比较将是if [ "$memuse" -gt 80 ]
  • 你的代码有很多问题,但最关键的引发错误的是]之前缺少空格。尽管修复 5hat 仍然不会使您的代码运行。见@user123 的评论
  • @TonoKuriakose :仅供参考(正如其他人已经指出您程序中的错误):您的if 语句实际上意味着运行命令test $memuse >80],即使用扩展值$memuse 作为test 的参数,然后将test 的标准输出写入名为80] 的文件中。我猜你会在脚本的工作目录中找到同名的空文件。
  • 这能回答你的问题吗? Bash scripting missing ']'

标签: linux bash shell unix


【解决方案1】:

嗯,有很多问题。首先,要将某些命令的输出输出到 var,您需要这个

var=$( some_command )

第二,free -m的输出是

              total        used        free      shared  buff/cache   available       
Mem:          14522        2128        4004        1845        8389       10096       
Swap:             0           0           0

怎么能和80比呢?看看你要比较什么:

"total used free shared buff/cache availableMem: 14522 2128 4004 1845 8389 10096 Swap: 0 0 0" > 80

第三,free -m 以 MB 为单位显示内存信息,我假设您想要百分比,对吗? free 输出中没有这样的指标。但是基于“可用”和“总计”,您可以计算内存使用百分比,然后将其与某个预定义值进行比较。好的,让我们尝试解决这个问题。

#!/bin/bash

memory_alert=80 # this will be an alerting value
memory_total=$( free | sed -n '2p' | awk '{print $2}' ) # get total memory by parsing free output with sed and awk
memory_available=$( free | sed -n '2p' | awk '{print $7}' ) # same with available memory
memory_used=$( echo 100-100*$memory_available/$memory_total | bc ) # calculate memory usage in %

# compare and alert if memory usage more than 80%
(( memory_used > memory_alert )) && echo "Attention: memory utilisation is high on $(hostname) at $(date)"

多亏了 Gordon,更紧凑的变体

#!/bin/bash
memory_alert=80 # this will be an alerting value
memory_used=$( free | awk 'NR==2 {print int(100-100*$7/$2)}' )
(( memory_used > memory_alert )) && echo "Attention: memory utilisation is high on $(hostname) at $(date)"

【讨论】:

  • @KeithThompson,谢谢!)
  • 您可以让awk 为您完成几乎所有的工作:memory_used=$( free | awk 'NR==2 {print int(100-100*$7/$2)}' )
【解决方案2】:

您在memuse=$ free -m这一行似乎有问题

我了解到您想将free -m 命令的输出分配给变量“memuse”,但这是不正确的。而是使用 $(command)`command` 语法。 还为变量memuse 添加一个回显语句,只是为了检查你得到什么作为变量memuse 中命令free -m 的响应。这对你会有帮助。

memuse=$(free -m)
echo $memuse

Output

$bash -f main.sh
total used free shared buff/cache available Mem: 257692 3748 244035 3 9909 252827 Swap: 4095 0 4095

【讨论】:

  • 您应该将更正的代码添加到您的答案中以进行演示。
【解决方案3】:

第一个问题是memuse=$ free -m中的语法错误,应该是这样的:

memuse=$(free -mh)

其次,在if [ $memuse > 80 ] 中,您没有提到它是百分比或绝对已用内存值,如果您检查的是百分比,那么您需要使用百分比公式相应地计算它。

如果是绝对值,则必须使用 grep 等获取已用内存值并与 80 进行比较。

【讨论】:

    【解决方案4】:

    我找到了解决办法

    memuse=$ free -m                                                                      
    if [ $memuse > 80 ];                                                                  
    then                                                                                 
    echo "Attention: memory utilisation is high on $(hostname) at $(date)"               
    else                                                                                  
    echo "Attention : memory utilisation is normal on $(hostname) at $(date)"             
    fi 
    

    这是我第一次编写 bash 脚本。所以这个问题主要是由于缩进。

    【讨论】:

      【解决方案5】:

      你错过了spacefi

      memuse=$ free -m                                                                      
      if [ $memuse > 80 ];
      then                                                               
          echo "Attention: memory utilisation is high on $(hostname) at $(date)"
      fi
      

      【讨论】:

      • 你测试过你发布的代码吗?
      • 是的,我在本地测试过
      • 我会推荐给check your code here
      • 谢谢,您的回答给了我解决方案
      猜你喜欢
      • 2022-12-02
      • 2022-10-25
      • 2022-12-26
      • 1970-01-01
      • 1970-01-01
      • 2022-12-01
      • 2020-03-12
      • 1970-01-01
      • 2022-12-02
      相关资源
      最近更新 更多