shell script 交互式脚本,可以读取命令行参数、选项,用户输入数据
2016-05-13 22:48
561 查看
1.命令行参数
1.1一般参数
1.1.1读取参数位置参数是标准的数字:$0是程序名,$1~$9是9个参数,如果脚本需要多于9个参数,需要在变量数字周围加花括号,比如${10}、${11}、、、、可以实现向脚本添加任意多要用的命令行参数。
#!/bin/bash total1=$[ $1 * $2 ] total2=$[ ${11} * ${12} ] echo The tenth number is ${10} echo The eleventh number is ${11} echo The twelfth number is ${12} echo The total1 is $total1 echo The total2 is $total2 执行./shell1.sh 1 2 3 4 5 6 7 8 9 10 11 12 The tenth number is 10 The tenth number is 11 The tenth number is 12 The total1 is 2 The total2 is 132
1.1.2读取程序名$0
$0参数获取shell在命令行启动的程序的名字。basename命令可以获取脚本名字
可以编写一些基于所用脚本名而执行不同功能的脚本
#!/bin/bash echo The command entered is:$0 执行结果:会带有路径,将第一个参数直接输出 caishu@lab403-1F:~$ /home/caishu/shell_script/shell1.sh The command entered is: /home/caishu/shell_script/shell1.sh caishu@lab403-1F:~/shell_script$ ./shell1.sh The command entered is: ./shell1.sh 改善:basename命令会只返回脚本名字 #!/bin/bash name=`basename $0` echo The command entered is:$bansename 执行结果:输出脚本名字 caishu@lab403-1F:~$ /home/caishu/shell_script/shell1.sh The command entered is: shell1.sh caishu@lab403-1F:~/shell_script$ ./shell1.sh The command entered is: shell1.sh
1.1.3测试参数 if [ -n $1 ]
在shell脚本中使用命令参数时,若执行脚本时,没有带参数,执行会报错
因此在写脚本时应先测试参数是否存在,可以用 if 语句测试,例如 if [ -n $1 ]
然后再对参数进行处理,若检测到没有参数,则给出提示。
1.2.特殊参数变量 $#、$* 和$@
1.2.1参数计数$#$#— —含有脚本运行时命令行参数的个数 注:${$#}不能反回最后一个参数值,应改为${!#}
#!/bin/bash #catch the last parameter parameter=$# echo the last parameter is $parameter echo the last parameter is ${!#} echo the last parameter is ${$#} 执行结果: 当有参数时,parameter和${!#}返回值一样 caishu@lab403-1F:~/shell_script$ ./shell2.sh 1 2 3 4 5 the last parameter is 5 the last parameter is 5 the last parameter is 27804 当无参数时$parameter返回0,而${!#}返回$0参数所指——脚本名 caishu@lab403-1F:~/shell_script$ ./shell2.sh the last parameter is 0 the last parameter is ./shell2.sh the last parameter is 27803
1.2.2抓取所有数据$*和$@
$* 和$@提供了对所有命令行参数的访问
$* 将命令行上所有参数当成一个参数
$@以空格为分隔符,单独处理每一个参数
1.3移动变量shift
shift命令可以移动变量,将每一个参数变量减一,所以变量$3的值会移到$2,变量$2的值会移到$1,变量$1的值会删除。(变量$0不会改变)shift n 实现多位移动,shift 2移动2位
应用:在不知道命令行有多少参数时,可以只操作第一个参数,移动参数,然后继续操作第一个参数,知道检测到第一个参数长度为0,以实现对所有参数的操作。
注:shift使用要小心,当参数被移除后,被丢掉无法恢复。
2.处理选项
选项(options):跟在单破折线后的单个字母,能改变命令的行为2.1查找选项
2.1.1处理简单选项使用case语句检查每个选项,是不是有效选项。
#!/bin/bash #处理选项 while [ -n "$1" ] do case "$1" in -a)command;; -b)command;; -c)command;; -d)command;; ... *)command;; esac shift done
2.1.2分离参数和选项
选项和参数同时存在时,使用双破直线(--)将二者分开,shell会用(--)来表明选项结束
#!/bin/bash #处理选项和参数 while [ -n "$1" ] do case "$1" in -a)command;; -b)command;; -c)command;; -d)command;; ... *)command;; esac shift done for parameter in $@ do command done 执行时选项和参数要用--分开,如:./test -a -b -c -- parameter1 parameter2 parameter3
2.1.3处理带值的选项
有些选项会带有额外的参数值,当命令行选项带有额外参数时,脚本正确检测和处理.
如下:-b选项有额外参数,由于要处理的选项为$1,因此额外的参数就应该位于$2位置,因此设置shift命令移动2次
#!/bin/bash #处理带参数的选项 while [ -n "$1" ] do case "$1" in -a)command;; -b)parameter=$2 command shift;; -c)command;; ... --)shift break;; *)command;; esac shift done for parameter in $@ do command done
2.2使用getopt命令
2.2.1命令格式getopt命令可以接受一系列任意形式的命令行选项和参数,并自动将它们转换成适当的格式。
getopt optstring options parameters
optstring 定义了选项有效字母,还定义了哪些选项需要参数(加:);如果指定了一个不在optstring中的选项,getopt命令会产生一条错误消息,若想忽略错误,则在getopt后加-q:
$ getopt ab:cd -a parameter1 -b parameter2 -cde parameter3 getopt 无效选项 --e -a -b parameter2 -c -d -- parameter1 parameter3 $ getopt -q ab:cd -a parameter1 -b parameter2 -cde parameter3 -a -b 'parameter2' -c -d -- 'parameter1' 'parameter3'
2.2.2在脚本中使用getopt
set --命令的带双破折线选项,它会将命令行参数替换成set命令的命令行的值。在脚本中使用方法:
set -- `getopt -q ab:c "$@"` #注意反引号 #!/bin/bash #使用getopt set -- `getopt -q ab:c "$@"` while [ -n "$1" ] do case "$1" in -a)command;; -b)parameter=$2 command shift;; -c)command;; ... --)shift break;; *)command;; esac shift done for parameter in $@ do command done
2.3使用高级的getopts命令
命令格式:getopts optstring variable optstring与之前相同,忽略错误信息不是-q,而是:optstring getopts命令将当前参数保存在variable中。getopts命令用到两个环境变量:OPTARG和OPTIND OPTARG保存了选项需要跟的参数值;OPTIND 保存了正在处理的参数位置
#!/bin/bash #使用getopts while getopts :ab:c opt do case "$opt" in a)command;; b) with value $OPTARG command shift;; c)command;; ... *)command;; esac done
注:getopts命令解析命令行选项时,移除开头的单破折线-(脚本中a b c前面都没有-)
执行脚本时字母和选项可以放在一起不用加空格:./test -abparameter -c (-c 前必须空格,否则无法识别-c选项,会被当成-b参数的一部分)
getopts命令知道何时停止处理选项,并将参数留给你处理。在getopts处理每个选项时,OPTIND环境变量的值增一,处理完选项后,加一句shift $[ $OPTIND -1 ] 作为处理参数的开始
#!/bin/bash #使用getopts while getopts :ab:c opt do case "$opt" in a)command;; b) with value $OPTARG command shift;; c)command;; ... *)command;; esac done
shift $[ $OPTIND -1 ]
for para in "$@"
do
command
done
2.4将选项标准化
在创建shell脚本时,尽量保持选项与Linux通用的选项含义相同,Linux通用选项有:-a 显示所有对象 -c 生产一个计数 -d 指定一个目录 -e 扩展一个对象 -f指定读入数据的文件 -h显示命令的帮助信息 -i 忽略文本大小写 -l 产生输出得长格式文本 -n 使用非交互模式 -o 指定将所有输出重定向到输出文件 -q 以安静模式运行 -r 递归的处理目录和文件 -s 以安静模式运行 -v 生成详细输出 -x 排除某个对象 -y 对所有问题回答yes
3.获得用户输入read
3.1基本读入
read接受用户从键盘的输入:#!/bin/bash echo -n "Enter your name:" #-n选项移调末尾换行符,不换行 read name echo "Hello $name" 输出结果 Enter your name: caishu Hello caishu
read的-p选项,直接置顶参数:
read -p "Enter your name:" name echo "Hello $name"
read命令会为每个提示符分配变量,若提示符用完了,则将剩下的所有变量,分配给最后一个提示符。
若在read命中不指定变量,read命令会将它收到的任何数据都放进特殊环境变量REPLY中,
$read -p "Enter your para:" para1 para2 para3; echo "your parameter is $para1,$para2,$para3..." Enter your para:1 2 3 4 5 6 7 your parameter is 1,2,34567... $read -p "Enter your para:" ; echo "your parameter is $REPLY" Enter your para:caishu your parameter is caishu
3.2超时
-t:使用read命令时,会一直等用户输入,可以用-t选项来制定计时器,当计时器过期后,read命令会返回一个非零退出状态码。read -t 5 -p "Please enter your name: " name #会等5s,可以改变数字以改变等的时间
-n和数字:对输入的字符计数,当输入的字符达到预设的字符数时,它会自动退出,将输入的数据赋给变量。
read -n1 -p "Please enter your name: " name #只接受一个字符,空格也是字符。
3.3隐藏方式读取
-s 阻止用户的输入显示在显示器上,(实际上,数据会被显示,只是read命令将文本颜色设置成跟背景颜色一样)3.4从文件中读取
read line 会从文本中读取一行,用cat命令的输出通过管道传给含有read的while命令#!/bin/bash #read data from a file count=1 cat filename | while read line do echo "Line $count: $line count=$[ $count + 1 ] done echo "Finished processing the file"
相关文章推荐
- 第二节windows系统下Xshell 5软件远程访问虚拟机 Linux系统
- shell原理和问答机制引入
- linux shell脚本监控端口
- clone repository in git shell under windows
- Shell脚本学习指南(二)——变量、条件、循环
- hbase基本概念和hbase shell常用命令用法
- linux shell 特殊变量
- shell脚本+x
- ~/.bashrc 和 ~/.bash_profile 和 /etc/bashrc 和 /etc/profile 脚本的区别和启动加载顺序
- Windows日志管理相关脚本
- Delete log files longer than 45 days
- shell-5.shell echo 命令
- Deploy NRPE config files
- Powershell check server patch status
- 在shell中如何把10进制字符转换成16进制形式输出?
- shell-4.shell双引号,单引号,反引号,反斜杠
- 用shell监控站点目录下的文件是否被修改
- shell-3.shell特殊变量
- shell脚本中修改文件的内容
- shell编程笔记