Chinaunix

标题: shell exit 疑问 [打印本页]

作者: ljwd1000    时间: 2011-11-28 17:02
标题: shell exit 疑问
  1. #!/bin/bash

  2. ls /unknow > /dev/null 2>&1

  3. ##echo $?    ##在这里$?的值是2

  4. exit 1

  5. echo "hello"
复制代码
exit 后面的值不是代表$?的值吗。我在状态为1 的时候想退出脚本

但是现在状态为2 也退出脚本了。请大家帮忙看看,谢谢了
作者: 清潼止水    时间: 2011-11-28 17:10
exit [n]
              Cause the shell to exit with a status of n.  If n is omitted, the exit status is that of the  last  com-
              mand executed.  A trap on EXIT is executed before the shell terminates.
作者: ljwd1000    时间: 2011-11-28 17:22
回复 2# 清潼止水


    大神,你贴这个想告诉我什么??  man exit 我看了多次了
作者: cu_little_bird    时间: 2011-11-28 17:27
你那样,$?随便等于多少都会推出,你判断都没有
作者: 清潼止水    时间: 2011-11-28 17:28
回复 3# ljwd1000


    exit 这个命令执行就退出脚本了 不管后面是 0 1 2 ..
     - -
作者: ljwd1000    时间: 2011-11-28 17:35
回复 5# 清潼止水


    那exit后面的那些对应的值存在有何意义??
作者: zooyo    时间: 2011-11-28 17:51
提示: 作者被禁止或删除 内容自动屏蔽
作者: blackold    时间: 2011-11-28 17:58
你那样,$?随便等于多少都会推出,你判断都没有
cu_little_bird 发表于 2011-11-28 17:27



    对啊,判断都没有,无论如何打死也要退出。
作者: Shell_HAT    时间: 2011-11-28 19:29
回复 3# ljwd1000


不去试着理解它,看再多遍也没用。
作者: ljwd1000    时间: 2011-11-29 10:19
谢谢了。现在对这个大概有一些理解了。
作者: cjaizss    时间: 2011-11-29 11:21
if [ $? -eq 1 ];then
exit 1
fi
作者: ljwd1000    时间: 2011-11-29 13:09
多谢诸位版主大人讲解  受益匪浅




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2