[b][size=medium]shift命令[/size][/b]
向脚本传递参数时,有时需要将每一个参数偏移以处理选项,这就是 shift命令的功能。它每次将参数位置向左偏移一位,下面用一段简单脚本详述其功能。脚本使用 while循环反馈所有传递到脚本的参数。使用shift命令来处理传递到脚本的每一个参数:
使用shift处理文件大小写转换
[b][size=medium]getopts命令[/size][/b]
getopts option_string variable
在上述例子中使用脚本:
while getopts ahfgv OPTION
可以看出while循环用于读取命令行,option_string为指定的5个选项(- a,- h,- f,- g,- v),脚本中variable为OPTION。注意这里并没有用连字符指定每一单个选项。
[b]使用getopts指定变量取值[/b]
有时有必要在脚本中指定命令行选项取值。getopts为此提供了一种方式,即在option_string中将一个冒号放在选项后。例如:
getopts ahfvc: OPTION
上面一行脚本指出,选项 a、h、f、v可以不加实际值进行传递,而选项 c必须取值。使用选项取值时,必须使用变量 OPTARG保存该值。如果试图不取值传递此选项,会返回一个错误信息。错误信息提示并不明确,因此可以用自己的反馈信息屏蔽它,方法如下:
将冒号放在option_string开始部分。
[b]访问取值方式[/b]
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
[b][size=medium]getopts命令[/size][/b]
#!/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一般格式为:
getopts option_string variable
在上述例子中使用脚本:
while getopts ahfgv OPTION
可以看出while循环用于读取命令行,option_string为指定的5个选项(- a,- h,- f,- g,- v),脚本中variable为OPTION。注意这里并没有用连字符指定每一单个选项。
[b]使用getopts指定变量取值[/b]
有时有必要在脚本中指定命令行选项取值。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
[b]访问取值方式[/b]
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"