59854

BASH dialog Dynamic menu

Question:

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

Answer1:

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!

Answer2:

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)

Recommend

  • acts_as_taggable_on Tags added twice
  • View Paypal shopping cart contents on my site
  • Refactoring advice: maps to POJOs
  • Want to save selected (i.e., more than 1) enums as string with NHibernate
  • Timeout a query
  • Select value from xtype selection type checkbox CQ5
  • Can my PDF ping my server when it is opened?
  • Synchronize windows folders
  • Django model for a Postgres view
  • How can I get the full list of running processes on a Mac from a python app
  • VSCode change debug shell to bash on windows
  • Error processing multiple files
  • Defined variables not working in javascript files when I use getScript
  • Trouble saving JSON data for use by multiple UIViews [duplicate]
  • Connect .sks to skscene.h
  • Stop Bash Script if Hive Fails
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • Installing iPhone App to iPhone
  • MongoDB in PHP using aggregate to group by _id is null not working
  • CSS Linear-gradient formatting issue accross different browsers
  • Why is an OPTIONS request sent to the server?
  • Spring security and special characters
  • Deleting and Updating values from a cusrsor adapter
  • Modifying destination and filename of gulp-svg-sprite
  • 'TypeError' while using NSGA2 to solve Multi-objective prob. from pyopt-sparse in OpenMDAO
  • Display Images one by one with next and previous functionality
  • Upload files with Ajax and Jquery
  • ORA-29908: missing primary invocation for ancillary operator
  • How to get next/previous record number?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Circular dependency while pushing http interceptor
  • Linker errors when using intrinsic function via function pointer
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • How do you join a server to an Active Directory (domain)?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How does Linux kernel interrupt the application?
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?