用来检测输入的选项$1是否在PATH中的shell脚本

 更新时间:2016年08月14日 11:26:49   投稿:mdxy-dxy  
今天无意中发现一本挺有意思的shell编程的书,是e文的,内容是101个shell案例,坚持明天看一个,写点心得

今天无意中发现一本挺有意思的shell编程的书,是e文的,内容是101个shell案例,坚持明天看一个,写点心得。
下面是例子001:

#!/bin/sh
# inpath - Verifies that a specified program is either valid as is,
#  or that it can be found in the PATH directory list.

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

 cmd=$1    path=$2     retval=1
 oldIFS=$IFS  IFS=":"

 for directory in $path
 do
  if [ -x $directory/$cmd ] ; then
   retval=0   # if we're here, we found $cmd in $directory
  fi
 done
 IFS=$oldIFS
 return $retval
}

checkForCmdInPath()
{
 var=$1

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

 if [ "$var" != "" ] ; then
  if [ "${var%${var#?}}" = "/" ] ; then
   if [ ! -x $var ] ; then
    return 1
   fi
  elif ! in_path $var $PATH ; then
   return 2
  fi
 fi
}

 
if [ $# -ne 1 ] ; then
 echo "Usage: $0 command" >&2 ; exit 1
fi

checkForCmdInPath "$1"
case $? in
 0 ) echo "$1 found in PATH"         ;;
 1 ) echo "$1 not found or not executable"  ;;
 2 ) echo "$1 not found in PATH"       ;;
esac

exit 0

这脚本目的是用来检测输入的选项$1是否在PATH中。


这脚本有几个地方值得注意的:
1)它运用了函数嵌套,在checkForCmdInPath里嵌套了in_path函数。
2)if [ "${var%${var#?}}" = "/" ] 这语句中的${var%${var#?}}是显示变量的第一个字符,也可以用${varname:1:1} 或$(echo $var | cut -c1)替代。
3) elif ! in_path $var $PATH ; then 这意思是如果in_path $var $PATH 执行结果不为0的话则
问题:
发现输入 echo , echo_err, /etco_err 都返回正确结果,但输入 /etc/echo_right (存在着执行文件但不在PATH中)却返回found in PATH。我想这脚本还有需要完善的地方。

相关文章

  • 一天一个shell命令 linux文本内容操作系列-sed命令详解

    一天一个shell命令 linux文本内容操作系列-sed命令详解

    这篇文章主要介绍了一天一个shell命令 linux文本内容操作系列-sed命令详解,需要的朋友可以参考下
    2016-06-06
  • shell脚本实现批量采集爱站关键词库

    shell脚本实现批量采集爱站关键词库

    这篇文章主要介绍了shell脚本实现批量采集爱站关键词库,本文工具实现简单,只用一句话实现,需要的朋友可以参考下
    2014-11-11
  • Linux 日常常用指令及应用小结

    Linux 日常常用指令及应用小结

    最近在学习一些基本的Linux指令,在这里总结一下,在搭环境中常用的一些指令,熟悉这些指令就基本能够使用CentOS进行日常操作了
    2017-08-08
  • bash脚本中if语句的使用方法

    bash脚本中if语句的使用方法

    与大多数语言一样,bash 有自己的条件形式。在使用时,要遵循以上格式;即,将 "if" 和 "then" 放在不同行,并使 "else" 和结束处必需的 "fi" 与它们水平对齐。这将使代码易于阅读和调试
    2014-05-05
  • windows下上传shell脚本不能运行的解决方法

    windows下上传shell脚本不能运行的解决方法

    windows下上传shell脚本不能运行—将dos模式修改为unix 文件格式 就可以顺利解决,下文给大家带来了详细解决方法,一起看看吧
    2018-06-06
  • shell脚本设置日志格式的方法

    shell脚本设置日志格式的方法

    本文给大家分享shell脚本设置日志格式的相关知识,设置日志级别并输出对应日志,文中还给大家提到了shell输出格式化日志信息的脚本,感兴趣的朋友跟随小编一起看看吧
    2022-10-10
  • Linux启动过程详细介绍

    Linux启动过程详细介绍

    这篇文章主要介绍了Linux启动过程详细介绍的相关资料,需要的朋友可以参考下
    2017-06-06
  • 监控服务器swap并重启php的Shell脚本

    监控服务器swap并重启php的Shell脚本

    有一台服务器老是交换扇区占满然后失去响应,很烦,因为加载了以前别人写的一个php扩展,效率低,资源占用大,悲剧的是现在还没有可以替换的东西
    2014-03-03
  • Linux中自定义shell脚本启动jar包的方法

    Linux中自定义shell脚本启动jar包的方法

    这篇文章主要介绍了Linux中自定义shell脚本启动jar包的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-01-01
  • 入门shell脚本基础及原理

    入门shell脚本基础及原理

    弄懂shell程序,源代码,以及python脚本能够向运维开发方向走,shell程序能够管理集群,提高开发的效率,shell是命令解释器,调用系统内核,帮助你对内核的刷新认识
    2021-09-09

最新评论