PATH中的shell脚本是否可以用来检测输入的选项$1
发表于:2026-11-04 作者:千家信息网编辑
千家信息网最后更新 2026年11月04日,PATH中的shell脚本是否可以用来检测输入的选项$1,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。下面是例子001:#!/bin/s
千家信息网最后更新 2026年11月04日PATH中的shell脚本是否可以用来检测输入的选项$1
PATH中的shell脚本是否可以用来检测输入的选项$1,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
下面是例子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 1ficheckForCmdInPath "$1"case $? in 0 ) echo "$1 found in PATH" ;; 1 ) echo "$1 not found or not executable" ;; 2 ) echo "$1 not found in PATH" ;;esacexit 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。我想这脚本还有需要完善的地方。
看完上述内容,你们掌握PATH中的shell脚本是否可以用来检测输入的选项$1的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!
脚本
输入
检测
问题
内容
函数
地方
方法
更多
结果
束手无策
为此
例子
原因
变量
字符
对此
意思
技能
文件
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网科技公司华为
科技与互联网
今日互联网科技
平安科技 互联网
嘟嘟瓜互联网科技
互联网科技革命
广州宏创互联网科技
九亚互联网科技
顺玮阁互联网科技
天扬科技互联网广告
深圳互联网科技怎么样
宁波青枫互联网科技有限公司
南山科技园互联网公司有哪些
智腾互联网科技有限公司
青岛科技大学互联网大赛研究生
互联网科技创新的路径
沐思想互联网科技有限公司
伍佰万互联网科技怎么赚钱
江苏互联网科技小贷
马化腾互联网科技教学
科技互联网技术
互联网金融与金融科技
上海喔噻互联网科技
互联网 新科技
撒么互联网科技公司
晓花互联网科技有限公司网址
烟台灵鸽互联网科技有限公司
全球科技与互联网公司市值变化
唯美互联网科技
科技美学互联网 艺术