用來(lái)檢測(cè)輸入的選項(xiàng)$1是否在PATH中的shell腳本
今天無(wú)意中發(fā)現(xiàn)一本挺有意思的shell編程的書(shū),是e文的,內(nèi)容是101個(gè)shell案例,堅(jiān)持明天看一個(gè),寫(xiě)點(diǎn)心得。
下面是例子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
這腳本目的是用來(lái)檢測(cè)輸入的選項(xiàng)$1是否在PATH中。
這腳本有幾個(gè)地方值得注意的:
1)它運(yùn)用了函數(shù)嵌套,在checkForCmdInPath里嵌套了in_path函數(shù)。
2)if [ "${var%${var#?}}" = "/" ] 這語(yǔ)句中的${var%${var#?}}是顯示變量的第一個(gè)字符,也可以用${varname:1:1} 或$(echo $var | cut -c1)替代。
3) elif ! in_path $var $PATH ; then 這意思是如果in_path $var $PATH 執(zhí)行結(jié)果不為0的話(huà)則
問(wèn)題:
發(fā)現(xiàn)輸入 echo , echo_err, /etco_err 都返回正確結(jié)果,但輸入 /etc/echo_right (存在著執(zhí)行文件但不在PATH中)卻返回found in PATH。我想這腳本還有需要完善的地方。
- 自動(dòng)化下載并檢測(cè)ftp文件備份的shell腳本
- shell腳本實(shí)現(xiàn)實(shí)時(shí)檢測(cè)文件變更
- Shell腳本實(shí)現(xiàn)檢測(cè)Cygwin最快的鏡像站點(diǎn)
- VBS腳本寫(xiě)的Windows硬件檢測(cè)工具分享
- Linux Shell腳本實(shí)現(xiàn)檢測(cè)tomcat
- Shell腳本實(shí)現(xiàn)檢測(cè)進(jìn)程是否正在運(yùn)行
- PowerShell腳本實(shí)現(xiàn)檢測(cè)網(wǎng)絡(luò)內(nèi)主機(jī)類(lèi)型
- 腳本批量檢測(cè)網(wǎng)站是否存活
相關(guān)文章
用shell+sendmail實(shí)現(xiàn)服務(wù)器監(jiān)控報(bào)警小腳本
用shell+sendmail寫(xiě)的服務(wù)器監(jiān)控報(bào)警小腳本,有需要的朋友可以參考下2013-03-03shell腳本使用兩個(gè)橫杠接收外部參數(shù)的方法
這篇文章主要介紹了shell腳本使用兩個(gè)橫杠接收外部參數(shù)的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12Shell腳本定時(shí)備份清除運(yùn)行系統(tǒng)日志的代碼
寫(xiě)備份并清除老日志Shell腳本,并附帶一個(gè)清除日志(log)的shell腳本代碼,需要的朋友可以參考下2013-02-02Linux執(zhí)行.sh文件時(shí)提示No such file or directory該怎么辦(三種解決辦法)
本文通過(guò)三種方法給大家介紹Linux執(zhí)行.sh文件,提示No such file or directory錯(cuò)誤的解決辦法,感興趣的朋友跟著小編一起學(xué)習(xí)學(xué)習(xí)吧2015-10-10Linux下使用Shell腳本實(shí)現(xiàn)ftp的自動(dòng)上傳下載的代碼小結(jié)
如何實(shí)現(xiàn)在ftp自動(dòng)上傳下載文件腳本呢?想必還有很多朋友都不太了解吧,下面是ftp自動(dòng)上傳下載文件腳本方法,希望對(duì)朋友們有些幫助2013-02-02Linux下使用shell腳本自動(dòng)執(zhí)行腳本文件
這篇文章主要介紹了Linux下使用shell腳本自動(dòng)執(zhí)行腳本文件的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-12-12