shell编程中的select用法

时间:2023-01-30 22:33:36

select 语句

select表达式是bash的一种扩展应用,擅长于交互式场合。用户可以从一组不同的值中进行选择:

select var in ... ; do
 break;
done
.... now $var can be used ....

下面是一个简单的示例:

#!/bin/bash

echo "What is your favourite OS?"
select var in "Linux" "Gnu Hurd" "Free BSD" "Other"; do
break;
done
echo "You have selected $var"

该脚本的运行结果如下:

What is your favourite OS?
1) Linux
2) Gnu Hurd
3) Free BSD
4) Other
#? 1
You have selected Linux

-------------------------------------------------------------------------------------------------

运行时出现:

select.sh: 3: select: not found

select.sh: 5: Syntax error: "done" unexpected

把#!/bin/sh改为#!/bin/bash

执行脚本时将sh select.sh改为 bash select.sh

 shell编程中的select用法