亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
12下一頁
最近訪問板塊 發(fā)新帖
查看: 45668 | 回復(fù): 15
打印 上一主題 下一主題

shell基礎(chǔ)學(xué)習(xí)第二十篇-向腳本傳遞參數(shù) [復(fù)制鏈接]

論壇徽章:
1
2015年辭舊歲徽章
日期:2015-03-03 16:54:15
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2010-11-26 14:05 |只看該作者 |倒序瀏覽
前面已經(jīng)講到如何使用特定變量$ 1 . . $ 9向腳本傳遞參數(shù)。$ #用于統(tǒng)計傳遞參數(shù)的個數(shù)?
以創(chuàng)建一個u s a g e語句,需要時可通知用戶怎樣以適當(dāng)?shù)恼{(diào)用參數(shù)調(diào)用腳本或函數(shù)。
本章內(nèi)容有:
• shift。
• getopts。
• shift和g e t o p t s例子。

簡單地說,下述腳本框架控制參數(shù)開始與停止。腳本需要兩個參數(shù),如果沒有輸入兩個
參數(shù),那么產(chǎn)生一個u s a g e語句。注意這里使用c a s e語句處理輸入腳本的不同參數(shù)。

  1. #!/bin/bash
  2. # opt.sh
  3. usage()
  4. {
  5.         echo "usage: `basename $0` start|stop process name"
  6. }      
  7. OPT=$1
  8. PROCESSID=$1
  9. if [ $# -ne 2 ]; then
  10.         usage
  11.         exit 1
  12. fi      
  13. case $OPT in
  14.         start|Start) echo "Starting..$PROCESSID"
  15.         # some process to go here
  16.         ;;
  17.         stop|Stop) echo "Stopping..$PROCESSID"
  18.         # some process to go here
  19.         ;;
  20.         *)usage
  21.         ;;
  22. esac   
復(fù)制代碼
執(zhí)行腳本,輸入一下參數(shù),結(jié)果為:
  1. [root@localhost ~]# sh opt.sh start named
  2. Starting..start
  3. [root@localhost ~]# sh opt.sh start
  4. usage: opt.sh start|stop process name
復(fù)制代碼
任何U N I X或L I N U X命令均接受一般格式:
命令選項文件選項部分最多可包含1 2個不同的值。上述腳本中,如果必須控制不同的命令選項,就要
加入大量腳本。這里只控制兩個選項:開始和停止。
幸運(yùn)的是s h e l l提供s h i f t命令以幫助偏移選項,使用s h i f t可以去除只使用$ 1到$ 9傳遞參數(shù)的
限制。

論壇徽章:
1
2015年辭舊歲徽章
日期:2015-03-03 16:54:15
2 [報告]
發(fā)表于 2010-11-26 14:10 |只看該作者
20.1 shift命令
向腳本傳遞參數(shù)時,有時需要將每一個參數(shù)偏移以處理選項,這就是s h i f t命令的功能。
它每次將參數(shù)位置向左偏移一位,下面用一段簡單腳本詳述其功能。腳本使用w h i l e循環(huán)反饋
所有傳遞到腳本的參數(shù)。
  1. #!/bin/bash
  2. # opt2.sh
  3. loop=0
  4. while [ $# -ne 0 ] # while there are still arguments
  5. do
  6.         echo $1
  7. done
復(fù)制代碼
你可能想像,上述腳本一直執(zhí)行,直到命令行中不再有更多的參數(shù)輸入。錯了,因為沒
有辦法偏移到腳本中下一個參數(shù),將只會反饋出第一個參數(shù)。執(zhí)行結(jié)果如下:
  1. # sh opt2.sh file1 file2 file3
  2. file1
  3. file1
  4. file1
  5. ......
復(fù)制代碼
20.1.1 shift命令簡單用法
使用s h i f t命令來處理傳遞到腳本的每一個參數(shù)。改動后腳本如下:
  1. #!/bin/bash
  2. # opt2.sh
  3. loop=0
  4. while [ $# -ne 0 ] # while there are still arguments
  5. do
  6.         echo $1
  7.         shift
  8. done
復(fù)制代碼
現(xiàn)在執(zhí)行一下,結(jié)果就會明顯不同了。如下所示:
  1. [root@localhost ~]# sh opt2.sh file1 file2 file3
  2. file1
  3. file2
  4. file3
復(fù)制代碼
20.1.2 命令行輸入的最后一個參數(shù)
雖然還沒有講e v a l命令,如果需要知道命令行中輸入的最后一個參數(shù)(通常是一個文件名),
可以有兩種選擇:使用命令eval echo \$$#;使用s h i f t命令:shift 'expr $# -2'。

論壇徽章:
1
2015年辭舊歲徽章
日期:2015-03-03 16:54:15
3 [報告]
發(fā)表于 2010-11-26 14:42 |只看該作者
20.1.3 使用shift處理文件轉(zhuǎn)換
s h i f t可使控制命令行選項更加容易。下面構(gòu)造一個轉(zhuǎn)換腳本,使用t r將文件名轉(zhuǎn)換為大寫
或小寫。
腳本選項為:
-l 用于小寫轉(zhuǎn)換。
-u 用于大寫轉(zhuǎn)換。
使用s h i f t命令將腳本放在一起以控制- l和- u選項。腳本的第一版本如下:
  1. #!/bin/bash
  2. # tr_case.sh
  3. # case conversion
  4. usage()
  5. {
  6.         # usage
  7.         echo "usage: `basename $0` -[l|u] file [files]" >&2
  8.         exit 1
  9. }      
  10. if [ $# -eq 0 ]; then
  11.         # no parameters passed !
  12. fi      
  13. while [ $# -gt 0 ]
  14. do
  15.         case $1 in
  16.                 -u|-U) echo "-u option specified"
  17.                 # do any setting of variables here for lowercase then shift
  18.                 shift
  19.                 ;;
  20.                 -l|-L) echo "-l option specified"
  21.                 # do any setting of variables here for uppercase then shift
  22.                 shift
  23.                 ;;
  24.                 *) usage
  25.                 ;;
  26.         esac   
  27. done   
復(fù)制代碼
首先檢查腳本是否有參數(shù),如果沒有,打印u s a g e語句,如果有需要處理的參數(shù),使用
c a s e語句捕獲每一個傳送過來的選項。當(dāng)處理完此選項后,使用s h i f t命令搜集命令行中下一選
項,如果未發(fā)現(xiàn)匹配選項,打印u s a g e語句。
當(dāng)向腳本傳遞兩個無效參數(shù)時,輸出如下:
  1. [root@localhost ~]# sh tr_case.sh -u -l -k
  2. -u option specified
  3. -l option specified
  4. usage: tr_case.sh -[l|u] file [files]
復(fù)制代碼
下一步就是要用c a s e語句處理選項后傳遞過來的文件名。為此需改動c a s e語句。c a s e語句
中捕獲任意模式*應(yīng)該為- *以允許傳遞無效選項,例如- p或- q。
*模式也匹配傳遞過來的所有文件名,以便用f o r循環(huán)處理每一個文件,這里也將使用- f選
項檢測文件是否存在。
改動后的c a s e語句如下:
  1. case
  2.           ......
  3.                 -*) echo "usage: `basename $0` -[l|u] file [file..]"
  4.                 exit 1
  5.                 ;;
  6.                 *) # collect the files to process
  7.                 if [ -f $1 ]; then
  8.                         # add the filenames to a variable list
  9.                         FILES=$FILES" "$1
  10.                 else   
  11.                         echo "`basename $0`: Error cannot find the file $1"
  12.                 fi      
  13.                 shift
  14.                 ;;
  15.         esac
復(fù)制代碼
還需要指定與選項( - l,- u)相關(guān)的變量設(shè)置。這些變量是:
T R C A S E 保存轉(zhuǎn)換類型(大寫或小寫)。
E X T 所有文件轉(zhuǎn)換后,大寫文件名為. U C,小寫為. L C,不保存初始文件狀態(tài)。
O P T 如果給出此選項,設(shè)其為y e s,否則為n o。如果沒有給出此選項,捕獲此信息并反
饋出來。
其他部分腳本用于實(shí)際轉(zhuǎn)換處理,這里即t r命令。t r命令放在c a s e語句f o r循環(huán)中讀取文件
名進(jìn)行處理的腳本末尾部分。
以下為完整腳本:
  1. #!/bin/bash
  2. # tr_case.sh
  3. # case conversion
  4. # convert files to either upper or lower case
  5. FILES=""
  6. TRCASE=""
  7. EXT=""
  8. OPT=no
  9. # gets called when a conversion fails
  10.                 # do any setting of variables here for lowercase then shift
  11.                 shift
  12.                 ;;
  13. error_msg()
  14. {
  15.         _FILENAME=$1
  16.                
  17.                 shift
  18.                 ;;
  19.         echo "`basename $0`: Error the conversion failed on $_FILENAME"
  20. }

  21. if [ $# -eq 0 ]; then
  22.         usage
  23.         # no parameters passed !
  24. fi
  25. while [ $# -gt 0 ]
  26. do
  27.         case $1 in
  28.                 # set the variables based on what option was used
  29.                 -u) TRCASE=upper
  30.                 EXIT".UC"
  31.                 OPT=yes
  32.                 shift
  33.                 ;;
  34.                 -l) TRCASE=lower
  35.                 EXT=".LC"
  36.                 OPT=yes
  37.                 shift
  38.                 ;;
  39.                 -help) echo "convert a file(s) to uppercase from lowercase"
  40.                 echo "convert a file(s) from lowercase to uppercase"
  41.                 echo "will convert all characters according to the"

  42.                 ;;
  43.                 echo "  specified command option."
  44.                 echo " Where option is"
  45.                 echo " -l Convert to lowercase"
  46.                 echo " -u Convert to uppercase"
  47.                 echo " The original file(s) is not touched. A new file(s)"
  48.                 echo "will be created with either a .UC or .LC extension"
  49.                 echo "usage: $0 -[l|u] file [file..]"
  50.                 exit 0
  51.                 ;;
  52.                 -*) echo "usage: `basename $0` -[l|u] file [file..]"
  53.                 exit 1
  54.                 ;;
  55.                 *) # collect the files to process
  56.                 if [ -f $1 ]; then
  57.                         # add the filenames to a variable list
  58.                         FILES=$FILES" "$1
  59.                 else   
  60.                         echo "`basename $0`: Error cannot find the file $1"
  61.                 fi      
  62.                 shift
  63.                 ;;
  64.         esac
  65. done
  66. # no options given ... help the user
  67. if [ "$OPT" == "no" ]; then
  68.         echo " try `basename $0` --help"
  69.         exit 1
  70. fi      
  71. # now read in all the file(s)
  72. # use the variable LOOP, I just love the word LOOP
  73. for LOOP in $FILES
  74. do      
  75.         case $TRCASE in
  76.                 lower) cat $LOOP | tr "[a-z]" "[A-Z]" > $LOOP$EXT
  77.                 if [ $? != 0 ]; then
  78.                         error_msg $LOOP
  79.                 else   
  80.                         echo "Converted file called $LOOP$EXT"
  81.                 fi      
  82.                 ;;
  83.                 upper) cat $LOOP | tr "[A-Z]" "[a-z]" >$LOOP$EXT
  84.                 if [ $? !=0 ]; then
  85.                         error_msg $LOOP
  86.                 else   
  87.                         echo "Converted file called $LOOP$EXT"
  88.                 fi
  89.                 ;;
  90.         esac
  91. done   
復(fù)制代碼
執(zhí)行上述腳本,給出不同選項,得結(jié)果如下:
轉(zhuǎn)換一個不存在的文件:
  1. [root@localhost ~]# sh tr_case.sh -k cursor
  2. usage: tr_case.sh -[l|u] file [file..]
復(fù)制代碼
傳遞不正確選項:
  1. [root@localhost ~]# sh tr_case.sh cursor
  2. tr_case.sh: Error cannot find the file cursor
  3. tr_case.sh : Error you need to specify an option. No action taken
  4. try tr_case.sh --help
復(fù)制代碼
只鍵入文件名,希望腳本提示更多幫助信息:
  1. [root@localhost ~]# sh tr_case.sh
  2. For more info try tr_case.sh --help
復(fù)制代碼
輸入兩個有效文件及第三個無效文件:
  1. [root@localhost ~]# sh tr_case.sh -l file1 file2 sd
  2. tr_case.sh: Error cannot find the file sd
  3. Converted file called file1.LC
  4. Converted file called file2.LC
復(fù)制代碼
使用上述腳本可以將許多文件轉(zhuǎn)換為同樣的格式。編寫一段腳本,使其控制不同的命令
行選項,這種方式編程量很大,是一件令人頭疼的事。
假定要寫一段腳本,要求控制以下各種不同的命令行選項:
命令-l -c 23 -v 文件1文件2
s h i f t命令顯得力不從心,這就需要用到g e t o p t s命令。

論壇徽章:
1
2015年辭舊歲徽章
日期:2015-03-03 16:54:15
4 [報告]
發(fā)表于 2010-11-26 14:48 |只看該作者
20.2 getopts

g e t o p t s可以編寫腳本,使控制多個命令行參數(shù)更加容易。g e t o p t s用于形成命令行處理標(biāo)
準(zhǔn)形式。原則上講,腳本應(yīng)具有確認(rèn)帶有多個選項的命令文件標(biāo)準(zhǔn)格式的能力。

20.2.1 getopts腳本實(shí)例
通過例子可以更好地理解g e t o p t s。以下g e t o p t s腳本接受下列選項或參數(shù)。
• a 設(shè)置變量A L L為t r u e。
• h 設(shè)置變量H E L P為t r u e。
第20章向腳本傳遞參數(shù)229
下載
• f 設(shè)置變量F I L E為t r u e。
• v 設(shè)置變量V E R B O S E為t r u e。
對于所有變量設(shè)置,一般總假定其初始狀態(tài)為f a l s e:
  1. #!/bin/bash
  2. # getopt1.sh
  3. # set the vars
  4. ALL=false
  5. HELP=false
  6. FILE=false
  7. VERBOSE=false
  8. while getopts ahfgv OPTION
  9. do
  10.         case $OPTION in
  11.                 a) ALL=true
  12.                 echo "ALL is $ALL"
  13.                 ;;
  14.                 h) HELP=true
  15.                 echo "HELP is $HELP"
  16.                 ;;
  17.                 f) FILE=true
  18.                 echo "FILE is $FILE"
  19.                 ;;
  20.                 v) VERBOSE=true
  21.                 echo "VERBOSE is $VERBOSE"
  22.                 ;;
  23.         esac   
  24. done   
復(fù)制代碼
g e t o p t s一般格式為:
getopts option_string variable
在上述例子中使用腳本:
while getopts ahfgv OPTION
可以看出w h i l e循環(huán)用于讀取命令行,o p t i o n s t r i n g為指定的5個選項(- a,- h,- f,- g,- v),
腳本中v a r i a b l e為O P T I O N。注意這里并沒有用連字符指定每一單個選項。
運(yùn)行上述腳本,給出幾個有效和無效的選項,結(jié)果為:
  1. [root@localhost ~]# sh getopt1.sh -a -h
  2. ALL is true
  3. HELP is true
  4. [root@localhost ~]# sh getopt1.sh -a -h -p
  5. ALL is true
  6. HELP is true
  7. getopt1.sh: illegal option -- p
復(fù)制代碼
可以看出不同選項的結(jié)合方式。

論壇徽章:
1
2015年辭舊歲徽章
日期:2015-03-03 16:54:15
5 [報告]
發(fā)表于 2010-11-26 14:49 |只看該作者
20.2.2 getopts使用方式

g e t o p t s讀取o p t i o n s t r i n g,獲知腳本中使用了有效選項。
g e t o p t s查看所有以連字符開頭的參數(shù),將其視為選項,如果輸入選項,將把這與
o p t i o n s t r i n g對比,如果匹配發(fā)現(xiàn),變量設(shè)置為O P T I O N,如果未發(fā)現(xiàn)匹配字符,變量能夠設(shè)
置為?。重復(fù)此處理過程直到選項輸入完畢。
g e t o p t s接收完所有參數(shù)后,返回非零狀態(tài),意即參數(shù)傳遞成功,變量O P T I O N保存最后處
理參數(shù),一會兒就可以看出處理過程中這樣做的好處。

論壇徽章:
1
2015年辭舊歲徽章
日期:2015-03-03 16:54:15
6 [報告]
發(fā)表于 2010-11-26 15:02 |只看該作者
20.2.3 使用getopts指定變量取值

有時有必要在腳本中指定命令行選項取值。g e t o p t s 為此提供了一種方式,即在
o p t i o n s t r i n g中將一個冒號放在選項后。例如:
getopts ahfvc: OPTION
上面一行腳本指出,選項a、h、f、v可以不加實(shí)際值進(jìn)行傳遞,而選項c必須取值。使用
選項取值時,必須使用變量O P TA R G保存該值。如果試圖不取值傳遞此選項,會返回一個錯
誤信息。錯誤信息提示并不明確,因此可以用自己的反饋信息屏蔽它,方法如下:
將冒號放在o p t i o n s t r i n g開始部分。
while getopts :ahfgvc: OPTION
在c a s e語句里使用?創(chuàng)建一可用語句捕獲錯誤。
  1. #!/bin/bash
  2. # getopt2.sh
  3. # set the vars
  4. ALL=false
  5. HELP=false
  6. FILE=false
  7. VERBOSE=false
  8. COPIES=0
  9. # the value for the -c option is set to zero
  10. while getopts ahfgvc: OPTION
  11. do
  12.         case $OPTION in
  13.                 a) ALL=true
  14.                 echo "ALL is $ALL"
  15.                 ;;
  16.                 h) HELP=true
  17.                 echo "HELP is $HELP"
  18.                 ;;
  19.                 f) FILE=true
  20.                 echo "FILE is $FILE"
  21.                 ;;
  22.                 v) VERBOSE=true
  23.                 echo "VERBOSE is $VERBOSE"
  24.                 ;;
  25.                 c) COPIES=$OPTARG
  26.                 echo "COPIES is $COPIES"
  27.                 \?) # usage statement
  28.                 echo "`basename $0` -[a h f v] -[c value] file" >&2
  29.                 ;;
  30.         esac   
  31. done
復(fù)制代碼
運(yùn)行上述腳本,選項- c不賦值,將返回錯誤,但顯示的是腳本語句中的反饋信息:
  1. [root@localhost ~]# sh getopt2.sh -ah -c
  2. ALL is true
  3. HELP is true
  4. getopt2.sh: option requires an argument -- c
  5. getopt2.sh -[a h f v] -[c value] file
復(fù)制代碼
現(xiàn)在輸入所有的合法選項:
  1. [root@localhost ~]# sh getopt2.sh -ah -c 3
  2. ALL is true
  3. HELP is true
  4. COPIES is 3
復(fù)制代碼

論壇徽章:
1
2015年辭舊歲徽章
日期:2015-03-03 16:54:15
7 [報告]
發(fā)表于 2010-11-26 15:09 |只看該作者
20.2.4 訪問取值方式

g e t o p t s的一種功能是運(yùn)行后臺腳本。這樣可以使用戶加入選項,指定不同的磁帶設(shè)備以
備份數(shù)據(jù)。使用g e t o p t s實(shí)現(xiàn)此任務(wù)的基本框架如下:
  1. #!/bin/bash
  2. # backups.sh
  3. QUITE=n
  4. DEVICE=awa
  5. LOGFILE=/tmp/logbackup
  6. usage()
  7. {
  8.         echo "Usage: `basename $0` -d [device] -l [logfile] -q"
  9.         exit 1
  10. }
  11. if [ $# == 0 ]; then
  12.         usage
  13. fi
  14. while getopts :qd:l: OPTION
  15. do
  16.         case $OPTION in
  17.                 q) QUITE=y
  18.                 LOGFILE="/tmp/backup.log"
  19.                 ;;
  20.                 d) DEVICE=$OPTARG
  21.                 ;;
  22.                 l) LOGFILE=$OPTARG
  23.                 ;;
  24.                 \?) usage
  25.                 ;;
  26.         esac
  27. done
  28. echo "you chose the following options .. I can process these"
  29. echo "Quite= $QUITE $DEVICE $LOGFILE"
復(fù)制代碼
上述腳本中如果指定選項d,則需為其賦值。該值為磁帶設(shè)備路徑。用戶也可以指定是否
備份輸出到登錄文件中的內(nèi)容。運(yùn)行上述腳本,指定下列輸入:
  1. [root@localhost ~]# sh backups.sh -d/dev/rmt0 -q
  2. you chose the following options .. I can process these
  3. Quite= y /dev/rmt0 /tmp/backup.log
復(fù)制代碼
g e t o p t s檢查完之后,變量O P TA R G取值可用來進(jìn)行任何正常的處理過程。當(dāng)然,如果輸
入選項,怎樣進(jìn)行進(jìn)一步處理及使該選項有有效值,完全取決于用戶。
以上是使用g e t o p t s對命令行參數(shù)處理的基本框架。
實(shí)際處理文件時,使用f o r循環(huán),就像在t r- c a s e腳本中使用s h i f t命令過濾所有選項一樣。
使用g e t o p t s與使用s h i f t方法比較起來,會減少大量的編程工作。

論壇徽章:
1
2015年辭舊歲徽章
日期:2015-03-03 16:54:15
8 [報告]
發(fā)表于 2010-11-26 15:24 |只看該作者
20.2.5 使用getopts處理文件轉(zhuǎn)換

現(xiàn)在用所學(xué)知識將t r- c a s e腳本轉(zhuǎn)換為g e t o p t s版本。命令行選項g e t o p t s方法與s h i f t方法的唯
一區(qū)別是一個V E R B O S E選項。
變量V E R B O S E缺省取值為n o,但選擇了命令行選項后, c a s e語句將捕獲它,并將其設(shè)為
y e s,反饋的命令是一個簡單的i f語句。
  1. if [ "$VERBOSE" == "on" ]; then
  2.                         echo "doing..lower on $LOOP .. newfile called $LOOP$EXT"
  3.                 fi   
復(fù)制代碼
如果正在使用其他系統(tǒng)命令包,它總是反饋用戶動作,只需簡單地將包含錯誤的輸出重
定向到/ d e v / n u l l中即可。如:
命令>/dev/null 2 >&1
缺省時V E R B O S E關(guān)閉(即不顯示),使用- v選項可將其打開。例如要用V E R B O S E將
m y f i l e文件系列轉(zhuǎn)換為小寫,方法如下:
tr-case -l -v myfile1 myfile2 ...
或者
tr-case -v -l myfile1 myfile2 ...
可能首先注意的是使用g e t o p t s后腳本的縮減效果。這里用于文件處理的腳本與s h i f t版本
相同。
腳本如下:
  1. #!/bin/bash
  2. # tr_case2.sh
  3. # convert case, using getopts
  4. EXT=""
  5. TRCASE=""
  6. FLAG=""
  7. OPT="no"
  8. VERBOSE="off"
  9. while getopts :luv OPTION
  10. do
  11.         case $OPTION in
  12.                 l) TRCASE="lower"
  13.                 EXT=".LC"
  14.                 OPT=yes
  15.                 ;;
  16.                 u) TRCASE="upper"
  17.                 EXT=".UC"
  18.                 OPT=yes
  19.                 ;;
  20.                 v) VERBOSE=on
  21.                 ;;
  22.                 \?) echo "usage: `basename $0`: -[l|u] --v file[s]"
  23.                         echo "doing.. lower on $LOOP .. newfile called $LOOP$EXT
  24.                 exit 1
  25.                 ;;
  26.         esac   
  27. done   
  28. # next argument down only please
  29. shift `expr $OPTION - 1`
  30. # are there any argument passed ??
  31. if [ "$#" == "0" ] || [ "$OPT" == "no" ]; then
  32.         echo "usage: `basename $0` : -[l|u] -v file[s]" >&2
  33.         exit 1
  34. fi      
  35. for LOOP in "$@"
  36. do      
  37.         if [ ! -f $LOOP ]; then
  38.                 echo "`basename $0` : Error cannot find file $LOOP" >&2
  39.                 exit 1
  40.         fi      
  41.         echo $TRCASE $LOOP
  42.         case $TRCSSE in
  43.                 lower)
  44.                 if [ "$VERBOSE" == "on" ]; then
  45.                         echo "doing..lower on $LOOP .. newfile called $LOOP$EXT"
  46.                 fi      
  47.                 cat $LOOP | tr "[a-z]" "[A-Z]" > $LOOP$EXT
  48.                 ;;
  49.                 upper)
  50.                 if [ "$VERBOSE" == "on" ]; then
  51.                         echo "doing..lower on $LOOP ..newfile called $LOOP$EXT"
  52.                 fi      
  53.                 cat $LOOP | tr "[A-Z]" "[a-z]" >$LOOP$EXT
  54.                 ;;
  55.         esac   
  56. done   
復(fù)制代碼
在腳本中指定命令行選項時,最好使其命名規(guī)則與U N I X或L I N U X一致。下面是一些選項
及其含義的列表。
選項含義
- a 擴(kuò)展
- c 計數(shù)、拷貝
- d 目錄、設(shè)備
- e 執(zhí)行
- f 文件名、強(qiáng)制
- h 幫助
- i 忽略狀態(tài)
- l 注冊文件
- o 完整輸出
- q 退出
- p 路徑
-v 顯示方式或版本

論壇徽章:
1
2015年辭舊歲徽章
日期:2015-03-03 16:54:15
9 [報告]
發(fā)表于 2010-11-26 15:24 |只看該作者
20.3 小結(jié)

正確控制命令行選項會使腳本更加專業(yè)化,對于用戶來說會使之看起來像一個系統(tǒng)命令。
本章講到了控制命令行選項的兩種方法, s h i f t和g e t o p t s。使用g e t o p t s檢測腳本的數(shù)量遠(yuǎn)遠(yuǎn)小
于使用s h i f t方法檢測腳本的數(shù)量。
s h i f t也克服了腳本參數(shù)$ 1 . . $ 9的限制。使用s h i f t命令,腳本可以很容易偏移至所有調(diào)用參
數(shù),因此腳本可以做進(jìn)一步處理。

論壇徽章:
1
2015年辭舊歲徽章
日期:2015-03-03 16:54:15
10 [報告]
發(fā)表于 2010-11-26 15:26 |只看該作者
shell腳本學(xué)習(xí)第二十篇,結(jié)束~~

END~~
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP