shell中怎么编辑需要用户选择或者输入的需求
方法一: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版权属于:Joyber
本文链接:https://blog.qqvbc.com/default/1132.html
转载时须注明出处及本声明