如何修复此脚本以获得正确的输出?

时间:2019-07-04 03:36:28

标签: linux bash shell if-statement

我正在尝试使用Shell脚本将/etc/sysctl.conf的实际值与建议值进行比较。这两个值相同,但是if语句正在执行,而不是else

shmmnireq=4096
shmmni=$(grep "shmmni" /etc/sysctl.conf | cut -d " " -f3)
echo $shmmnireq
echo $shmmni
if [[ $shmmnireq  == $shmmni ]];then
    echo "shmmni is configured"
else
    echo "shmmni is not configured"
fi

输出:

4096
4096
shmmni is not configured

2 个答案:

答案 0 :(得分:1)

尝试一下-

#!/bin/bash
shmmnireq=4096
shmmni=$(grep "shmmni" /etc/sysctl.conf | cut -d " " -f3)    
echo $shmmnireq
echo $shmmni
if [ "$shmmnireq" -eq "$shmmni" ];then
    echo "shmmni is configured"
else
    echo "shmmni is not configured"
fi

答案 1 :(得分:0)

我已经测试了以下代码。这将起作用:

#!/bin/bash

shmmnireq=4096
shmmni=$(grep "shmmni" /etc/sysctl.conf | cut -d " " -f3)
echo $shmmnireq
echo $shmmni
if [[ ${shmmnireq}  -eq ${shmmni} ]]
then
    echo "shmmni is configured"
else
    echo "shmmni is not configured"
fi