LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 690|回复: 3

一个关于命令执行顺序的问题

[复制链接]
发表于 2004-3-28 22:11:28 | 显示全部楼层 |阅读模式
判断脚本参数是否为空,如为空,则给出错误信息并退出
[ $# -lt 1 ] && echo "Usage: `basename $0` argument(s)" && exit 1


[ $# -ge 1 ] || echo "Usage: `basename $0` argument(s)"  && exit 1
echo "This line will not echo"                                 


[ $# -ge 1 ] || (echo "Usage: `basename $0` argument(s)"  && exit 1 )
echo "This line will echo, why? "                 <<-- 为什么此行会执行?

can you tell me, thanks !
发表于 2004-3-28 22:19:03 | 显示全部楼层

回复: 一个关于命令执行顺序的问题

最初由 coolend 发表
判断脚本参数是否为空,如为空,则给出错误信息并退出
[ $# -lt 1 ] && echo "Usage: `basename $0` argument(s)" && exit 1


[ $# -ge 1 ] || echo "Usage: `basename $0` argument(s)"  && exit 1
echo "This line will not echo"                                 


[ $# -ge 1 ] || (echo "Usage: `basename $0` argument(s)"  && exit 1 )
echo "This line will echo, why? "                 <<-- 为什么此行会执行?

can you tell me, thanks !

父shell会把用()括起来的语句块放在子shell里运行。
所以同样一个"exit",在第三句只是退出子shell而已,接着运行echo,而在第二句则是退出父shell,也就是自身,当然就不会运行echo啦。
发表于 2004-3-29 21:50:34 | 显示全部楼层

回复: 一个关于命令执行顺序的问题

最初由 coolend 发表
判断脚本参数是否为空,如为空,则给出错误信息并退出
[ $# -lt 1 ] && echo "Usage: `basename $0` argument(s)" && exit 1


[ $# -ge 1 ] || echo "Usage: `basename $0` argument(s)"  && exit 1
echo "This line will not echo"                                 


[ $# -ge 1 ] || (echo "Usage: `basename $0` argument(s)"  && exit 1 )
echo "This line will echo, why? "                 <<-- 为什么此行会执行?

can you tell me, thanks !

我常用的方法:
  1. (($#<1))&&{ echo "Usage:$(basename $0) argument(s)";exit 1; }
复制代码
发表于 2004-3-29 22:58:14 | 显示全部楼层
if [ $# == 0 ] ; then
echo "Usage : $0 "
exit 1
fi
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表