1.shift命令
#!/bin/sh
#opt2.sh
loop=0
while [ $# -ne 0 ]
do
echo $1
shift
done
结果:
[root@localhost yjg]# ./opt2.sh 1 2 3 4 5
1
2
3
4
5
[root@localhost yjg]#
2.命令行输入的最后一个参数
eval echo /$$#
shift 'expr $#-2'
3.使用shift处理文件转换
#!/bin/sh
#tr_case.sh
function usage(){
echo "usage: ` basename $0` -[l|u] file [files] " >&2
exit 1
}
if [ $# -eq 0 ]; then
usage
fi
while [ $# -gt 0 ]
do
case $1 in
-u|-U) echo "-u option specified"
shift
;;
-l|-L) echo "-l option specified"
shift
;;
*) usage
;;
esac
done
4.getopts命令
#!/bin/sh
#getopt.sh
ALL=false
HELP=false
FILE=false
VERBOSE=false
while getopts ahfgv OPTION
do
case $OPTION in
a)ALL=true
echo "ALL is $ALL"
;;
h)HELP=true
echo "ALL is $HELP"
;;
f)FILE=true
echo "FILE is $FILE"
;;
v)VERBOSE=true
echo "VERBOSE is $VERBOSE"
;;
esac
done
4.getopts : ahfgvc : OPTION
则选取c 时,必须赋值
#!/bin/sh
#getopt.sh
ALL=false
HELP=false
FILE=false
VERBOSE=false
COPIES=0
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 "VERBOSE is $VERBOSE"
;;
c)COPIES=$OPTARG
echo "COPIES is $COPIES"
/?)
echo "`basename $0` -[a h f v] -[c value] file" >&2
;;
esac
done
[root@localhost yjg]# ./getopts.sh -a -h -c 3