详解Shell read

要与Linux交互,脚本获取键盘输入的结果是必不可少的,read可以读取键盘输入的字符。

shell作为一门语言,自然也具有读数据的功能,read就是按行从文件(或标准输入或给定文件描述符)中读取数据的最佳选择。当使用管道、重定向方式组合命令时感觉达不到自己的需求时,不妨考虑下while read line。

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

文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/222391.html<

(0)
运维的头像运维
上一篇2025-04-15 11:40
下一篇 2025-04-15 11:42

相关推荐

  • 个人主题怎么制作?

    制作个人主题是一个将个人风格、兴趣或专业领域转化为视觉化或结构化内容的过程,无论是用于个人博客、作品集、社交媒体账号还是品牌形象,核心都是围绕“个人特色”展开,以下从定位、内容规划、视觉设计、技术实现四个维度,详细拆解制作个人主题的完整流程,明确主题定位:找到个人特色的核心主题定位是所有工作的起点,需要先回答……

    2025-11-20
    0
  • 社群营销管理关键是什么?

    社群营销的核心在于通过建立有温度、有价值、有归属感的社群,实现用户留存、转化和品牌传播,其管理需贯穿“目标定位-内容运营-用户互动-数据驱动-风险控制”全流程,以下从五个维度展开详细说明:明确社群定位与目标社群管理的首要任务是精准定位,需明确社群的核心价值(如行业交流、产品使用指导、兴趣分享等)、目标用户画像……

    2025-11-20
    0
  • 香港公司网站备案需要什么材料?

    香港公司进行网站备案是一个涉及多部门协调、流程相对严谨的过程,尤其需兼顾中国内地与香港两地的监管要求,由于香港公司注册地与中国内地不同,其网站若主要服务内地用户或使用内地服务器,需根据服务器位置、网站内容性质等,选择对应的备案路径(如工信部ICP备案或公安备案),以下从备案主体资格、流程步骤、材料准备、注意事项……

    2025-11-20
    0
  • 如何企业上云推广

    企业上云已成为数字化转型的核心战略,但推广过程中需结合行业特性、企业痛点与市场需求,构建系统性、多维度的推广体系,以下从市场定位、策略设计、执行落地及效果优化四个维度,详细拆解企业上云推广的实践路径,精准定位:明确目标企业与核心价值企业上云并非“一刀切”的方案,需先锁定目标客户群体,提炼差异化价值主张,客户分层……

    2025-11-20
    0
  • PS设计搜索框的实用技巧有哪些?

    在PS中设计一个美观且功能性的搜索框需要结合创意构思、视觉设计和用户体验考量,以下从设计思路、制作步骤、细节优化及交互预览等方面详细说明,帮助打造符合需求的搜索框,设计前的规划明确使用场景:根据网站或APP的整体风格确定搜索框的调性,例如极简风适合细线条和纯色,科技感适合渐变和发光效果,电商类则可能需要突出搜索……

    2025-11-20
    0

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注