• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >linux shell > 用来检测输入的选项$1是否在PATH中的shell脚本

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

作者:mdxy-dxy 字体:[增加 减小] 来源:互联网

mdxy-dxy 通过本文主要向大家介绍了path to mongo shell,shell path,java path设置,win764位path初始值,path等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

今天无意中发现一本挺有意思的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
</div>

这脚本目的是用来检测输入的选项$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。我想这脚本还有需要完善的地方。

</div>

您可能想查找下面的文章:

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

相关文章

  • bash scp command not found的解决方法
  • Bash中数组的操作教程
  • linux使用管道命令执行ps获取cpu与内存占用率
  • 浅谈ctrl+c,ctrl+d,ctrl+z在linux中的意义
  • linux定时任务基础命令介绍(14)
  • 定时导出mysql本地数据替换远程数据库数据脚本分享
  • Linux oracle数据库自动备份自动压缩脚本代码
  • shell中的数组操作小结和冒泡排序实现脚本分享
  • 备份网站内容的shell脚本代码
  • Shell脚本中使用for循环和cat命令实现按顺序合并文件

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • linux定时任务基础命令介绍(14)
    • Linux shell常用的73条命令总结
    • shell写的告警次数控制及恢复示例代码
    • rsync结合find技巧分享
    • 定时导出mysql本地数据替换远程数据库数据脚本分享
    • 一个用了统计CPU 内存 硬盘 使用率的shell脚本
    • linux shell 自定义函数方法(定义、返回值、变量作用域)
    • Shell脚本传递参数的3种方法比较
    • Shell逐行读取文件的4种方法
    • 两个备份数据库的shell脚本

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有