bash bash中的动态对话框--菜单框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4889187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 20:09:42  来源:igfitidea点击:

Dynamic dialog --menu box in bash

bash

提问by kasper

I'm searching for good explanation about making dynamic dialog --menu box in bash. I'm trying to load a list of users from a file that have structure like this:

我正在寻找关于在 bash 中制作动态对话框 --menu 框的好解释。我正在尝试从具有如下结构的文件中加载用户列表:

------ user ------  
/rw412 0.2 /rx511 23.1 /sgo23 9.2  
/fs352 1.4 /...  
------ another_user ------
/rw412 0.3 / and so on...

of course the user name is between ------
i don't really know how to use loops inside dialog. I'm also trying to avoid creating additional files.

当然用户名介于------
我真的不知道如何在对话框中使用循环。我也试图避免创建额外的文件。

Please help

请帮忙

采纳答案by kasper

Ok

好的

Following Dennis Williamson clues and my own ideas i came to something like this

根据丹尼斯威廉姆森的线索和我自己的想法,我得出了这样的结论

#!/bin/bash
c=0
while read -r dashes1 username dashes2
do
  if [[ $dashes1 == --*-- && $dashes2 == --*-- ]]
  then
    options=("${options[@]}" "$((++c))" "$username")
  fi
done < inputfile
cmd=(dialog --backtitle "title" --menu "Select_user:" 22 38 2) #2 becouse 
i know there will be 2 options
command=`echo "${cmd[@]}" "${options[@]}" "2>file"`
$command

Now, there is an error like this: Error: Expected 2 arguments, found only 1.

现在,有这样的错误: Error: Expected 2 arguments, found only 1.

Why is that??

这是为什么??

回答by Paused until further notice.

Here's an example of one way to use dialog. The optionsarray can be built up in a variety of ways (see below).

这是使用dialog. 该options阵列可以通过多种方式构建(见下文)。

#!/bin/bash
cmd=(dialog --keep-tite --menu "Select options:" 22 76 16)

options=(1 "Option 1"
         2 "Option 2"
         3 "Option 3"
         4 "Option 4")

choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)

for choice in $choices
do
    case $choice in
        1)
            echo "First Option"
            ;;
        2)
            echo "Second Option"
            ;;
        3)
            echo "Third Option"
            ;;
        4)
            echo "Fourth Option"
            ;;
    esac
done

Here's one way to build the options array:

这是构建选项数组的一种方法:

count=0
while read -r dashes1 username dashes2
do
    if [[ $dashes1 == --*-- && $dashes2 == --*-- ]]
    then
        options+=($((++count)) "$username")
    fi
done < inputfile

回答by a1danel

following the above clues and having my own ideas as well; here is another way:

遵循上述线索,也有自己的想法;这是另一种方式:

#!/bin/bash

MENU_OPTIONS=
COUNT=0

for i in `ls`
do
       COUNT=$[COUNT+1]
       MENU_OPTIONS="${MENU_OPTIONS} ${COUNT} $i off "
done
cmd=(dialog --separate-output --checklist "Select options:" 22 76 16)
options=(${MENU_OPTIONS})
choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)
for choice in $choices
do
       " WHATEVER from HERE"
done

回答by Shrout1

This is a repostof my answer from a very similar question. I arrived at this answer with the help of a cohort but couldn't find it in the wild; I think adding it here might help others.

这是我从一个非常相似的问题的回答的转贴。我在一群人的帮助下得出了这个答案,但在野外找不到;我认为在这里添加它可能会帮助其他人。

The ${options[@]}array required the following (rather strange) structure in order to work. This was tested on bash in Ubuntu 18.04:

${options[@]}数组需要以下(相当奇怪的)结构才能工作。这是在 Ubuntu 18.04 中的 bash 上测试的:

 #Dynamic dialogs require an array that has a staggered structure
 #array[1]=1
 #array[2]=First_Menu_Option
 #array[3]=2
 #array[4]=Second_Menu_Option

Here is bash code that reads in a directory listing from an argument and creates a dynamic menu from it:

这是 bash 代码,它从参数中读取目录列表并从中创建动态菜单:

 #! /bin/bash
 #usage: Dynamic_Menu.bash /home/user/target_directory

 declare -a array

 i=1 #Index counter for adding to array
 j=1 #Option menu value generator

 while read line
 do     
    array[ $i ]=$j
    (( j++ ))
    array[ ($i + 1) ]=$line
    (( i=($i+2) ))

 done < <(find  -type f) #consume file path provided as argument

 #Define parameters for menu
 TERMINAL=$(tty) #Gather current terminal session for appropriate redirection
 HEIGHT=20
 WIDTH=76
 CHOICE_HEIGHT=16
 BACKTITLE="Back_Title"
 TITLE="Dynamic Dialog"
 MENU="Choose a file:"

 #Build the menu with variables & dynamic content
 CHOICE=$(dialog --clear \
                 --backtitle "$BACKTITLE" \
                 --title "$TITLE" \
                 --menu "$MENU" \
                 $HEIGHT $WIDTH $CHOICE_HEIGHT \
                 "${array[@]}" \
                 2>&1 >$TERMINAL)