2011-02-03 33 views
1

我正在尋找關於在bash中製作動態對話框 - 菜單框的好解釋。我試圖從具有結構像這樣的文件中加載用戶的列表:動態對話框 - bash中的菜單框

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

當然,用戶名是------之間
我真的不知道如何使用內部對話循環。我也試圖避免創建額外的文件。

請幫

回答

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.

這是爲什麼?

8

這裏是一種方式的示例使用dialogoptions陣列可以通過多種方式建立(見下文)。

#!/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+=($((++c)) "$username") 
    fi 
done < inputfile 
+0

這似乎是有可能的工作,但它給了我一個錯誤:`溫度:行7:附近意外的標記`$語法錯誤((+ C))」 溫度:7號線: `\t \t options + =($((++ c))「$ user」)'` – kasper 2011-02-03 18:41:01

+0

@kasper:腳本中的shebang行是什麼? – 2011-02-03 18:45:17

+0

@丹尼斯威廉姆森#!bin/bash – kasper 2011-02-03 18:54:36

5

遵循上述線索並有我自己的想法;這裏是另一種方式:

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