read可以从控制台或者文件读取输入内容
?
1、从控制台读取输入
?
#!/bin/bash # -n 选项移除字符串末尾的换行符 echo -n "Enter your name:" read name echo "Hello $name,welcome to my program"
?
? ??
#!/bin/bash # -p 允许直接在read命令行指定提示符 read -p "please enter your name:" name echo "Hello $name,welcome to my program"
?
? ??
#!/bin/bash # -p 允许直接在read命令行指定提示符 # -t 指定超时的时间 read -t 10 -p "please enter your name:" name echo "Hello $name,welcome to my program"
?
? ?
#!/bin/bash # -p 允许直接在read命令行指定提示符 # -s 隐藏方式读取,比如输入密码等,实际是将输入文本颜色变成和背景色一致 # -t 指定超时的时间 read -t 10 -s -p "please enter your name:" name echo "Hello $name,welcome to my program"
?
2、从文件中读取
??
? ??
#!/bin/bash count=1 cat testfile | while read line do echo "line #$count: $line" count=$[ $count + 1 ] done echo "finished processing the file"
?
?
?