---
title: "朋友问 if [ \"$?\" != \"0\" ]; then 是个啥 "
author: "qvb3d"
date: "2012-04-20T10:19:03Z"
tags: []
categories: ["编程语言"]
slug: "if-0-then"
draft: false
lastmod: "2012-04-20T10:19:03Z"
id: 1491144
url: "https://qvb3d.iteye.com/blog/1491144"
---

刚刚有个朋友来问

说脚本中的是什么意思

 

./setboard

if \[ "$?" != "0" ]; then 

　　echo "Error run Environment"

　　exit 1

fi

 

系统学习真的有必要,呵呵

 

我们的系统都有一个

 

Expands  to  the status of the most recently executed foreground pipeline.

 

扩展到最后执行的前台状态管道

 

也说就是

 

我C语言main有一个return

 

shell 也有一个 exit 

 

脚本中的　$?　就是上一次脚本调用的return或是exit 的返回值

 

比如

如果你的程序

 

setboard.c

 

#include &lt;stdio.h&gt;

int main(int argc,char \*argv\[])

{

printf("\\nDemo\\n");

return 0;

} 　

当然我的程序不可能这么少

这里只是举给朋友们看

这里return的是0

那么

gcc setboard.c -o setboard

 

上面的脚本不会出错提示了

但是我在程序中

加了一些检查主板,等处理

如果不符合我的要求

我就会用 return　１

那么,我下面的脚本就会有错误了

 

同理如果在脚本中使用

exit 1 或是 exit 0

也是同样效果