shell 脚本里的$?

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
badboych22
帖子: 200
注册时间: 2007-09-11 15:15

shell 脚本里的$?

#1

帖子 badboych22 » 2010-07-29 23:41

shell脚本里的$?网上查了是说关于程序执行后返回的结果
但不知道是什么结果,是指错误结果,还是正常执行结果,
假如脚本里有一个ping命令,用$?怎么知道有没有ping通
请大家指教一下,谢谢!
头像
fanhe
帖子: 2357
注册时间: 2007-03-24 23:45

Re: shell 脚本里的$?

#2

帖子 fanhe » 2010-07-30 0:14

就类似c的函数返回值之类的
不过这个$?保存的是上一个命令的返回值

代码: 全选

Fan:~/Desktop$ping -c 3 www.baidu.com
PING www.a.shifen.com (202.108.22.5) 56(84) bytes of data.
64 bytes from xd-22-5-a8.bta.net.cn (202.108.22.5): icmp_seq=1 ttl=55 time=49.3 ms
64 bytes from xd-22-5-a8.bta.net.cn (202.108.22.5): icmp_seq=2 ttl=55 time=48.0 ms
64 bytes from xd-22-5-a8.bta.net.cn (202.108.22.5): icmp_seq=3 ttl=55 time=48.4 ms

--- www.a.shifen.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2001ms
rtt min/avg/max/mdev = 48.054/48.625/49.381/0.585 ms
Fan:~/Desktop$echo $?
0
Fan:~/Desktop$ping -c 3 www.baidu.comm
PING www.baidu.comm (220.250.64.24) 56(84) bytes of data.

--- www.baidu.comm ping statistics ---
3 packets transmitted, 0 received, 100% packet loss, time 2015ms

Fan:~/Desktop$echo $?
1
tusooa
帖子: 6548
注册时间: 2008-10-31 22:12
系统: 践兔
联系:

Re: shell 脚本里的$?

#3

帖子 tusooa » 2010-07-30 0:36

代码: 全选

tlcr: 0 庚寅年六月十九日 00:31:55 ~
>> ls
Apps  Dropbox  Private  Share  个人  共享  应用
tlcr: 0 庚寅年六月十九日 00:31:59 ~
>> 
就是这个里面的,tlcr(The Last Command Returned)
如果命令执行正确,$? == 0,否则$? != 0

代码: 全选

] ls -ld //
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: shell 脚本里的$?

#4

帖子 eexpress » 2010-07-30 9:07

不同的命令,返回可以看man
● 鸣学
头像
badboych22
帖子: 200
注册时间: 2007-09-11 15:15

Re: shell 脚本里的$?

#5

帖子 badboych22 » 2010-07-30 10:09

谢谢了大家了!
回复