Press "Enter" to skip to content

shell之if语句常见错误

以下是一个错误的if语句

#!/bin/bash
oldversion=b3-cdrom
function test()
{
    if [ ! `df -h | grep $oldversion` ]; then
        echo found.
    fi
}
test

以下都是正确的:

#!/bin/bash
oldversion=b3-cdrom
function test()
{
    if [ ! "`df -h | grep $oldversion`" ]; then
        echo found.
    fi
}
test

加双引号引起来就正确了……

下面这个也是正确的:

#!/bin/bash
oldversion=3
function test()
{
    if [ ! $oldversion ]; then
        echo found.
    fi
}
test

One Comment

Leave a Reply to ipcpu Cancel reply

Your email address will not be published. Required fields are marked *