read命令 -n(不换行) -p(提示语句) -n(字符个数) -t(等待时间) -s(不回显)
1、基本读取
- #!/bin/bash
- echo -n "Enter your name:" //参数-n的作用是不换行,echo默认是换行
- read name //从键盘输入
- echo "hello $name,welcome to my program" //显示信息
- exit 0 //退出shell程序。
- #!/bin/bash
- read -p "Enter your name:" name
- echo "hello $name, welcome to my program"
- exit 0
- #!/bin/bash
- if read -t 5 -p "please enter your name:" name
- then
- echo "hello $name ,welcome to my script"
- else
- echo "sorry,too slow"
- fi
- exit 0
- #!/bin/bash
- read -n1 -p "Do you want to continue [Y/N]?" answer
- case $answer in
- Y | y)
- echo "fine ,continue";;
- N | n)
- echo "ok,good bye";;
- *)
- echo "error choice";;
- esac
- exit 0
- #!/bin/bash
- read -s -p "Enter your password:" pass
- echo "your password is $pass"
- exit 0
- #!/bin/bash
- count=1 //赋值语句,不加空格
- cat test | while read line //cat 命令的输出作为read命令的输入,read读到的值放在line中
- do
- echo "Line $count:$line"
- count=$[ $count + 1 ] //注意中括号中的空格。
- done
- echo "finish"
- exit 0
- #!/bin/bash
- read -p "Enter your name: " A
- if [ "$A" = "GS" ];then
- echo "yes"
- elif [ "$A" = "UC" ];then
- echo "no"
- else
- echo "your are wrong"
- fi