shell select语句用法
select表达式是bash的一种扩展应用,擅长于交互式场合。用户可以从一组不同的值中进行选择:
select var in ... ; do
break;
done
.... now $var can be used ....
下面是一个简单的示例:
#!/bin/sh
echo "What is your favourite OS?"
select var in "Linux" "Gnu Hurd" "Free BSD" "Other"; do
break;
done
echo "You have selected $var"
如果以上脚本运行出现 select :NOT FOUND 将 #!/bin/sh 改为 #!/bin/bash 该脚本的运行结果如下:
What is your favourite OS?
1) Linux
2) Gnu Hurd
3) Free BSD
4) Other
#? 1
You have selected Linux
select var in ... ; do
break;
done
.... now $var can be used ....
下面是一个简单的示例:
#!/bin/sh
echo "What is your favourite OS?"
select var in "Linux" "Gnu Hurd" "Free BSD" "Other"; do
break;
done
echo "You have selected $var"
如果以上脚本运行出现 select :NOT FOUND 将 #!/bin/sh 改为 #!/bin/bash 该脚本的运行结果如下:
What is your favourite OS?
1) Linux
2) Gnu Hurd
3) Free BSD
4) Other
#? 1
You have selected Linux