Reputation: 87
I need to create a dynamic menu with dialog using vars from an array.
Heres my code:
#!/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)
I recieve vars from a file, and then i need to build the "options" dynamicly to create a case.
So this menu dialog, will have x numbers of entries and when i run any of them i do something.
Any ideas how can i build this "options"?
Thanks in advance
Upvotes: 3
Views: 4532
Reputation: 2607
Oddly, the solution listed here did not work for me. The ${options[@]}
array required the following (rather strange) structure in order to work. This was tested on bash in Ubuntu 18.04:
#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:
#! /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)
Upvotes: 2
Reputation: 87
I just solved guys.
#!/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)
Thank you all!
Upvotes: 3