Home >Operation and Maintenance >Linux Operation and Maintenance >Detailed explanation of how to use the read command to obtain the value in a variable in Linux

Detailed explanation of how to use the read command to obtain the value in a variable in Linux

巴扎黑
巴扎黑Original
2017-09-08 10:44:412095browse

对于写bash脚本的朋友,read命令是不可或缺的,需要实践一下就可以了解,下面这篇文章主要给大家介绍了关于linux如何利用read命令来获取变量中值的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。

前言

本文主要给大家介绍了关于linux利用read命令获取变量中值的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。

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&#39;-&#39; &#39;{print $1, $2}&#39;) 
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=&#39;127.127.127.10-127.127.127.14&#39; 
root@linux~# read start_ip end_ip <<-EOF 
> `echo $input_ips | awk -F&#39;-&#39; &#39;{print $1, $2}&#39;` 
> 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

The above is the detailed content of Detailed explanation of how to use the read command to obtain the value in a variable in Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn