Mrp*_*911 21 shell shell-script menu
如何在 shell 脚本中创建一个菜单,该菜单将显示 3 个选项,用户将使用箭头键移动突出显示光标并按 Enter 选择一个?
Ale*_*hek 36
这是函数bash形式的纯脚本解决方案select_option,完全依赖于ANSI 转义序列和内置的read.
适用于 OSX 上的 Bash 4.2.45。据我所知get_cursor_row(),在所有环境中可能无法在所有环境中同样有效地工作的时髦部分是, key_input()(检测向上/向下键)和cursor_to()函数。
#!/usr/bin/env bash
# Renders a text based list of options that can be selected by the
# user using up, down and enter keys and returns the chosen option.
#
# Arguments : list of options, maximum of 256
# "opt1" "opt2" ...
# Return value: selected index (0 for opt1, 1 for opt2 ...)
function select_option {
# little helpers for terminal print control and key input
ESC=$( printf "\033")
cursor_blink_on() { printf "$ESC[?25h"; }
cursor_blink_off() { printf "$ESC[?25l"; }
cursor_to() { printf "$ESC[$1;${2:-1}H"; }
print_option() { printf " $1 "; }
print_selected() { printf " $ESC[7m $1 $ESC[27m"; }
get_cursor_row() { IFS=';' read -sdR -p $'\E[6n' ROW COL; echo ${ROW#*[}; }
key_input() { read -s -n3 key 2>/dev/null >&2
if [[ $key = $ESC[A ]]; then echo up; fi
if [[ $key = $ESC[B ]]; then echo down; fi
if [[ $key = "" ]]; then echo enter; fi; }
# initially print empty new lines (scroll down if at bottom of screen)
for opt; do printf "\n"; done
# determine current screen position for overwriting the options
local lastrow=`get_cursor_row`
local startrow=$(($lastrow - $#))
# ensure cursor and input echoing back on upon a ctrl+c during read -s
trap "cursor_blink_on; stty echo; printf '\n'; exit" 2
cursor_blink_off
local selected=0
while true; do
# print options by overwriting the last lines
local idx=0
for opt; do
cursor_to $(($startrow + $idx))
if [ $idx -eq $selected ]; then
print_selected "$opt"
else
print_option "$opt"
fi
((idx++))
done
# user key control
case `key_input` in
enter) break;;
up) ((selected--));
if [ $selected -lt 0 ]; then selected=$(($# - 1)); fi;;
down) ((selected++));
if [ $selected -ge $# ]; then selected=0; fi;;
esac
done
# cursor position back to normal
cursor_to $lastrow
printf "\n"
cursor_blink_on
return $selected
}
Run Code Online (Sandbox Code Playgroud)
这是一个示例用法:
echo "Select one option using up/down keys and enter to confirm:"
echo
options=("one" "two" "three")
select_option "${options[@]}"
choice=$?
echo "Choosen index = $choice"
echo " value = ${options[$choice]}"
Run Code Online (Sandbox Code Playgroud)
输出如下所示,当前选择的选项使用反向 ansi 着色突出显示(这里很难在 Markdown 中表达)。print_selected()如果需要,这可以在功能中进行调整。
Select one option using up/down keys and enter to confirm:
[one]
two
three
Run Code Online (Sandbox Code Playgroud)
更新:这是一个select_opt包装上述select_option函数的小扩展,使其易于在case语句中使用:
function select_opt {
select_option "$@" 1>&2
local result=$?
echo $result
return $result
}
Run Code Online (Sandbox Code Playgroud)
使用 3 个文字选项的示例用法:
case `select_opt "Yes" "No" "Cancel"` in
0) echo "selected Yes";;
1) echo "selected No";;
2) echo "selected Cancel";;
esac
Run Code Online (Sandbox Code Playgroud)
如果有一些已知条目(在这种情况下是和否),您还可以混合使用,并利用$?通配符案例的退出代码:
options=("Yes" "No" "${array[@]}") # join arrays to add some variable array
case `select_opt "${options[@]}"` in
0) echo "selected Yes";;
1) echo "selected No";;
*) echo "selected ${options[$?]}";;
esac
Run Code Online (Sandbox Code Playgroud)
miu*_*miu 14
问题仅涉及一项选择。
\n如果你正在寻找一个多选菜单,这里是它的纯 bash实现:
使用
\n j/k或\xe2\x86\x91/\xe2\x86\x93箭头键向上或向下导航
\n \xe2\x8e\xb5(空格)切换选择,
\n \xe2\x8f\x8e(Enter)确认选择。
可以这样调用:
\nmy_options=( "Option 1" "Option 2" "Option 3" )\npreselection=( "true" "true" "false" )\n\nmultiselect result my_options preselection\nRun Code Online (Sandbox Code Playgroud)\n该multiselect函数的最后一个参数是可选的,可用于预先选择某些选项。
结果将作为数组存储在multiselect作为第一个参数传递的变量中。这是将选项与结果结合起来的示例:
idx=0\nfor option in "${my_options[@]}"; do\n echo -e "$option\\t=> ${result[idx]}"\n ((idx++))\ndone\nRun Code Online (Sandbox Code Playgroud)\n\nfunction multiselect {\n # little helpers for terminal print control and key input\n ESC=$( printf "\\033")\n cursor_blink_on() { printf "$ESC[?25h"; }\n cursor_blink_off() { printf "$ESC[?25l"; }\n cursor_to() { printf "$ESC[$1;${2:-1}H"; }\n print_inactive() { printf "$2 $1 "; }\n print_active() { printf "$2 $ESC[7m $1 $ESC[27m"; }\n get_cursor_row() { IFS=\';\' read -sdR -p $\'\\E[6n\' ROW COL; echo ${ROW#*[}; }\n\n local return_value=$1\n local -n options=$2\n local -n defaults=$3\n\n local selected=()\n for ((i=0; i<${#options[@]}; i++)); do\n if [[ ${defaults[i]} = "true" ]]; then\n selected+=("true")\n else\n selected+=("false")\n fi\n printf "\\n"\n done\n\n # determine current screen position for overwriting the options\n local lastrow=`get_cursor_row`\n local startrow=$(($lastrow - ${#options[@]}))\n\n # ensure cursor and input echoing back on upon a ctrl+c during read -s\n trap "cursor_blink_on; stty echo; printf \'\\n\'; exit" 2\n cursor_blink_off\n\n key_input() {\n local key\n IFS= read -rsn1 key 2>/dev/null >&2\n if [[ $key = "" ]]; then echo enter; fi;\n if [[ $key = $\'\\x20\' ]]; then echo space; fi;\n if [[ $key = "k" ]]; then echo up; fi;\n if [[ $key = "j" ]]; then echo down; fi;\n if [[ $key = $\'\\x1b\' ]]; then\n read -rsn2 key\n if [[ $key = [A || $key = k ]]; then echo up; fi;\n if [[ $key = [B || $key = j ]]; then echo down; fi;\n fi \n }\n\n toggle_option() {\n local option=$1\n if [[ ${selected[option]} == true ]]; then\n selected[option]=false\n else\n selected[option]=true\n fi\n }\n\n print_options() {\n # print options by overwriting the last lines\n local idx=0\n for option in "${options[@]}"; do\n local prefix="[ ]"\n if [[ ${selected[idx]} == true ]]; then\n prefix="[\\e[38;5;46m\xe2\x9c\x94\\e[0m]"\n fi\n\n cursor_to $(($startrow + $idx))\n if [ $idx -eq $1 ]; then\n print_active "$option" "$prefix"\n else\n print_inactive "$option" "$prefix"\n fi\n ((idx++))\n done\n }\n\n local active=0\n while true; do\n print_options $active\n\n # user key control\n case `key_input` in\n space) toggle_option $active;;\n enter) print_options -1; break;;\n up) ((active--));\n if [ $active -lt 0 ]; then active=$((${#options[@]} - 1)); fi;;\n down) ((active++));\n if [ $active -ge ${#options[@]} ]; then active=0; fi;;\n esac\n done\n\n # cursor position back to normal\n cursor_to $lastrow\n printf "\\n"\n cursor_blink_on\n\n eval $return_value=\'("${selected[@]}")\'\n}\nRun Code Online (Sandbox Code Playgroud)\n信用:这个 bash 函数是Denis Semenenko 实现的定制版本。
\nJoh*_*ith 12
对话是您要实现的目标的绝佳工具。这是一个简单的 3 选项菜单示例:
dialog --menu "Choose one:" 10 30 3 \
1 Red \
2 Green \
3 Blue
Run Code Online (Sandbox Code Playgroud)
语法如下:
dialog --menu <text> <height> <width> <menu-height> [<tag><item>]
Run Code Online (Sandbox Code Playgroud)
选择将发送到stderr。这是使用 3 种颜色的示例脚本。
#!/bin/bash
TMPFILE=$(mktemp)
dialog --menu "Choose one:" 10 30 3 \
1 Red \
2 Green \
3 Blue 2>$TMPFILE
RESULT=$(cat $TMPFILE)
case $RESULT in
1) echo "Red";;
2) echo "Green";;
3) echo "Blue";;
*) echo "Unknown color";;
esac
rm $TMPFILE
Run Code Online (Sandbox Code Playgroud)
在 Debian 上,您可以dialog通过同名的软件包进行安装。