9 个解决方案
#1
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
#2
$?表示上一次程序退出值,你可以自己写一个1.sh脚本如下:
exit 2
然后在命令行下执行该脚本,最后在shell中输入echo $?看看效果
exit 2
然后在命令行下执行该脚本,最后在shell中输入echo $?看看效果
#3
其实就是上一次程序或脚本的退出码,主要是用来判断程序或脚本的执行是否成功,检查出错的原因之类的
#4
获取进程的退出状态及接收的信号值。
#5
$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.
#6
呵呵,赞.
#7
up
#8
呵呵,就是上一次命令的退出状态。成功值为0,不成功为非0!
#9
这是SHELL的一部分,意思上面有提及。
一般情况下,在linux/unix下编程时,我们会在应用程序退出时返回一个整型数据(在main函数中通过return/exit,或者别的函数/方法中exit)。这个返回值是给SHELL使用的,因为我们可能需要了解应用程序的执行状态,然后根据不同的执行状态去执行不同的分支(类似于函数的返回值)。
一般情况下,在linux/unix下编程时,我们会在应用程序退出时返回一个整型数据(在main函数中通过return/exit,或者别的函数/方法中exit)。这个返回值是给SHELL使用的,因为我们可能需要了解应用程序的执行状态,然后根据不同的执行状态去执行不同的分支(类似于函数的返回值)。
#1
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
#2
$?表示上一次程序退出值,你可以自己写一个1.sh脚本如下:
exit 2
然后在命令行下执行该脚本,最后在shell中输入echo $?看看效果
exit 2
然后在命令行下执行该脚本,最后在shell中输入echo $?看看效果
#3
其实就是上一次程序或脚本的退出码,主要是用来判断程序或脚本的执行是否成功,检查出错的原因之类的
#4
获取进程的退出状态及接收的信号值。
#5
$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.
#6
呵呵,赞.
#7
up
#8
呵呵,就是上一次命令的退出状态。成功值为0,不成功为非0!
#9
这是SHELL的一部分,意思上面有提及。
一般情况下,在linux/unix下编程时,我们会在应用程序退出时返回一个整型数据(在main函数中通过return/exit,或者别的函数/方法中exit)。这个返回值是给SHELL使用的,因为我们可能需要了解应用程序的执行状态,然后根据不同的执行状态去执行不同的分支(类似于函数的返回值)。
一般情况下,在linux/unix下编程时,我们会在应用程序退出时返回一个整型数据(在main函数中通过return/exit,或者别的函数/方法中exit)。这个返回值是给SHELL使用的,因为我们可能需要了解应用程序的执行状态,然后根据不同的执行状态去执行不同的分支(类似于函数的返回值)。