shift命令
向腳本傳遞參數時,有時需要將每一個參數偏移以處理選項,這就是 shift命令的功能。它每次將參數位置向左偏移一位,下面用一段簡單腳本詳述其功能。腳本使用 while循環反饋所有傳遞到腳本的參數。使用shift命令來處理傳遞到腳本的每一個參數:
使用shift處理文件大小寫轉換
getopts命令
getopts一般格式為:
getopts option_string variable
在上述例子中使用腳本:
while getopts ahfgv OPTION
可以看出while循環用于讀取命令行,option_string為指定的5個選項(- a,- h,- f,- g,- v),腳本中variable為OPTION。注意這里并沒有用連字符指定每一單個選項。
使用getopts指定變量取值
有時有必要在腳本中指定命令行選項取值。getopts為此提供了一種方式,即在option_string中將一個冒號放在選項后。例如:
getopts ahfvc: OPTION
上面一行腳本指出,選項 a、h、f、v可以不加實際值進行傳遞,而選項 c必須取值。使用選項取值時,必須使用變量 OPTARG保存該值。如果試圖不取值傳遞此選項,會返回一個錯誤信息。錯誤信息提示并不明確,因此可以用自己的反饋信息屏蔽它,方法如下:
將冒號放在option_string開始部分。
訪問取值方式
getopts的一種功能是運行后臺腳本。這樣可以使用戶加入選項,指定不同的磁帶設備以備份數據。使用getopts實現此任務的基本框架如下:
向腳本傳遞參數時,有時需要將每一個參數偏移以處理選項,這就是 shift命令的功能。它每次將參數位置向左偏移一位,下面用一段簡單腳本詳述其功能。腳本使用 while循環反饋所有傳遞到腳本的參數。使用shift命令來處理傳遞到腳本的每一個參數:
- #!/bin/sh
- loop=0
- while [ $# -ne 0 ]
- do
- echo $1
- shift
- done
使用shift處理文件大小寫轉換
- !#/bin/sh
- # tr_case
- # convert files to either upper or lower case
- FILES=""
- TRCASE=""
- EXT=""
- OPT=no
- # gets called when a conversion fails
- error_msg()
- {
- _FILENAME=$1
- echo "`basename $0`: Error the conversion failed on $_FILENAME"
- }
- if [ $# -eq 0 ]
- then
- echo "For more info try `basename $0` --help"
- exit 1
- fi
- while [ $# -gt 0 ]
- do
- case $1 in
- #set the variables based on what option was used
- -u)TRCASE=upper
- EXT=".UC"
- OPT=yes
- shift
- ;;
- -l)TRCASE=lower
- EXT=".LC"
- OPT=yes
- shift
- ;;
- -help) echo "convert a file(s) to uppercase from lowercase"
- echo "convert a file(s) from lowercase to uppercase"
- echo "will convert all characters according to the sepcified command option."
- echo "where option is"
- echo "-l Convert to lowercase"
- echo "-u Convert to uppercase"
- echo "The original file(s) is not touched. A new file(s)"
- echo "will be created with either a .UC or .LC extension"
- echo "usage: $0 -[l|u] file [file..]"
- exit 0
- ;;
- -*) echo "usage: `basename $0` -[l|u] file [file..]"
- exit 1
- ;;
- *) # collect the files to process
- if [ -f $1 ]
- then
- # add the filenames to a variable list
- FILES=$FILES" "$1
- else
- echo "`basename $0` : Error cannot find the file $1"
- fi
- shift
- ;;
- esac
- done
- if [ "$OPT" = "no" ]
- then
- echo "`basename $0`: Error you need to specify an option. No action taken"
- echo "`basename` --help"
- exit 1
- fi
- # now read in all the file(s)
- # use the variable LOOP, I just love the word LOOP
- for LOOP in $FILES
- do
- case $TRCASE in
- lower) cat $LOOP|tr "[a-z]" "[A-Z]" >$LOOP$EXT
- if [ $? != 0 ]
- then
- error_msg $LOOP
- else
- echo "Converted file called $LOOP$EXT"
- fi
- ;;
- upper) cat $LOOP|tr "[A-Z]" "[a-z]" > $LOOP$EXT
- if [ $? != 0 ]
- then
- error_msg $LOOP
- else
- echo "Converted file called $LOOP$EXT"
- fi
- ;;
- esac
- done
getopts命令
- #!/bin/sh
- #getopts1
- #set the vars
- ALL=false
- HELP=false
- FILE=false
- VERBROSE=false
- while getopts ahfgv OPTION
- do
- case $OPTION in
- a)ALL=true
- echo "ALL is $ALL"
- ;;
- h)HELP=true
- echo "HELP is $HELP"
- ;;
- f)FILE=true
- echo "FILE is $FILE"
- ;;
- v)VERBOSE=true
- echo "VERROSE is $VERROSE"
- ;;
- esac
- done
- /home/l/g/tomotoboy/getopts >getopts1 -a
- ALL is true
- /home/l/g/tomotoboy/getopts >getopts1 -v
- VERROSE is
- /home/l/g/tomotoboy/getopts >getopts1 -v -a -h
- VERROSE is
- ALL is true
- HELP is true
getopts option_string variable
在上述例子中使用腳本:
while getopts ahfgv OPTION
可以看出while循環用于讀取命令行,option_string為指定的5個選項(- a,- h,- f,- g,- v),腳本中variable為OPTION。注意這里并沒有用連字符指定每一單個選項。
使用getopts指定變量取值
有時有必要在腳本中指定命令行選項取值。getopts為此提供了一種方式,即在option_string中將一個冒號放在選項后。例如:
getopts ahfvc: OPTION
上面一行腳本指出,選項 a、h、f、v可以不加實際值進行傳遞,而選項 c必須取值。使用選項取值時,必須使用變量 OPTARG保存該值。如果試圖不取值傳遞此選項,會返回一個錯誤信息。錯誤信息提示并不明確,因此可以用自己的反饋信息屏蔽它,方法如下:
將冒號放在option_string開始部分。
- while getopts :ahfgvc: OPTION
- #!/bin/sh
- #getopts1
- #set the vars
- ALL=false
- HELP=false
- FILE=false
- VERBROSE=false
- COPIES=0 #the value for the -c option is set to zero
- while getopts :ahfgvc: OPTION
- do
- case $OPTION in
- a)ALL=true
- echo "ALL is $ALL"
- ;;
- h)HELP=true
- echo "HELP is $HELP"
- ;;
- f)FILE=true
- echo "FILE is $FILE"
- ;;
- v)VERBOSE=true
- echo "VERROSE is $VERROSE"
- ;;
- c)COPIES=$OPTARG
- echo "COPIES is $COPIES"
- ;;
- \?) #usage stagement
- echo "`basename $0` -[a h f v] -[c value] file" >&2
- ;;
- esac
- done
- /home/l/g/tomotoboy/getopts >chmod 755 getopts2
- /home/l/g/tomotoboy/getopts >getopts2 -c hello
- COPIES is hello
- /home/l/g/tomotoboy/getopts >getopts2 -h -c hello
- HELP is true
- COPIES is hello
訪問取值方式
getopts的一種功能是運行后臺腳本。這樣可以使用戶加入選項,指定不同的磁帶設備以備份數據。使用getopts實現此任務的基本框架如下:
- #!/bin/sh
- QUITE=n
- DEVICE=awa
- LOGFILE=/tmp/logbackup
- usage()
- {
- echo "Usage: `basename $0` -d [device] -l [logfile] -q"
- exit 1
- }
- if [ $# = 0 ]
- then
- usage
- fi
- while getopts :qd:l: OPTION
- do
- case $OPTION in
- q) QUIET=y
- LOGFILE="/tmp/backup.log"
- ;;
- d) DEVICE=$OPTARG
- ;;
- l) LOGFILE=$OPTARG
- ;;
- \?)usage
- ;;
- esac
- done
- echo "you chose the following options...I can now process these"
- echo "Quite = $QUIET $DEVICE $LOGFILE"