我们知道,在Shell中接收传入的参数有两种方式。一种是通过脚本进行参数传递,另外一种是通过read来接收传入的参数。通过脚本来传递参数的简单示例如下: # 通过脚本来传递,这里$0指脚本名,$1为第一个参数,$2为第二个参数 [root@host ~]# ./script.sh 1 2 Total = 3 [root@host ~]# vim script.sh #!/bin/bash function add() { total=$(expr $1 + $2) echo -e "Total = $total" } add $1 $2 再来看通过read来接收传入的参数,先看read的基本格式: read [-rs] [-a ARRAY] [-d delim] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [var1 var2 var3......] [root@host ~]# ./script.sh Enter Password: The password your input is: Test@1234\ [root@host ~]# vim script.sh #!/bin/bash read -n10 -t30 -r -s -d $ -p "Enter Password:" password echo -e "\nThe password your input is:$password" -p 提示语句,后面接输入提示信息,这里为'Enter Password: ' -n 参数个数,有时候要限制密码长度,或者其他输入长度限制,比如[Y/N],只输入输入一位,-n1 -s 屏蔽回显,屏幕上不显示输入内容,一般用于密码输入 -t 等待时间,这里设置30秒,30秒内未输入或者输入不全,终止 -d 输入界限,这里是$,输入到$,自然终止输入 -r 屏蔽特殊字符\的转译功能,加了之后作为普通字符处理 从上面一个例子,基本上囊括了上面的大部分常用功能,特别是-p,-n,-t,-s等参数,可以很好的学习read这个命令 (责任编辑:IT) |