Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic dialog --menu box in bash

Tags:

bash

I'm searching for good explanation about making dynamic dialog --menu box in bash. I'm trying to load a list of users from a file that have structure like this:

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

of course the user name is between ------
i don't really know how to use loops inside dialog. I'm also trying to avoid creating additional files.

Please help

like image 424
kasper Avatar asked Feb 03 '11 17:02

kasper


3 Answers

Here's an example of one way to use dialog. The options array can be built up in a variety of ways (see below).

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

Here's one way to build the options array:

count=0
while read -r dashes1 username dashes2
do
    if [[ $dashes1 == --*-- && $dashes2 == --*-- ]]
    then
        options+=($((++count)) "$username")
    fi
done < inputfile
like image 64
Dennis Williamson Avatar answered Sep 29 '22 00:09

Dennis Williamson


following the above clues and having my own ideas as well; here is another way:

#!/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
like image 21
a1danel Avatar answered Sep 29 '22 00:09

a1danel


Ok

Following Dennis Williamson clues and my own ideas i came to something like this

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

Now, there is an error like this: Error: Expected 2 arguments, found only 1.

Why is that??

like image 23
kasper Avatar answered Sep 29 '22 01:09

kasper