read的常用用法如下:
read -[pstnd] var1 var2 ...
-p提示语句
-n 字符个数
-s 屏蔽回显
-t 等待时间
-d 输入分界
read来获取变量,是通过屏幕中等待用户输入的,如果是用read命令从已知的变量来获取值要如何操作呢?
比如说: input_ips='127.127.127.10-127.127.127.14',通过read命令分别读取到start_ip, end_ip两个变量中?
方法一:
root@linux~# input_ips='127.127.127.10-127.127.127.14'
root@linux~# read start_ip end_ip < <(echo $input_ips | awk -F'-' '{print $1, $2}')
root@linux~# echo -e "START_IP=$start_ip; END_IP=$end_ip"
START_IP=127.127.127.10; END_IP=127.127.127.14
方法二:
root@linux~# input_ips='127.127.127.10-127.127.127.14'
root@linux~# read start_ip end_ip <<-EOF
> `echo $input_ips | awk -F'-' '{print $1, $2}'`
> EOF
root@linux~# echo -e "START_IP=$start_ip; END_IP=$end_ip"
START_IP=127.127.127.10; END_IP=127.127.127.14