read命令 -n(不換行) -p(提示語句) -n(字符個數) -t(等待時間) -s(不回顯)shell
一、基本讀取bash
- #!/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