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

时间:2011-02-03 17:02:59

标签: bash

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

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

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

请帮忙

4 个答案:

答案 0 :(得分:9)

以下是使用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

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

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

答案 1 :(得分:7)

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

#!/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

答案 2 :(得分:1)

确定

继丹尼斯威廉姆森的线索和我自己的想法后,我来到了这样的事情

#!/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

现在,出现如下错误: Error: Expected 2 arguments, found only 1.

为什么会这样?

答案 3 :(得分:0)

This is a repost来自一个非常相似的问题的答案。我是在一个团队的帮助下得出这个答案的,但是在野外却找不到。我认为在此处添加它可能会对其他人有所帮助。

${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

这是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 $1 -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)
相关问题