免费注册 查看新帖 |

Chinaunix

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

100 CoolShell ch01 问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-03-20 16:53 |只看该作者 |倒序浏览
  1. #!/bin/sh
  2. # inpath - Verifies that a specified program is either valid as is,
  3. #   or that it can be found in the PATH directory list.

  4. in_path()
  5. {
  6.   # Given a command and the PATH, try to find the command. Returns
  7.   # 0 if found and executable, 1 if not. Note that this temporarily modifies
  8.   # the IFS (input field separator) but restores it upon completion.

  9.   cmd=$1        path=$2         retval=1
  10.   oldIFS=$IFS   IFS=":"

  11.   for directory in $path
  12.   do
  13.     if [ -x $directory/$cmd ] ; then
  14.       retval=0      # if we're here, we found $cmd in $directory
  15.     fi
  16.   done
  17.   IFS=$oldIFS
  18.   return $retval
  19. }

  20. checkForCmdInPath()
  21. {
  22.   var=$1

  23.   # The variable slicing notation in the following conditional
  24.   # needs some explanation: ${var#expr} returns everything after
  25.   # the match for 'expr' in the variable value (if any), and
  26.   # ${var%expr} returns everything that doesn't match (in this
  27.   # case, just the very first character. You can also do this in
  28.   # Bash with ${var:0:1}, and you could use cut too: cut -c1.

  29.   if [ "$var" != "" ] ; then
  30.     if [ "${var%${var#?}}" = "/" ] ; then
  31.       if [ ! -x $var ] ; then
  32.         return 1
  33.       fi
  34.     elif ! in_path $var $PATH ; then
  35.       return 2
  36.     fi
  37.   fi
  38. }

复制代码
其中后那个elif ! in_path $var $PATH 看不懂啊,请教各位。。。

论坛徽章:
33
ChinaUnix元老
日期:2015-02-02 08:55:39CU十四周年纪念徽章
日期:2019-08-20 08:30:3720周年集字徽章-周	
日期:2020-10-28 14:13:3020周年集字徽章-20	
日期:2020-10-28 14:04:3019周年集字徽章-CU
日期:2019-09-08 23:26:2519周年集字徽章-19
日期:2019-08-27 13:31:262016科比退役纪念章
日期:2022-04-24 14:33:24
2 [报告]
发表于 2012-03-20 18:40 |只看该作者
调用函数in_path,$var和$PATH传到函数里面之后就是$1和$2
还有哪里不懂?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP