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

主頁 > 知識庫 > shell腳本使用兩個橫杠接收外部參數的方法

shell腳本使用兩個橫杠接收外部參數的方法

熱門標簽:電話機器人對家居行業幫助大嗎 地圖標注審核周期 AI智能電銷機器人壞處 如何申請400的電話呀 合肥電銷外呼系統供應商 電商外呼系統排名 沈陽ai電銷智能機器人 黑暗之魂3地圖標注 蘭州電銷機器人加盟

首先,效果是這樣的:

既可以處理短選項(-)又可以處理長選項(--)

[developer@hadoop-cluster-manager shell]$ ./demo.sh --help
sqoop程序開始運行: demo.sh
Usage: ./demo.sh [options]
 
Options:
 --append, -a:   追加導入(默認為追加模式)
 --overwrite, -o: 覆蓋導入 
 
 --method, -m:   single-單日導入
          interval-區間導入
          all-全表導入
 
 --date, -d:    單日導入,某一日期數據(格式為yyyymmdd)
 
 --startdate, -s: 區間導入,開始日期
 --enddate, -e:  區間導入,結束日期
 
 --help, -h     幫助

shell腳本接外部參數有一種很簡單的辦法,在腳本中使用$0,$1,$2...指代執行腳本時傳入的第幾個參數($0是腳本名)。

但是,這樣做畢竟不夠優雅,

另一種方法shell腳本內使用getopts命令,只可以接短選項(eg:-d,-s,-h),很方便,比較簡單,可以自己去搜一搜。

但如果想要達成上面這種效果同時支持長選項和短選項(eg:--date,-d,--startdate,-s,--help,-h),

就只能使用getopt命令了:

# 定義命令執行選項
if ! ARGS=$(getopt -o aom:d:s:e:h --long append,overwrite,method:,date:,startdate:,enddate:,help -n "$0" -- "$@"); then
  echo "Terminating..."
  echo -e "Usage: ./$SCRIPT_NAME [options]\n"
  echo -e "Options:\n --append, -a:   追加導入(默認為追加模式)\n --overwrite, -o: 覆蓋導入 \n\n --method, -m:   single-單日導入\n          interval-區間導入\n          all-全表導入\n\n --date, -d:    單日導入,某一日期數據(格式為yyyymmdd)\n\n --startdate, -s: 區間導入,開始日期\n --enddate, -e:  區間導入,結束日期\n\n --help, -h     幫助"
  exit 1
fi
 
# 將規范化后的命令行參數分配至位置參數($1,$2,...)
# The -- ensures that whatever options passed in as part of the script won't get interpreted as options for set, but as options for the command denoted by the $progname variable.
eval set -- "${ARGS}"
 
# 接受執行選項;賦值給變量
while true; do
 case "$1" in
  -a|--append)
   mode='append'
   shift
   ;;
  -o|--overwrite)
   mode='overwrite'
   shift
   ;;
  -m|--method)
   method=$2
   shift 2
   ;;
  -d|--date)
   date=$2
   shift 2
   ;;
  -s|--startdate)
   startdate=$2
   shift 2
   ;;
  -e|--enddate)
   enddate=$2
   shift 2
   ;;
  --)
   shift
   break
   ;;
  -h|--help)
   echo -e "Usage: ./$SCRIPT_NAME [options]\n"
   echo -e "Options:\n --append, -a:   追加導入(默認為追加模式)\n --overwrite, -o: 覆蓋導入 \n\n --method, -m:   single-單日導入\n          interval-區間導入\n          all-全表導入\n\n --date, -d:    單日導入,某一日期數據(格式為yyyymmdd)\n\n --startdate, -s: 區間導入,開始日期\n --enddate, -e:  區間導入,結束日期\n\n --help, -h     幫助"
   exit 0
   ;;
  ?)
   echo "missing options, pls check!"
   exit 1
   ;;
 esac
done

到此這篇關于shell腳本使用兩個橫杠接收外部參數的文章就介紹到這了,更多相關shell腳本接收參數內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • 監控MySQL主從狀態的shell腳本
  • 使用Shell腳本如何啟動/停止Java的jar程序
  • Shell中使用grep、sed正則提取和替換字符串
  • Shell eval通過變量獲取環境變量的方法實現
  • shell腳本實戰-while循環語句
  • shell腳本--sed的用法詳解
  • linux shell中 if else以及大于、小于、等于邏輯表達式介紹
  • Linux中執行shell腳本的4種方法總結
  • 一個不錯的shell 腳本教程 入門級
  • Shell字符串比較相等、不相等方法小結
  • python中執行shell命令的幾個方法小結
  • 分享一個可以通過命令簡寫執行對應命令的Shell腳本

標簽:黔南 黔南 河北 河池 淮南 常州 隴南 通遼

巨人網絡通訊聲明:本文標題《shell腳本使用兩個橫杠接收外部參數的方法》,本文關鍵詞  shell,腳本,使用,兩個,橫杠,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《shell腳本使用兩個橫杠接收外部參數的方法》相關的同類信息!
  • 本頁收集關于shell腳本使用兩個橫杠接收外部參數的方法的相關信息資訊供網民參考!
  • 推薦文章