shell script中read的用法

1、read基本读取

1
2
3
4
5
6
#!/bin/bash 
#testing the read command 
   
echo -n "Enter you name:"   #echo -n 让用户直接在后面输入  
read name  #输入的多个文本将保存在一个变量中 
echo "Hello $name, welcome to my progra

执行:

1
2
3
# ./read.sh 
Enter you name: yuan 
Hello yuan, welcome to my program   

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

1
2
3
4
5
#!/bin/bash 
#testing the read -p option 
read -p "Please enter your age: " age 
days=$[ $age * 365 ] 
echo "That makes you over $days days old!"

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

1
2
3
4
5
#!/bin/bash 
# entering multiple variables 
   
read -p "Enter your name:" first last 
echo "Checking data for $last, $first"

执行: 

 
1
2
3
# ./read1.sh 
Enter your name: a b 
Checking data for b, a

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

1
2
3
4
5
6
7
8
9
10
#!/bin/bash 
# timing the data entry 
   
if read -t 5 -p "Please enter your name: " name     #记得加-p参数, 直接在read命令行指定提示符 
then 
    echo "Hello $name, welcome to my script" 
else 
    echo  
    echo "Sorry, too slow!" 
fi

执行:

1
2
3
4
5
6
7
8
# ./read3.sh 
Please enter your name:  
Sorry, too slow! 
   
   
# ./read3.sh  
Please enter your name: wang 
Hello wang, welcome to my script

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

  • []有比较的判断的功能
  • -o代表or
  • &&在shell中的用法是如果&&左边的命令执行成功(即$?=0)时才能执行&&右边的命令
1
2
3
4
5
6
7
#!/bin/bash 
   
   
read -p "Please input(Y/N):" yn 
[ "$yn" == "Y" -o "$yn" == "y" ]&&echo  "OK,continue"&&exit
[ "$yn" == "N" -o "$yn" == "n" ]&&echo "Oh,interrupt!"&&exit
echo "i don't konw what your choice is"&&exit

 执行:

1
2
3
4
5
6
yuanqiangfei@ubuntu:~/script$ ./sh01.sh  
Please input(Y/N):y 
OK,continue 
yuanqiangfei@ubuntu:~/script$ ./sh01.sh  
Please input(Y/N):n 
Oh,interrupt!

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

  • 在中括号 [] 内的每个组件都需要有空白键来分隔;
  • 在中括号内的变量,最好都以双引号括号起来;
  • 在中括号内的常数,最好都以单或双引号括号起来。
  • ==和!=两边都要有空格
  • []有比较的判断的功能 
1
2
3
4
5
6
7
8
9
10
#!/bin/bash 
#entering muiltple variables 
   
while true 
do 
        read -s -p "Please enter your password:"  passwd 
   
        [ "$passwd" == "123456" ]&&echo "password is right!"&&exit
        [ "$passwd" != "123456" ]&&echo "password is not right,Please input again!"&&continue 
done

执行:

1
2
3
4
5
yuanqiangfei@ubuntu:~/script$ ./read.sh  
Please enter your password:password is right! 
yuanqiangfei@ubuntu:~/script$ ./read.sh  
Please enter your password:password is not right,Please input again! 
Please enter your password:

7、从文本中读取

 
1
2
3
4
5
6
7
8
9
10
#!/bin/bash 
# reading data from a file 
   
count=1 
cat test | while read line 
do 
   echo "Line $count: $line" 
   count=$[ $count + 1 ] 
done 
echo "Finished processing the file"

执行结果:

1
2
3
4
5
./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 

  

posted @   轻轻的吻  阅读(683)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示