命令行参数和选项
传递参数
1、向脚本中传递命令行参数
脚本路径 参数1 参数2 ...
读取参数
1、位置参数
概述
bash shell会将命令行参数指派给称作 位置参数 的特殊变量。位置参数 $0 是脚本名称, $1 是第一个命令行参数,$2 是第二个命令行参数...
例:阶乘计算
#!/usr/bin/bash
echo script name is $0
res=1
for (( i=1; i<=$1;i++ ))
dores=$[ $res * $i ]
done
echo $res
测试:
~$ ./test.sh 5
script is ./test.sh
120
script is ./test.sh
~$ ./test.sh 10
3628800
两个问题
- $0 会输出文件的路径,如果只想输出文件名可以使用bashname 命令。 $(basename $0)
- 如果位置参数多于9个需要用大括号将数值部分括起来。如:${10}
2、特殊参数变量
$# 包含命令行参数的个数
间接引用位置参数的时候需要用大括号将数值部分括起来.并将大括号内的$替换成 !
echo Parameter number : $#
for (( i=1;i<=$#;i++ ))
doecho Parameter $i is ${!i}
done
~$ ./test.sh 10 20
Parameter number : 2
Parameter 1 is 10
Parameter 2 is 20
$* 会将所有命令行参数当作一个单词
$@ 会将所有命令行参数当作一个字符串中的多个单词
#!/usr/bin/bash
echo \$*-------$*
for var in "$*"
doecho $var
doneecho "\$@-------$@"
for var in "$@"
doecho $var
done
输出:
~$ ./test.sh first second
$*-------first second
first second
$@-------first second
first
second
移动参数
概述
shift命令可用于操作命令行参数,默认情况下shift将每个位置上变量左移一位。命令格式: shift n #n默认为1
例:用shift命令遍历命令行参数
count=1
while [ -n "$1" ]
doecho param \#$count = $1count=$[ $count + 1 ]shift
done
输出:
~$ ./test.sh first second third
param #1 = first
param #2 = second
param #3 = third
处理选项
1、像处理命令行参数一样处理选项
例:输出命令选项
#!/usr/bin/bash
while [ -n "$1" ]
do
case "$1" in-a|-b|-c) echo found "$1" option;;*) echo "$1" not an option;;esacshift
done
输出
$ ./test.sh -a -b -c -d
found -a option
found -b option
found -c option
-d not an option
case命令格式参考 https://blog.csdn.net/weixin_37703001/article/details/139477905
2、分离参数和选项
经常会碰到同时使用选项和参数的情况,linux的标准做法是使用特殊字符将两者分开,该字符会告诉脚本选项何时结束,命令行参数何时开始。这个特殊字符是双连字符(--)
例:输出选项和参数
#!/usr/bin/bash
while [ -n "$1" ]
do
case "$1" in-a|-b|-c) echo found "$1" option;;--) shiftbreak;;*) echo "$1" not an option;;esacshift
donecount=1
for var in "$@"
doecho param \#$count = $varcount=$[ $count + 1 ]
done
输出
~$ ./test.sh -a -b -c arg1 arg2 arg3
found -a option
found -b option
found -c option
arg1 not an option
arg2 not an option
arg3 not an option
~$ ./test.sh -a -b -c -- arg1 arg2 arg3
found -a option
found -b option
found -c option
param #1 = arg1
param #2 = arg2
param #3 = arg3
3、处理含值的选项
有些选项需要一些额外的参数值。在这种情况下,命令行看起来像下面这样
./test.sh -a -b test1 -c
处理带参数的选项:
#!/usr/bin/bash
while [ -n "$1" ]
do
case "$1" in-a|-c) echo found "$1" option;;-b) echo found -b option with parameter value $2shift;;--) shiftbreak;;*) echo "$1" not an option;;esacshift
done
输出
~$ ./test.sh -a -b test1 -c
found -a option
found -b option with parameter value test1
found -c option
4、使用getopt命令
getopt命令格式
getopt optstring parameters
optstring:定义了有效的命令行选项,在需要参数值的选项字符后加冒号":"
在命令行中测试getopt
optstring=ab:c
parameters=“-a -b test1 -c arg1 arg2”
~$ getopt ab:c -a -b test1 -c arg1 arg2
-a -b test1 -c – arg1 arg2
在脚本中使用getopt
set命令的–选项可以把位置变量的值替换成set命令所指定的值
#!/usr/bin/bash
echo old parameter : "$@"
parameter=$(getopt ab:c "$@") set -- $parameter
echo new parameter : "$@"while [ -n "$1" ]
do
case "$1" in-a|-c) echo found "$1" option;;-b) echo found -b option with parameter value $2shift;;--) shiftbreak;;*) echo "$1" not an option;;esacshift
done
echo end
输出:
~$ ./test.sh -a -b test1 -c arg1 arg2
old parameter : -a -b test1 -c arg1 arg2
new parameter : -a -b test1 -c -- arg1 arg2
found -a option
found -b option with parameter value test1
found -c option
end
5、使用getopts命令
getopts命令格式
getopts optstring variable
optstring:定义了有效的命令行选项,在需要参数值的选项字符后加冒号":"
getopts命令会用到两个变量
- OPTIND环境变量保存参数列表中getopts正在处理的参数位置
- OPTARG环境变量保存选项的附加参数值
#!/usr/bin/bash
echo old parmeter = $@
while getopts ab:c opt
docase "$opt" ina|c) echo found "$opt" option;;b) echo found -b option with parameter value $OPTARG;;*) echo $opt not an option;;esac
doneecho OPTIND = $OPTIND
shift $[ $OPTIND-1]
echo new parameter = $@for var in "$@"
doecho $var is a parameter
done
输出
~$ ./test.sh -a -b test1 -c arg1 arg2
old parmeter = -a -b test1 -c arg1 arg2
found a option
found -b option with parameter value test1
found c option
OPTIND = 5
new parameter = arg1 arg2
arg1 is a parameter
arg2 is a parameter