gpt4 book ai didi

linux - 在 bash 脚本中使用 IF 条件比较整数

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:51:21 26 4
gpt4 key购买 nike

我正在 linux 中编写一些 bash 脚本,我只想比较两个数字。一个是磁盘大小,另一个是限制。我使用 linux cmd 获取磁盘大小并将其存储在变量中,如下所示,

declare -i output    
output= df -h | grep /beep/data| awk '{ printf ("%d",$5)}'
echo "$output" # Got 80 here

limit = 80


if [ $output -eq $limit ];
then
fi

运行时出现以下错误:

line 27: [: -eq: unary operator expected"

最佳答案

output= df -h | grep /beep/data| awk '{ printf ("%d",$5)}'    

应该是

output="$(df -h | grep /beep/data| awk '{ printf ("%d",$5)}')"
#Used command substitution in the previous step

还有

limit = 80

应该是

limit=80 # no spaces around =, check all your variables for this

旁注:检查[ command substitution ]并使用 [ shellcheck ]检查脚本问题

关于linux - 在 bash 脚本中使用 IF 条件比较整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38388944/

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