香港六马会开奖结果-六合彩特码资料-本港台最快开奖直播

[2019]让您放心游戏,提供六合彩特码资料最丰厚回馈!,本港台最快开奖直播是为您电脑安全以及资金安全的考虑,所以说压实向感受娱乐首先应该进下载时必不可少的。

基础教程
分类:业界快讯

焦点用法

    read命令主要用于从规范输入读取内容或从文件中读取内容,并把音讯保存到变量中。其常用用法如下所示:

read [选项] [文件]
选项 解释
-a array 将内容读取到数值中,变量默认为数组且以空格做为分割符
-d delimiter 遇到指定的字符即停止读取
-n nchars 指定最多可以读入的字符数,即定义输入文本的长度
-r 屏蔽转义符
-p prompt 显示提示信息
-s 静默模式,在输入字符时不在终端中显示,常用于密码输入等
-t timeout 指定超时时间
-u FD 从文件描述符中读入,该FD可以由exec开启

用法示例

1、从专门的学业输入读入

[root@localhost test]# cat read.sh
#!/bin/bash
echo -n "Please input your name:"
read name
echo "Hello $name"
[root@localhost test]# bash read.sh
Please input your name:Jack
Hello Jack

2、钦命显示新闻从正式输入读入

[root@localhost test]# cat read.sh
#!/bin/bash
# echo -n "Please input your name:"
read -p "Please input your name:" name
# read name
echo "Hello $name"
[root@localhost test]# bash read.sh
Please input your name:Jack
Hello Jack

在以上示例中,read是叁遍能够承当八个参数的,如下所示:

read -p "Please input your name:" firstName secondName lastName

但要求小心的事项如下:

  • 如果输入的数量少于变量个数,则多余的变量不会收获到数码,即变量值为空
  • 生龙活虎旦输入的多寡多于变量个数,则出乎的数据将全方位赋给最后三个变量
  • 如果在read命令后边未有概念任何变量,脚本在施行时,若是客户输入数据,那个时候数码则保留随境况变量$REPLY

3、钦点超时时间

[root@localhost test]# cat read.sh
#!/bin/bash
if read -t 3 -p "Please input your name:" firstName secondName lastName
then
  echo "variable is $firstName - $secondName - $lastName"
else
   echo -e  "ntimeoutn"
fi
[root@localhost test]# bash read.sh
Please input your name:
timeout

4、从内定文件中读取内容

[root@localhost test]# cat -n test.txt
     1  this is test text.
     2  this is second line.
     3  Hello world
     4  C# Main
     5  Python
# 使用-u选项
[root@localhost test]# cat readtest.sh
#!/bin/bash
exec 5< ~/test/test.txt
count=0
while read -u 5 var
do
 let count=$count+1
 echo "Line $count is $var"
done
echo "Total line count is $count"
exec 5<&-
[root@localhost test]# bash readtest.sh
Line 1 is this is test text.
Line 2 is this is second line.
Line 3 is Hello world
Line 4 is C# Main
Line 5 is Python
Total line count is 5
# 使用管道
[root@localhost test]# cat readtest.sh
#!/bin/bash
count=1
cat ~/test/test.txt |  while read line
do
 echo "Current line $count - $line "
 let count=$count+1
done
echo "Total line count is $count"
[root@localhost test]# bash readtest.sh
Current line 1 - this is test text.
Current line 2 - this is second line.
Current line 3 - Hello world
Current line 4 - C# Main
Current line 5 - Python
Total line count is 1
# 使用重定向
[root@localhost test]# cat readtest.sh
#!/bin/bash
count=0
while read line
do
 let count=$count+1
 echo "Current line $count - $line "
done < ~/test/test.txt
echo "Total line count is $count"
[root@localhost test]# bash readtest.sh
Current line 1 - this is test text.
Current line 2 - this is second line.
Current line 3 - Hello world
Current line 4 - C# Main
Current line 5 - Python
Total line count is 5

本文同步在微信订阅号上颁发,如各位小友人们欣赏作者的篇章,也得以关怀自己的微信订阅号:woaitest,或扫描上面包车型地铁二维码增加关心:
图片 1

本文由香港六马会开奖结果发布于业界快讯,转载请注明出处:基础教程

上一篇:命令学习 下一篇:没有了
猜你喜欢
热门排行
精彩图文