Linux脚本选择菜单 [英] Linux script select menu

查看:76
本文介绍了Linux脚本选择菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用select循环和case指令创建带有六个选项的select菜单,但不是菜单选项的回声命令,它们必须显示如下:

I need to create a select menu with six options using the select loop and the case instruction BUT NOT THE ECHO COMMAND FOR THE MENU OPTION and they have to display like this:

1) opt1
2) opt2
3) opt3
4) opt4
5) opt5
6) opt6

不喜欢:

1) opt1 3) opt3 5) opt5
2) opt2 4) opt4 6) opt6

到目前为止,我已经有了这段代码,但是问题出在显示器上,有5个选项可以垂直显示,而有6个选项可以并排显示:

So far I have this code, but the problem is with the display, with 5 options it displays vertically, but with 6 it displays side by side:

#! /bin/sh
PS3="Enter your choice :"
select choice in "opt1" "opt2" "opt3" "opt4" "opt5" "Exit"; do
case $REPLY in
    1) echo "$choice";;
    2) echo "$choice";;
    3) echo "$choice";;
    4) echo "$choice";;
    5) echo "$choice";;
    6) echo "see you next time";break;;
    *) echo "Wrong choice!";;
esac
done

推荐答案

调整COLUMNS变量有助于限制菜单中的列数.我通常会这样做(在脚本中):

Adjusting the COLUMNS variable helps to limit the number of columns in the menu. I typically do (in a script):

COLUMNS=1
select ...

当我一直想要一列时.

when I want one column all the time.

准确地说,COLUMNS = 1表示您的TERMINAL宽1个字符.然后,select命令别无选择,只能打印一个列的菜单项.

To be precise, COLUMNS=1 means that your TERMINAL is ONE CHARACTER wide. The select command then has no choice but to then print ONE COLUMN of menu items.

要准确地说,您可以

  1. 找到最长物品的长度(itemLen)
  2. 将项目数和mod乘以10,以获取最大位数(numberLen)
  3. COLUMNS =((itemLen + numberLen = 2))

其中"2"表示菜单项编号和菜单项之间的括号和空格.但这不是必需的.

where the '2' is for the paren and space between the menu item number and the item. But this is not necessary.

这篇关于Linux脚本选择菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆