好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > 用來檢測輸入的選項$1是否在PATH中的shell腳本

用來檢測輸入的選項$1是否在PATH中的shell腳本

熱門標簽:測繪地圖標注名稱 百度地圖標注直線距離 鶴崗400電話申請 德陽400電話申請 天津電話外呼系統排名 怎么在百度地圖標注公司的位置 智能電銷機器人有用嗎 外呼電話系統怎么操作 商機地圖標注

今天無意中發現一本挺有意思的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。我想這腳本還有需要完善的地方。

您可能感興趣的文章:
  • 自動化下載并檢測ftp文件備份的shell腳本
  • shell腳本實現實時檢測文件變更
  • Shell腳本實現檢測Cygwin最快的鏡像站點
  • VBS腳本寫的Windows硬件檢測工具分享
  • Linux Shell腳本實現檢測tomcat
  • Shell腳本實現檢測進程是否正在運行
  • PowerShell腳本實現檢測網絡內主機類型
  • 腳本批量檢測網站是否存活

標簽:自貢 武漢 優質小號 丹東 鎮江 滁州 六盤水 百色

巨人網絡通訊聲明:本文標題《用來檢測輸入的選項$1是否在PATH中的shell腳本》,本文關鍵詞  用來,檢測,輸入,的,選項,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《用來檢測輸入的選項$1是否在PATH中的shell腳本》相關的同類信息!
  • 本頁收集關于用來檢測輸入的選項$1是否在PATH中的shell腳本的相關信息資訊供網民參考!
  • 推薦文章