当前位置: 技术问答>linux和unix
Linux中$?是什么意思?
来源: 互联网 发布时间:2016-04-14
本文导语: 最近在看Linux Shell,一直搞不懂$?是什么,请高手赐教。拜谢! | $n $1 the first parameter,$2 the second... $# The number of command-line parameters. $0 The name of current ...
最近在看Linux Shell,一直搞不懂$?是什么,请高手赐教。拜谢!
|
$n $1 the first parameter,$2 the second...
$# The number of command-line parameters.
$0 The name of current program.
$? Last command or function's return value.
$$ The program's PID.
$! Last program's PID.
$@ Save all the parameters.
almost any shell book will talk about them,from which you can get their detail usages.
$# The number of command-line parameters.
$0 The name of current program.
$? Last command or function's return value.
$$ The program's PID.
$! Last program's PID.
$@ Save all the parameters.
almost any shell book will talk about them,from which you can get their detail usages.
|
$?表示上一次程序退出值,你可以自己写一个1.sh脚本如下:
exit 2
然后在命令行下执行该脚本,最后在shell中输入echo $?看看效果
exit 2
然后在命令行下执行该脚本,最后在shell中输入echo $?看看效果
|
其实就是上一次程序或脚本的退出码,主要是用来判断程序或脚本的执行是否成功,检查出错的原因之类的
|
这是SHELL的一部分,意思上面有提及。
一般情况下,在linux/unix下编程时,我们会在应用程序退出时返回一个整型数据(在main函数中通过return/exit,或者别的函数/方法中exit)。这个返回值是给SHELL使用的,因为我们可能需要了解应用程序的执行状态,然后根据不同的执行状态去执行不同的分支(类似于函数的返回值)。
一般情况下,在linux/unix下编程时,我们会在应用程序退出时返回一个整型数据(在main函数中通过return/exit,或者别的函数/方法中exit)。这个返回值是给SHELL使用的,因为我们可能需要了解应用程序的执行状态,然后根据不同的执行状态去执行不同的分支(类似于函数的返回值)。
|
Before we move on, there is a perversity about tests in Bash shells that I want to discuss. It turns out, because of a historical accident that now might as well be cast in concrete, when a test is conducted or a command returns a result value, the numerical value for "true" is 0, and "false" is 1. Those of you who have some programming experience will likely find this reversal of intuition as annoying as I do.
Here is a way to get the result of the most recent logical test (and to show the weird reversal described above):
$ test -e .
$ echo $?
0
$ test -e xyz
$ echo $?
1
Here is a way to get the result of the most recent logical test (and to show the weird reversal described above):
$ test -e .
$ echo $?
0
$ test -e xyz
$ echo $?
1
|
获取进程的退出状态及接收的信号值。
|
呵呵,就是上一次命令的退出状态。成功值为0,不成功为非0!