免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 2731 | 回复: 10
打印 上一主题 下一主题

关于返回状态的疑问 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-03-02 15:16 |只看该作者 |倒序浏览
以前一直以为如果返回状态如果不是0的话,脚本就会退出。可是看到了shell函数的使用时,发现以前的理解不对。请高手指导。

#!/bin/bash
function hello {
        echo "123"
        return 1
}
hello
echo $?
echo "aaa"

输出:
123
1
aaa

在这里,就有个疑问,return返回的返回状态为1,按照我以前的理解脚本应该退出了,不应该再输出aaa了。可是现在无论返回状态值为多少,脚本还是会运行到最后。

那么我想问各位大侠,这个返回状态有什么用?查看上一条命令是否成功执行?还有,我常看到有些人写脚本的时候在退出时用上exit 1 这样的命令。那么这里的1又是什么东西啊?

本人新手,请大侠不要笑话。

论坛徽章:
9
2015亚冠之阿尔纳斯尔
日期:2015-09-10 16:21:162015亚冠之塔什干火车头
日期:2015-07-01 16:23:022015年亚洲杯之巴勒斯坦
日期:2015-04-20 17:19:46子鼠
日期:2014-11-13 09:51:26未羊
日期:2014-08-28 18:13:36技术图书徽章
日期:2014-02-21 09:30:15酉鸡
日期:2014-01-14 11:12:49天蝎座
日期:2013-12-09 17:56:53平安夜徽章
日期:2015-12-26 00:06:30
2 [报告]
发表于 2009-03-02 15:20 |只看该作者
没啥问题啊
要退出,用exit

论坛徽章:
0
3 [报告]
发表于 2009-03-02 15:24 |只看该作者
原帖由 HH106 于 2009-3-2 15:20 发表
没啥问题啊
要退出,用exit



那看到很多人退出都写exit 1  这样的命令退出啊。加上一个1 是别有意义,还是画蛇添足啊?

论坛徽章:
1
处女座
日期:2014-12-23 17:59:27
4 [报告]
发表于 2009-03-02 15:25 |只看该作者
exit [n]
              Cause the shell to exit with a status of n.  If n is omitted, the exit sta-
              tus is that of the last command executed.   A  trap  on  EXIT  is  executed
              before the shell terminates.

论坛徽章:
23
15-16赛季CBA联赛之吉林
日期:2017-12-21 16:39:27白羊座
日期:2014-10-27 11:14:37申猴
日期:2014-10-23 08:36:23金牛座
日期:2014-09-30 08:26:49午马
日期:2014-09-29 09:40:16射手座
日期:2014-11-25 08:56:112015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:0315-16赛季CBA联赛之山东
日期:2017-12-21 16:39:1915-16赛季CBA联赛之广东
日期:2016-01-19 13:33:372015亚冠之山东鲁能
日期:2015-10-13 09:39:062015亚冠之西悉尼流浪者
日期:2015-09-21 08:27:57
5 [报告]
发表于 2009-03-02 15:25 |只看该作者

回复 #1 wangduo112 的帖子

#!/bin/bash
function hello {
        echo "123"
        return 1
        echo "bbb"
}
hello
echo $?
echo "aaa"

return 是退出函数的,在后面加个echo "bbb"你再看会运行不。

论坛徽章:
0
6 [报告]
发表于 2009-03-02 15:26 |只看该作者
原帖由 wangduo112 于 2009-3-2 15:16 发表
以前一直以为如果返回状态如果不是0的话,脚本就会退出。可是看到了shell函数的使用时,发现以前的理解不对。请高手指导。

#!/bin/bash
function hello {
        echo "123"
        return 1
}
hello ...


比如改一下:
if hello;then
echo "aaa"
fi

先察看hello函数的返回值,是真(0)还是假(1),再决定是否执行呀

论坛徽章:
9
2015亚冠之阿尔纳斯尔
日期:2015-09-10 16:21:162015亚冠之塔什干火车头
日期:2015-07-01 16:23:022015年亚洲杯之巴勒斯坦
日期:2015-04-20 17:19:46子鼠
日期:2014-11-13 09:51:26未羊
日期:2014-08-28 18:13:36技术图书徽章
日期:2014-02-21 09:30:15酉鸡
日期:2014-01-14 11:12:49天蝎座
日期:2013-12-09 17:56:53平安夜徽章
日期:2015-12-26 00:06:30
7 [报告]
发表于 2009-03-02 15:27 |只看该作者
原帖由 wangduo112 于 2009-3-2 15:24 发表



那看到很多人退出都写exit 1  这样的命令退出啊。加上一个1 是别有意义,还是画蛇添足啊?

个人爱好吧,退出时的返回值,我比较喜欢用exit 0

论坛徽章:
0
8 [报告]
发表于 2009-03-02 15:27 |只看该作者
原帖由 leetaedong 于 2009-3-2 15:25 发表
exit [n]
              Cause the shell to exit with a status of n.  If n is omitted, the exit sta-
              tus is that of the last command executed.   A  trap  on  EXIT  is  executed
    ...


这个学习下!!!

论坛徽章:
0
9 [报告]
发表于 2009-03-02 15:38 |只看该作者
我想这种情况下有用吧,就像函数的return值一样的作用
不知道shell、子shell有没有关系

脚本1:a.sh
...
...
exit 1

脚本2:b.sh
a.sh
if [ $? -eq 1];
then
CMD
fi

论坛徽章:
0
10 [报告]
发表于 2009-03-02 16:18 |只看该作者
原帖由 wangduo112 于 2009-3-2 15:24 发表



那看到很多人退出都写exit 1  这样的命令退出啊。加上一个1 是别有意义,还是画蛇添足啊?


exit n ,是退出程序(脚本)的
n是返回给shell的,1~255通常用于程序出错时的返回值,0则表示程序运行正常的返回值,这样方便调试时查找原因

return [n]
              Causes  a  function  to  exit with the return value
              specified by n.  If n is omitted, the return status
              is  that  of the last command executed in the func-
              tion body.  If used outside a function, but  during
              execution  of  a script by the .  (source) command,
              it causes the shell to stop executing  that  script
              and  return either n or the exit status of the last
              command executed within the script as the exit sta-
              tus  of the script.  If used outside a function and
              not during execution of a script by .,  the  return
              status  is  false.  Any command associated with the
              RETURN trap is executed  before  execution  resumes
              after the function or script.

return是函数返回给脚本的

[ 本帖最后由 haimming 于 2009-3-2 16:26 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP