Ubuntu – Using for loop to create a flexible radio list in a dialog box from a shell script

bashscripts

I want to create a little Program using Dialog to play logfiles created with Script. The problem is that I can't get a dynamic radio list that automatically adds a row if a new logfile is created. So I know that I "just" need to use a if loop like # for i in ...etc. with a Counter that Counts the logfiles but I can't get it to work.

So this is what I have right now:

#!/bin/bash
COUNTER=1
for i in $( ls /mnt/home/$USER/shell_logs/*.log); do
echo $i $COUNTER
let COUNTER=COUNTER+1
done

I just need to get this working with my radiolist:

#! /bin/bash
COUNTER=1
for i in $( ls /mnt/home/$USER/shell_logs/*.log); do
let COUNTER=COUNTER+1
done 
dialog --backtitle "Radiolist" \
--radiolist "test" 0 0 $COUNTER \
$COUNTER $i  <-- This is the main problem

Best Answer

To make it working, you have to add the list entries to a variable inside your for loop. Something like:

#!/bin/bash

COUNTER=1
RADIOLIST=""  # variable where we will keep the list entries for radiolist dialog
for i in /mnt/home/$USER/shell_logs/*.log; do
    RADIOLIST="$RADIOLIST $COUNTER $i off "
    let COUNTER=COUNTER+1
done

dialog --backtitle "Radiolist" \
--radiolist "test" 0 0 $COUNTER \
$RADIOLIST