Dynamic Bash Dialog from directory listing

Hey!

I want to get a directory listing and turn it into a bash dialog menu.

  • I need to append information from the files themselves (they'll be text files) onto the actual filenames as well.
  • I want to feed the appended filename list into the dialog menu as options.
  • I need to make a case selector that can handle the dynamic output from the dialog.

Here is the code I have so far (it does not work, at all).

#! /bin/bash

#Usage: bash Dynamic_Menu.bash /home/user/target_directory

#Section 1 - read in directory listing, store to $array[@]
i=0
while read line
do
    array[ $i ]=$i")  \"$line\""
    (( i++ ))
done < <(find $1) #consume directory path provided as argument

printf "\nInitial Array\n"
printf '%s\n' "${array[@]}"

read -rsp "Press any key to continue..." -n1 key

#Section 2 - read in file contents from array[@] files, temporarily store to $entries[@] and append to array[@]

filecount=0
for file in $array
do
	echo $filecount ": " $file
    count=0
    while IFS=";" read -r line || [[ -n "$line" ]]; do #remember to set the text delimiter
	    count=$((count + 1)); #Increment counter for menu options
	    entries[$count]=$count" \"$line\"" #Store line content into current OPTIONS array index.
    done < "$file" #this line reads the filename argument into the loop
	array[$filecount]="$array[$filecount] - $entries[1]"
	filecount= $((filecount + 1));
done

printf "\nAppended Array\n"
printf '%s\n' "${array[@]}"

read -rsp "Press any key to continue..." -n1 key

#Section 3 - Take array and feed it into a bash dialog menu as options for the user.
cmd=(dialog "Select options:" 22 76 16)
options=(${array[@]})
choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)

#Section 4 - Create a case that can handle the dynamic output from the dialog. Not yet started

Problems:

  1. I can't get the "find" command to provide absolute paths only to the files. It also provides the parent directory.
  2. I can't get the append action to work in section section (the append)
  3. I can't get the dialog to display at all.

My text files are formatted as follows:

Filename: test1.txt
Content:

The text files are contained within the directory that is fed into this script.

My desired bash dialog menu would appear as follows:

Each of these files is intended to hold settings for installation and I need to track (in the text file) whether that package has been installed. The settings for the actual file installation will be labeled differently in the file and will be parsed out by the Section 4 case that doesn't yet exist.

I am developing this in Ubuntu 18.04 but intend to deploy it on TinyCore Linux with the dialog libraries installed.

I realize this is a lot to chew off at once. I haven't posted this on Stack yet because it isn't fleshed out enough and I find myself struggling.

How far would

IFS=$'\n'
for FN in *; do [ -f $FN ] && { read LN < $FN; TMP=$TMP$'\n'"$FN $LN"; }; done
PS3="Choose a package for installation:"
select CHC in $TMP; do echo $CHC; done

get you?

You don't have to append each item to an array individually. And bash has a built in menu system.

# Split only on newlines
OLDIFS="$IFS"
IFS=$'\n'

# Store listing in array
array=( $(find "$1" -type f) )

IFS="$OLDIFS" # Return to normal splitting behavior

# Make a dialog of choices
select X in "${array[@]}"
do
        # In this loop, X will be set to the file selected
        break
done

Corona688 and RudiC - thank you both so much for your time; cannot tell you how much I appreciate it. After wrestling with this for a while I am grateful that someone would be willing to lend me some assistance. So thank you thank you!!

RudiC: I am *really* green to this, and unix terminal is not at all my forte. A couple of questions:

1) Where is the directory listing occurring in this script? I'm guessing it's at `[ -f $FN ]`
2) Can I pass an argument into this? Traditionally I've used $1, $2 etc. but I'm not intimately familiar with bash.

Corona688: Thank you for the find syntax! I had completely forgotten the `type -f` argument for `find` :stuck_out_tongue: Man pages for the win (Windows has broken my brain I think lol).

The simplicity of the "select" command is fantastic, and it may be the route I end up taking. I was hoping to use the Bash Dialog interface for consistency as I won't be the end user of this software. Currently I have a master menu (built statically) with common tasks. I am attempting to build this installer as a "sub" menu, or basically a bash script that gets invoked through one of the options in the main menu. A sudden change in the look & feel could confuse some users; they aren't the type that know anything about bash, linux or really much about software. Surge support we'll say.

I have the Tiny Core ncurses extensions remastered into my core file and would like to be able to use them.

But again, I appreciate all the help thus far! If an ncurses dialog is a no go then I'll give up on it. I just figured that it had to be possible...

Also, Python has been suggested but I need to keep the footprint of my OS as small as possible. My initrd is currently sitting around 13mb, compressed.

for FN in *

The shell will expand the * to all files in the directory. You can add a prefix of your choice, e.g test* . Then - if you're sure all selected are regular files - you may drop the -f test which safeguards you against offering e.g. directories in your select .

Try using e.g.

for FN in $1*

.

Ok! A coworker helped me noodle this through; turns out the input required for dialog is rather odd...

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

	array[ $i ]=$j
	(( j++ ))
	array[ ($i + 1) ]=$line
        (( i=($i+2) ))
	
done < <(find $1 -type f) #consume file path provided as argument

##uncomment for debug
#printf '%s\n' "${array[@]}"
#read -rsp "Press any key to continue..." -n1 key

#Build the menu with dynamic content
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:"

CHOICE=$(dialog --clear \
                --backtitle "$BACKTITLE" \
                --title "$TITLE" \
                --menu "$MENU" \
                $HEIGHT $WIDTH $CHOICE_HEIGHT \
                "${array[@]}" \
                2>&1 >$TERMINAL)

I haven't fully debugged this yet, but from a directory with less than 10 files it's working!