Linux培训
达内IT学院

400-111-8989

Linux知识分享-Linux中read命令的使用方法有哪些?


今天小编要跟大家分享的Linux小知识是关于在Linux系统中read命令的使用方法有哪些?熟悉Linux系统的小伙伴都知道,Linux的操作离不开Linux命令,Linux命令有很多不同的命令用那个法也不同,今天小编就来跟大家一起说一说Linux中read命令的使用方法有哪些。

Linux中read命令的使用方法有哪些?

1、read基本读取

1 #!/bin/bash

2 #testing the read command

3

4 echo -n "Enter you name:" #echo -n 让用户直接在后面输入

5 read name #输入的多个文本将保存在一个变量中

6 echo "Hello $name, welcome to my program."

执行:

# ./read.sh

Enter you name: wangtao

Hello wangtao, welcome to my program.

2、read -p (直接在read命令行指定提示符)

1 #!/bin/bash

2 #testing the read -p option

3 read -p "Please enter your age: " age

4 days=$[ $age * 365 ]

5 echo "That makes you over $days days old!"

执行:

# ./age.sh

Please enter your age: 23

That makes you over 8395 days old!

3、read -p (指定多个变量)

1 #!/bin/bash

2 # entering multiple variables

3

4 read -p "Enter your name:" first last

5 echo "Checking data for $last, $first"

执行:

# ./read1.sh

Enter your name: a b

Checking data for b, a

4、read 命令中不指定变量,那么read命名将它收到的任何数据都放在特殊环境变量REPLY中

1 #!/bin/bash

2 # testing the REPLY environment variable

3

4 read -p "Enter a number: "

5 factorial=1

6 for (( count=1; count<= $REPLY; count++ ))

7 do

8 factorial=$[ $factorial * $count ] #等号两端不要有空格

9 done

10 echo "The factorial of $REPLY is $factorial"

执行:

./read2.sh

Enter a number: 6

The factorial of 6 is 720

5、超时, 等待输入的秒数(read -t)

1 #!/bin/bash

2 # timing the data entry

3

4 if read -t 5 -p "Please enter your name: " name #记得加-p参数, 直接在read命令行指定提示符

5 then

6 echo "Hello $name, welcome to my script"

7 else

8 echo

9 echo "Sorry, too slow!"

10 fi

执行:

# ./read3.sh

Please enter your name:

Sorry, too slow!

# ./read3.sh

Please enter your name: wang

Hello wang, welcome to my script

6、read命令对输入的字符判断

1 #!/bin/bash

2 # getting just one character of input

3

4 read -n1 -p "Do you want to continue [Y/N]? " answer

5 case $answer in

6 Y | y) echo

7 echo "fine, continue on...";;

8 N | n) echo

9 echo "OK, goodbye"

10 exit;;

11 esac

执行:

# ./read4.sh

Do you want to continue [Y/N]? y

fine, continue on...

./read4.sh

Do you want to continue [Y/N]? n

OK, goodbye

7、隐藏方式读取(read -s)

1 #!/bin/bash

2 # hiding input data from the monitor

3

4 read -s -p "Enter your passwd: " pass #-s 参数使得read读入的字符隐藏

5 echo

6 echo "Is your passwd readlly $pass?"

~

执行:

# ./read5.sh

Enter your passwd:

Is your passwd readlly osfile@206?

8、从文本中读取

1 #!/bin/bash

2 # reading data from a file

3

4 count=1

5 cat test | while read line

6 do

7 echo "Line $count: $line"

8 count=$[ $count + 1 ]

9 done

10 echo "Finished processing the file"

执行:

./read6.sh

Line 1: The quick brown dog jumps over the lazy fox.

Line 2: This is a test, this is only a test.

Line 3: O Romeo, Romeo! Wherefore art thou Romeo?

Finished processing the file

以上就是小编今天为大家分享的关于Linux中read命令的使用方法有哪些?的文章,希望本篇文章能够对正在从事Linux相关工作的小伙伴们有所帮助。想要了解更多Linux相关知识记得关注达内Linux培训官网。

原文链接:#/wangtao1993/p/6136169.html

【免责声明:本文图片及文字信息均由小编转载自网络,旨在分享提供阅读,版权归原作者所有,如有侵权请联系我们进行删除。】

预约申请免费试听课

填写下面表单即可预约申请免费试听! 怕学不会?助教全程陪读,随时解惑!担心就业?一地学习,可全国推荐就业!

上一篇:Linux运维人员你知道Linux中的2>&1是什么意思吗?
下一篇:Linux运维人员应知道的Linux磁盘管理命令!

运维工程师经常加班吗?

运维工程师简历专业技能?

公司运维部工作方案

铁路局公务运维是干什么的

  • 扫码领取资料

    回复关键字:视频资料

    免费领取 达内课程视频学习资料

Copyright © 2023 Tedu.cn All Rights Reserved 京ICP备08000853号-56 京公网安备 11010802029508号 达内时代科技集团有限公司 版权所有

选择城市和中心
黑龙江省

吉林省

河北省

湖南省

贵州省

云南省

广西省

海南省