select in 循环用来增强交互性,它可以显示出带编号的菜单,用户输入不同的编号就可以选择不同的菜单,并执行不同的功能。

select in 是 Shell 独有的一种循环,非常适合终端(Terminal)这样的交互场景,C语言、C++JavaPythonC# 等其它编程语言中是没有的。

 

【shell】How can I create a select menu in a shell script?

【shell】How can I create a select menu in a shell script?

#?用来提示用户输入菜单编号;^D表示按下 Ctrl+D 组合键,它的作用是结束 select in 循环。

运行到 select 语句后,取值列表 value_list 中的内容会以菜单的形式显示出来,用户输入菜单编号,就表示选中了某个值,这个值就会赋给变量 variable,然后再执行循环体中的 statements(do 和 done 之间的部分)。

每次循环时 select 都会要求用户输入菜单编号,并使用环境变量 PS3 的值作为提示符,PS3 的默认值为#?,修改 PS3 的值就可以修改提示符。

如果用户输入的菜单编号不在范围之内,那么就会给 variable 赋一个空值;如果用户输入一个空值(什么也不输入,直接回车),会重新显示一遍菜单。

注意,select 是无限循环(死循环),输入空值,或者输入的值无效,都不会结束循环,只有遇到 break 语句,或者按下 Ctrl+D 组合键才能结束循环。

 

# vim select.sh  

#########################################

#!/bin/bash

#功能描述(Description):根据用户选择的菜单实现对应的功能.

echo "请根据提示选择一个选项."

PS3="Please input your choice:"

select item in "CPU" "IP" "MEM" "exit"

do

    case $item in

    "CPU")

        uptime;;

    "IP")

        ip a s;;

    "MEM")

        free;;

    "exit")

        exit;;

    *)

        echo error;;

    esac

  break

done

echo  "Your choice is $item."

##########################################

 

【shell】How can I create a select menu in a shell script?

 

【shell】How can I create a select menu in a shell script?

 

 

PS 是 prompt statement (提示表达式)的缩写。

PS1 – Default interaction prompt   

PS2 – Continuation interactive prompt

PS3 – Prompt used by “select” inside shell script

PS4 – Used by “set -x” to prefix tracing output

 PROMPT_COMMAND – Bash shell executes the content of the PROMPT_COMMAND just before displaying the PS1 variable.

 

bash中的PS1、 PS2、PS3、PS4和PROMPT_COMMAND详解

https://via.hypothes.is/https://blog.51cto.com/kusorz/1968827

 

Bash Shell: Take Control of PS1, PS2, PS3, PS4 and PROMPT_COMMAND

https://via.hypothes.is/https://www.thegeekstuff.com/2008/09/bash-shell-take-control-of-ps1-ps2-ps3-ps4-and-prompt_command

 

 

Unix / Linux Shell - The select Loop

https://via.hypothes.is/https://www.tutorialspoint.com/unix/select-loop.htm

 

shell编程中select语句的使用

https://blog.csdn.net/m0_37556444/article/details/82915966

 

Shell select in循环详解

https://via.hypothes.is/http://c.biancheng.net/view/2829.html

 

select loop

https://via.hypothes.is/https://bash.cyberciti.biz/guide/Select_loop

 

Linux shell中case与select语句

https://blog.51cto.com/11555417/2165029?source=dra

 

md5sum 和 sha256sum用于 验证软件完整性

https://www.cnblogs.com/xuyaowen/p/md5sum_sha256_usages.html

相关文章: