gpt4 book ai didi

linux - 检索可用内存时 IF 语句上的 Bash 整数表达式

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:07:46 24 4
gpt4 key购买 nike

运行脚本 memory.sh 脚本时出现以下错误:

[root@test tmp]# ./memory.sh
./memory.sh: line 3: [: 2.05028: integer expression expected
Normal

memory.sh脚本的内容是:

[root@test tmp]# cat memory.sh
threshold=80
MEMORY=$(free | grep Mem | awk '{print $3/$2 * 100.0}')
if [ ${MEMORY} -gt ${threshold} ]; then
sudo sync;echo 3 > /proc/sys/vm/drop_caches
else
echo "Normal"
fi

有谁知道如何防止这个错误?

最佳答案

从中检索浮点值时出现 IF 语句错误内存。

以下脚本将浮点内存值转换为整数以进行比较:

#!/bin/bash

threshold=80
memory=$(free | grep Mem | awk '{print $3/$2 * 100.0}')
castedMemory=$(echo $memory | cut -d'.' -f1)
if [[ "$castedMemory" -gt $threshold ]]; then
sudo sync
echo 3 > /proc/sys/vm/drop_caches
else
echo "Normal"
fi

关于linux - 检索可用内存时 IF 语句上的 Bash 整数表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43135801/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com