自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Bash Getopts :讓你的腳本支持命令行參數(shù)

運維 系統(tǒng)運維
以前我總想知道如何為我的Bash腳本創(chuàng)建命令行參數(shù)。經(jīng)過搜索,我發(fā)現(xiàn)了2個函數(shù)可以處理這個問題,getopt 函數(shù)和 getopts 函數(shù)。我無意爭論哪一個函數(shù)更好的。getopts 是一個shell內(nèi)建命令,而且似乎比 getopt 更容易實現(xiàn)這個功能,所以在這篇文章里我準備講講getopts。

以前我總想知道如何為我的Bash腳本創(chuàng)建命令行參數(shù)。經(jīng)過搜索,我發(fā)現(xiàn)了2個函數(shù)可以處理這個問題,getopt 函數(shù)和 getopts 函數(shù)。我無意爭論哪一個函數(shù)更好的。getopts 是一個shell內(nèi)建命令,而且似乎比 getopt 更容易實現(xiàn)這個功能,所以在這篇文章里我準備講講getopts。

[[114412]]

bash getopts

開始的時候,我只試著處理傳遞給腳本的命令行參數(shù)。***,我添加了另外一些有用的功能函數(shù),使得這個腳本可以成為其他任何交互式腳本處理命令行的開始模板。我還添加了一個純文本格式的幫助函數(shù),讓腳本更加容易閱讀。

與其來一長段文字解釋 getopts 在bash中是如何工作的,我認為不如直接來一個能工作的腳本更讓人覺得輕松一些。

  1. #!/bin/bash
  2.  
  3. ######################################################################
  4. #This is an example of using getopts in Bash. It also contains some
  5. #other bits of code I find useful.
  6. #Author: Linerd
  7. #Website: http://tuxtweaks.com/
  8. #Copyright 2014
  9. #License: Creative Commons Attribution-ShareAlike 4.0
  10. #http://creativecommons.org/licenses/by-sa/4.0/legalcode
  11. ######################################################################
  12.  
  13. #Set Script Name variable
  14. SCRIPT=`basename ${BASH_SOURCE[0]}`
  15.  
  16. #Initialize variables to default values.
  17. OPT_A=A
  18. OPT_B=B
  19. OPT_C=C
  20. OPT_D=D
  21.  
  22. #Set fonts for Help.[譯注: 這里tput用來更改終端文本屬性,比如加粗,高亮等]
  23. NORM=`tput sgr0`
  24. BOLD=`tput bold`
  25. REV=`tput smso`
  26.  
  27. #Help function
  28. function HELP {
  29. echo -e \\n"Help documentation for ${BOLD}${SCRIPT}.${NORM}"\\n
  30. echo -e "${REV}Basic usage:${NORM} ${BOLD}$SCRIPT file.ext${NORM}"\\n
  31. echo "Command line switches are optional. The following switches are recognized."
  32. echo "${REV}-a${NORM} --Sets the value for option ${BOLD}a${NORM}. Default is ${BOLD}A${NORM}."
  33. echo "${REV}-b${NORM} --Sets the value for option ${BOLD}b${NORM}. Default is ${BOLD}B${NORM}."
  34. echo "${REV}-c${NORM} --Sets the value for option ${BOLD}c${NORM}. Default is ${BOLD}C${NORM}."
  35. echo "${REV}-d${NORM} --Sets the value for option ${BOLD}d${NORM}. Default is ${BOLD}D${NORM}."
  36. echo -e "${REV}-h${NORM} --Displays this help message. No further functions are performed."\\n
  37. echo -e "Example: ${BOLD}$SCRIPT -a foo -b man -c chu -d bar file.ext${NORM}"\\n
  38. exit 1
  39. }
  40.  
  41. #Check the number of arguments. If none are passed, print help and exit.
  42. NUMARGS=$#
  43. echo -e \\n"Number of arguments: $NUMARGS"
  44. if [ $NUMARGS -eq 0 ]; then
  45. HELP
  46. fi
  47.  
  48. ### Start getopts code ###
  49.  
  50. #Parse command line flags
  51. #如果選項需要后跟參數(shù),在選項后面加":"
  52. #注意"-h"選項后面沒有":",因為他不需要參數(shù)。選項字符串最開始的":"是用來去掉來自getopts本身的報錯的,同時獲取不能識別的選項。(譯注:如果選項字符串不以":"開頭,發(fā)生錯誤(非法的選項或者缺少參數(shù))時,getopts會向錯誤輸出打印錯誤信息;如果以":"開頭,則不會打印[在man中叫slient error reporting],同時將出錯的選項賦給OPTARG變量)
  53.  
  54. while getopts :a:b:c:d:h FLAG; do
  55. case $FLAG in
  56. a) #set option "a"
  57. OPT_A=$OPTARG
  58. echo "-a used: $OPTARG"
  59. echo "OPT_A = $OPT_A"
  60. ;;
  61. b) #set option "b"
  62. OPT_B=$OPTARG
  63. echo "-b used: $OPTARG"
  64. echo "OPT_B = $OPT_B"
  65. ;;
  66. c) #set option "c"
  67. OPT_C=$OPTARG
  68. echo "-c used: $OPTARG"
  69. echo "OPT_C = $OPT_C"
  70. ;;
  71. d) #set option "d"
  72. OPT_D=$OPTARG
  73. echo "-d used: $OPTARG"
  74. echo "OPT_D = $OPT_D"
  75. ;;
  76. h) #show help
  77. HELP
  78. ;;
  79. \?) #unrecognized option - show help
  80. echo -e \\n"Option -${BOLD}$OPTARG${NORM} not allowed."
  81. HELP
  82. #在這里如果你不想打印完整的幫助信息,只想顯示簡單的錯誤信息,去掉上面的兩行,同時使用下面的兩行。
  83. #echo -e "Use ${BOLD}$SCRIPT -h${NORM} to see the help documentation."\\n
  84. #exit 2
  85. ;;
  86. esac
  87. done
  88.  
  89. shift $((OPTIND-1)) #This tells getopts to move on to the next argument.
  90.  
  91. ### End getopts code ###
  92.  
  93.  
  94. ### Main loop to process files ###
  95.  
  96. #這里你可以用你的腳本處理邏輯來替代。這個例子只是在終端中打印文件的文件名和后綴名。你可以把任意其他的文件處理任務(wù)放到這個while-do循環(huán)中。
  97.  
  98. while [ $# -ne 0 ]; do
  99. FILE=$1
  100. TEMPFILE=`basename $FILE`
  101. #TEMPFILE="${FILE##*/}" #另外一種獲取不帶后綴的文件名的方法。
  102. FILE_BASE=`echo "${TEMPFILE%.*}"` #file without extension
  103. FILE_EXT="${TEMPFILE##*.}" #file extension
  104.  
  105.  
  106. echo -e \\n"Input file is: $FILE"
  107. echo "File withouth extension is: $FILE_BASE"
  108. echo -e "File extension is: $FILE_EXT"\\n
  109. shift #Move on to next input file.
  110. done
  111.  
  112. ### End main loop ###
  113.  
  114. exit 0

將上面的代碼復(fù)制到你的文本編輯器里,然后保存到你的可執(zhí)行路徑下。我將這個腳本命名為 options 并保存到/home/linerd/bin 路徑下。保存之后記得給你的腳本添加可執(zhí)行權(quán)限。

  1. chmod +x ~/bin/options

現(xiàn)在腳本已經(jīng)可以運行了。試試用 -h 參數(shù)來打印幫助信息吧。

  1. options -h

遇到不支持的選項,腳本同樣可以給出提示,并打印幫助信息。

  1. options -z

***,getopts可以以任意的順序處理你給的命令行參數(shù)。唯一的限制是你要處理的文件必須放在所有參數(shù)的***。

  1. options -d bar -c chu -b man -a foo example1.txt example2.txt

現(xiàn)在你可以從這些例子里看到如何通過命令行參數(shù)給腳本里的變量賦值。這個腳本里除了getopts還有很多其他的東西,但是我認為這些就足以成為一個新腳本的開頭模板了。如果你有興趣更深入地學(xué)習(xí)bash的getopts,你可以找找深埋在man page的“Builtins”這一節(jié)里的文檔,也可以從 Bash Reference Manual 找到信息。

接下來呢?

你會用getops來干什么呢?在評論里告訴我吧。


via: http://tuxtweaks.com/2014/05/bash-getopts/

譯者: CNprober <travelwithheart@yeah.net, QQ619913541> 校對:wxy

責任編輯:黃丹 來源: Linux中國
相關(guān)推薦

2021-08-30 07:50:42

腳本語言命令行

2019-08-20 10:02:35

2018-03-29 08:30:48

Linux命令BASH

2018-08-22 09:40:27

2017-05-25 10:32:40

命令linux系統(tǒng)

2020-04-26 15:38:28

Docker容器

2021-02-06 14:21:12

Linux 開發(fā)操作系統(tǒng)

2009-12-25 17:05:09

LINUX Bash

2023-06-25 12:00:53

2010-03-10 17:23:37

Python 命令行參

2010-11-24 15:33:59

mysql命令行參數(shù)

2010-06-23 14:28:23

LINUX Bash

2018-03-30 13:06:32

Linux命令Bash

2013-03-14 16:25:46

命令行

2011-08-22 11:51:13

Linuxconfigure

2010-07-20 14:02:38

Perl命令行參數(shù)

2010-07-26 09:32:41

Perl命令行

2019-10-12 10:12:13

Bash命令行Linux

2009-11-20 09:31:47

Linux命令命令行Linux

2023-06-08 12:37:17

點贊
收藏

51CTO技術(shù)棧公眾號