Linux/Shell 判断语句unary operator expected报错
实现目的是为了写一个简单的储存利用率超过阈值的报警小脚本
但是出现多次报错
disk_check.sh: line 6: [: -ge: unary operator expected
我的VIM源码是这样的
#! checkdiskfree alert script alert_num=10 percent=df -h |grep "^/dev/" |grep -oE "[0-9]+%"|grep -oE "[0-9]+"|sort -nr| head -n 1echo $alert_num echo $percent [ $percent -ge $alert_num ] && echo disk will be full
假设磁盘超过10%会报警
percent 是用正则表达式Grep抓出来的数字如17
但是我发现运行脚本一直报错
一开始以为是空格的原因,[ $percent -ge $alert_num ]
实际问题出现在$percent参数echo出来是空的
问题在于我在定义percent参数时没有吧输出字符串化,于是加上两个反引号就解决了
percent=`df -h |grep "^/dev/" |grep -oE "[0-9]+%"|grep -oE "[0-9]+"|sort -nr| head -n 1`
这个报错就是[]内变量缺失或者是空格不整齐导致的,切记比较的变量的类型要一致且不为空
[root@jycCentos1 data]# bash disk_check.sh 10 17 disk will be full
修改后成功运行