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

share|improve this question

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
share|improve this answer
    
After six years this is still one of the best examples for dynamic checklist... ok the only one I found in 3 days :) – WinEunuuchs2Unix Mar 14 '17 at 23:11
up vote 1 down vote accepted

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??

share|improve this answer
    
Ok... i made it :D. I just took off those "" from "2>file". Awesome! Can you just tell me please how to make this command to accept two words in backtitle or menu? like --backtitle "title title" – kasper Feb 3 '11 at 20:31
    
--backtitle "title title" should work fine. Don't do the last two lines like you have them above - do them like I have them in my answer. That's what allows you to capture the selection that the user makes so you can act on it. What version of Bash are you using? That may be why the += didn't work. – Dennis Williamson Feb 3 '11 at 23:25
    
bash --version shows that it's 2.05b :) and space inside "" does not work, sadly. The way i did that also gives me what i need. Selection is written to file and then to variable. with that variable i can do case or if. Or are there any dangers with my point of view? – kasper Feb 4 '11 at 1:30

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+=($((++c)) "$username")
    fi
done < inputfile
share|improve this answer
    
This seems likely to work but it gives me an error: temp: line 7: syntax error near unexpected token $((++c))' temp: line 7: ` options+=($((++c)) "$user")'` – kasper Feb 3 '11 at 18:41
    
@kasper: What is the shebang line in your script? – Dennis Williamson Feb 3 '11 at 18:45
    
@Dennis Williamson #!bin/bash – kasper Feb 3 '11 at 18:54
    
Is there any other way to write that line? – kasper Feb 3 '11 at 19:17
1  
works perfectly, I'd have searched for ages to find this elegant solution. Thx @Dennis +1 vote :-) – hornetbzz Mar 17 '11 at 15:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.