方法一:read指令
该命令是在shell提示符下获取用户输入的最简单,使用最广泛的方法read。演示其用法的最佳方法是一个简单的演示:

#! /bin/bash

while true; do
    read -p "Do you wish to install this program? [Y/N]" yn
    echo You input is: $yn;
    case $yn in
        [Yy]* ) echo make install; break;;
        [Nn]* ) exit;;
        * ) echo "Please answer yes or no.";;
    esac
done

方法二:select指令
另一种方法是Bash的命令。这是使用的相同示例:select

#! /bin/bash

echo "Do you wish to install this program? Please select a number:"
select yn in "Yes" "No"; do
    echo You input is: $yn;
    case $yn in
        Yes ) echo make install; break;;
        No ) exit;;
    * ) echo "Please answer yes or no.";;
    esac
done

执行命令结果:

$ ./test_select.sh 
Do you wish to install this program? Please select a number:
1) Yes
2) No
#? 2
You input is: No
$ ./test_select.sh 
Do you wish to install this program? Please select a number:
1) Yes
2) No
#? 1
You input is: Yes
make install

标签: none

添加新评论