Linux shell:条件语句中的空格

时间:2017-07-25 12:28:07

标签: linux shell command-line

我最近学习了如何在shell中编程。 我无法理解为什么这两个陈述产生不同的输出。似乎没有空格,test将10==11视为字符串并始终返回true。

$test 10==11 && echo yes || echo no
$yes
$test 10 == 11 && echo yes || echo no
$no

1 个答案:

答案 0 :(得分:0)

public class HomeController : Controller
{
    public ActionResult Index()
    {
        ControllerContext.HttpContext.Response.Cookies.Add(
             new HttpCookie("test", "hello") { Path = @"/Admin", 
             Expires = DateTime.Now.AddDays(1)});

        return RedirectToAction("About", "Admin");
    }
}

public class AdminController : Controller
{
    public ActionResult About()
    {
        var cookieCount = HttpContext.Request.Cookies.Count;
        return View();
    }
}

Read More Here

<强>当量

# single string, not null so true, and result is yes
test 10==11 && echo yes || echo no

# there exist space 10 == 11 not equal string comparison so result no
test 10 == 11 && echo yes || echo no

来自http://tldp.org/LDP/abs/html/comparison-ops.html

if test 10==11; then echo yes; else echo no; fi
yes

if test 10 == 11; then echo yes; else echo no; fi
no

# or this same as above
if test 10 = 11; then echo yes; else echo no; fi
no