shift命令 getopts
一。shift
每次将参数值向左偏移N位
#!/bin/bash
#opt2
usage()
{
echo "usage :`basement $0` filenames"
totalline=0
if[ $# -lt 2 ]
then
usage
fi
while[ $# -ne 0 ]
do
line=`cat $1|wc-l`
echo "$1:${line}"
totalline=$[ $totalline+$line ]
shift
done
echo "--------"
echo "total :${totalline}"
二。getopts
获得多个命令行参数
#!/bin/bash
ALL=false
HELP=false
FILE=false
VERBOSE=false
while getopts ahfvs 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"
;;
c)
echo "c is $c"
;;
\?)echo "`basename $0` -[a h f v] -[c value] file"
;;
esac
done
./opt -a -h -f -v -c
如果给c赋值需要在ahfvc c后面加:说明需要赋值 如下
#!/bin/bash
ALL=false
HELP=false
FILE=false
VERBOSE=false
while getopts ahfvc: 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"
;;
c)
c=$var
echo "c is $c"
;;
\?)echo "`basename $0` -[a h f v] -[c value] file"
;;
esac
done
./opt -a -h -f -v -c value
阅读(625) | 评论(0) | 转发(0) |