BASH对话框动态菜单

BASH dialog Dynamic menu

我需要使用数组中的变量创建带对话框的动态菜单。

这是我的代码:

#!/bin/bash
file="teste.cfg"
count=0;
while IFS=";" read nomeTarefa dirOrigem dirDest tipoBkp agendarBkp compactarBkp gerarLog || [[ -n "$gerarLog" ]]; do #RECEBE NAS VARS OS VALORES DELIMITADOS POR ;
    count=$((count + 1));#INICIA O COUNT PARA INCREMENTAR O OPTIONS
    options[$count]="$options$count) \"$nomeTarefa\"" #CONCATENA O OPTIONS

done < $file

options=$"$options"

for ((i=1; i<=count; i++))
do
    echo ${options[$i]}
done

options=(${options[$count]}) 

cmd=(dialog --keep-tite --menu "Select options:" 22 76 16)

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

我从一个文件中接收变量,然后我需要动态构建 "options" 来创建一个案例。

所以这个菜单对话框将有 x 个条目,当我 运行 其中任何一个我做某事时。

关于如何构建这个 "options" 有什么想法吗?

提前致谢

我刚解决了大家。

#!/bin/bash
file="teste.cfg"
count=0;
while IFS=";" read nomeTarefa dirOrigem dirDest tipoBkp agendarBkp compactarBkp gerarLog || [[ -n "$gerarLog" ]]; do #RECEBE NAS VARS OS VALORES DELIMITADOS POR ;
        count=$((count + 1));#INICIA O COUNT PARA INCREMENTAR O OPTIONS 
    options[$count]=$count") \"$nomeTarefa\"" #CONCATENA O OPTIONS  
done < $file ##END READ FILE

options=(${options[@]})

cmd=(dialog --keep-tite --menu "Select options:" 22 76 16)

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

谢谢大家!

奇怪的是,此处列出的解决方案对我不起作用。 ${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  -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)