使用:getopt命令
1:getopt命令的格式:getopt optstring parameters
optstring 是这个过程的关键所在,它定义了命令行有效的选项字母,然后每个需要参数数值的选项字母后家一个冒号。getopt命令会基于你定义的optstring解析提供的参数。
例如:getopt ab:cd -a -b test1 -cd test2 test3
optstring定义了四个有效的选项字母 a ,b ,c ,d 冒号:被放在字母后面,因为b选项需要一个参数,当getopt命令运行时候,他会检查提供的参数列表(-a -b test1 -cd test2 test3),并基于提供的optstring 进行解析,注意他会自动将-cd选项分成来两个单独的选项,并插入双破折线来分割行中的额外参数
如果指定了一个不在optstring中的选项,默认情况下,getopt命令会产生一个错误信息
如果想忽略这条错误信息,可以在命令后加上-q选项
getopt -q ab:cd -a -b test1 -cde test2 tese3
在脚本中使用getopt命令
eg:
#!/bin/bash
set --$(getopt -q ab:cd "$@")
#
echo
while [ -n "$1" ]
do
case "$1" in
-a) echo "found the -a option";;
-b) param="$2";;
echo "found the -b option,with parameter value $param";;
-c) echo "found the -c option";;
-- ) shift
break;;
*) echo "$1 is not an option";;
esac
shift
done
#
count=1
for param in "$@"
do
echo "paarmeter #$count:$param"
count=$ [ $count + 1 ]
done
输入:./test.sh -a -b test1 -c test2 test3是没有问题的,但是如果输入
./test.sh -a -b test -c "test2 test4"就会出现问题
found the -a option
found the -b option with value test
found the -c option
parameter #1 :test2
parameter #2 : test4
getopt 命令并不擅长处理带空格的参数值,他会将空格的当作参数分隔符对待,而不是根据双引号将二者的当作一个参数来处理,接下来就是getopts登场的时候了
2:getopts命令
getopts 命令是用来扩展getopt命令的
基本格式:getopts optstring variable
opstring 的值类似与getopt命令,有效的选项字母都会列在optstring中,如果选项字母要求有个参数值的,家一个冒号。如果想要去掉错误消息的话,可以在optstring前加上一个冒号,getopts命令将当前参数保存到命令行中定义的
variable中
getopts会用到两个环境变量,如果选项需要跟一个参数值,OPTARG环境变量就会保存这个值,OPTIND环境变量保存了参数列表中getopts正在处理的参数位置,这样你就能在处理选项之后继续处理其他命令行参数了
eg:getopts的简单列子:
echo
echo "the param opt is a storage for store the ab or c"
while getopts :ab:c opt
do
case "$opt" in
a) echo "found the -a option";;
b) echo "found the -b option,with value $OPTARG";;
c) echo "found the -c option";;
*) echo "unknown option $opt";;
esac
done
echo "you will be add some space in parameter"
输入:./test.sh -ab test1 -c
found the -a option
found the -b option with value test1found the -c option
默认情况下getopt,getopts命令会将参数中的"-"去掉,然后在case语句中判断
当然我们可以在参数中添加空格符如:./test.sh -b "test1 test2" -a
另外一个好用的功能是将选项字母和参数值放在一起使用,而不用添加空格
./test.sh -abtest1
--> found the -a option
found the -b option wiht value test1
另外getopts命令还能将命令行上的找到的未定义的选项统一输出成问号
如:./test.sh -d
unknown option : ?
getopts命令知道如何停止处理选项,并将参数留给你处理,在它处理每个选项时候,它将OPTIND环境变量值加一,
在getopts完成处理时候,你可以使用shift命令和OPTIND值来移动参数
eg:
#!/bin/bash
echo "now the OPTIND is : $OPTIND"
while getopts :ab:c opt
do
case "$opt" in
a) echo "found the -a option";;
b) echo "found the -b option,with the value:$OPTARG";;
c) echo "found the -c option,";;
d) echo "found the -d option";;
*) echo "unknown option:$opt";;
esac
echo "in while loop the OPTIND is :$OPTIND"
done
echo "out of the loop the OPTIND is :$OPTIND"
#
shift $[ $OPTIND - 1 ]
#
echo
count=1
for param in "$@"
do
echo "parameter $count:$param"
count=$[ $count + 1 ]
done
[centos@localhost test]$ ./aftergetopts.sh -a -b test1 -c -d test3 test4 test5
now the OPTIND is : 1
found the -a option
in while loop the OPTIND is :2
found the -b option,with the value:test1
in while loop the OPTIND is :4
found the -c option,
in while loop the OPTIND is :5
unknown option:?
in while loop the OPTIND is :6
out of the loop the OPTIND is :6
parameter 1:test3
parameter 2:test4
parameter 3:test5
这里注意OPTIND是从1开始的每遇到一个选项参数的时候加一,离开循环的时候,将这几个选项参数全部移动出去,接下来就是处理普通参数了
3:将选项标准化
LINUX中常用到的一些命令行选项的含义
-a 显示所有对象
-c 生成一个计数器
-d 指定一个目录
-e 扩展一个对象
-f 指定读入数据的文件
-h 显示命令的帮助信息
-i 忽略文本的大小写
-l 产生输出的长格式文本
-n 使用非交互式模式
-q 以安静模式运行
-r 第归的处理目录和文件
-s 以安静模式运行
-v 生成详细输出
-x 排除某个对象
4:获得用户输入:read命令
a:基本的读取
read命令从标准输入设备或者另一个文件描述符中接受输入,在受到输入命令后,read命令会将数据放到一个变量中去,下面是read命令的最简单用法:
eg:
#!/bin/bash
echo -n "enter your name:"
read name
echo "Hello $name,welcome to my program"
它会以提问的形式与输入者进行互动
./test.sh
enter your name:rick faker
Hello rick faker,welcome to my program
这里使用了-n选项参数,该选项不会在字符串末尾输出换行符,这看起来更像是表单。
实际上,read命令包含了-p选项,允许你直接在read命令行指定提示符。
eg:
#!/bin/bash
read -p "Enter your name:" name
echo "Hello $name,welcome to my program"
但是问题是read命令会将姓和名放到同一个变量中去了,这时候可以指定多个变量来存储它们
eg:
#!/bin/bash
read -p "Enter you first name and last name:" first_name last_name
echo "Hello $last_name $first_name,welcome to my program"
当然了你也可以选择在read命令行中不指定变量,如果这样的话,read命令会将它收到的任何数据都放进特殊环境变量REPLY中去,
#!/bin/bash
read -p "Enter your name:"
echo "Hello $REPLY,welcome to my program"
效果也是一样的
b:超时效果:-t
-t选项指定了read命令等待输入的秒数,当记时器过期后会返回一个非零的推出状态码
如:
#!/bin/bash
if read -t 6 -p "please enter your name:" name
then
echo "Hello $name,welcome to my sr"
else
echo "Sorry ,too slow"
fi
./test.sh
please enter your name:rick
Hello rick,welcome to sr
如果超过六秒时间没有输入任何数据的话就会提示 Sorry too slow
c:隐藏方式读取: -s
有时候你需要从脚本用户处得到输入,但又在屏幕上显示输入信息,其中典型的例子就是输入密码信息
eg:
#!/bin/bash
read -s -p "enter you password:" password
echo 'is this your password:$password"
d:从文件中读取:每次调用read命令,他都会从文件中读取一行文本,当文件中没有内容可以读取的时候,read命令会退出并返回非零退出状态码。
其中最难的部分就是将文本中的数据传给read命令,最常用的方法是使用cat命令,将结果通过管道直接传给含有read命令的while命令,
#!/bin/bash
count=1
cat test | while read line
do
echo "Line $count:$line"
count=$[ $count + 1 ]
done
echo "Finished processing the file"