read 判定用户输入的状态后运行相应的结果

文件名: test26.sh 

#!/bin/bash
# getting just one character of input

read -n1 -p "Do you want to continue [Y/N]? " answer
case $answer in
Y | y) echo 
    echo "fine,continue on...";;
N | n) echo 
    echo OK, goodbye
    exit;;
esac
echo "This is the end of script"

运行 sh test26.sh 

出现 Do you want to continue [Y/N]?  

输入 y 后

fine,continue on...
This is the end of script

输入 n 后

OK, goodbye

 该例子使用了-n选项,后接数字1,指示read命令只要接收到一个字符就退出,只要按下一个字符进行回答,read命令立即接受输入并将其传给变量,无需按回车键

原文地址:https://www.cnblogs.com/jacson/p/4790990.html