shell中使用内置命令select生成程序菜单的例子

发布时间:2020-05-05编辑:脚本学堂
shell中使用内置命令select生成程序菜单的例子,以下的脚本可以提供给用户一系列的选项去选择。其中定义的finished选项挺好。

shell中使用内置命令select生成程序菜单的例子,以下的脚本可以提供给用户一系列的选项去选择。
其中定义的finished选项挺好。
 

复制代码 代码如下:
#!/bin/bash
directorylist="Finished $(ls /)"
PS3='Directory to process? ' # Set a useful select prompt
until [ "$directory" == "Finished" ]; do
    printf "%b" "annSelect a directory to process:n" >&2
    select directory in $directorylist; do
        # User types a number which is stored in $REPLY, but select
        # returns the value of the entry
        if [ "$directory" = "Finished" ]; then
            echo "Finished processing directories."
            break
        elif [ -d "/$directory" ]; then
            echo "You chose number $REPLY, processing $directory ..."
            # Do something here
            break
        else
            echo "Invalid selection!"
        fi # end of handle user's selection
    done # end of select a directory
done # end of while not