dialog menu to display files and select one of them

25,519

Solution 1

dialog has file-selection and directory-selection widgets (like Xdialog):

picture of dialog with --fselect

To use it, OP's script might be

#!/bin/bash
dialog --title "List file of directory" --fselect /home 100 100

though a 100x100 window seems rather large.

If you want to limit yourself to scripts that could be run with whiptail, the --radiolist option is an alternative to --menu.

Solution 2

You should be using menu not message box. Try this script:

#!/bin/bash
let i=0 # define counting variable
W=() # define working array
while read -r line; do # process file by file
    let i=$i+1
    W+=($i "$line")
done < <( ls -1 /home )
FILE=$(dialog --title "List file of directory /home" --menu "Chose one" 24 80 17 "${W[@]}" 3>&2 2>&1 1>&3) # show dialog and store output
clear
if [ $? -eq 0 ]; then # Exit with OK
    readlink -f $(ls -1 /home | sed -n "`echo "$FILE p" | sed 's/ //'`")
fi

Array is here necessary, otherwise it would not parse right as command, see http://mywiki.wooledge.org/BashFAQ/050.

Script is listing everything in /home folder, same as your example. If you really want only files, replace

ls -1 /home 

with

find /home -maxdepth 1 -type f

Also think about using 'whiptail', because it is default in most distributions. Dialog is not mostly installed.

Share:
25,519

Related videos on Youtube

maihabunash
Author by

maihabunash

I am 17 years old and love to develop

Updated on September 18, 2022

Comments

  • maihabunash
    maihabunash over 1 year

    I want to display all the files under /home directory in menu, and select only one of them. Then the script will print the full path of the selected file.

    I have created the following script. This script only displays the files in the dialog box menu.

    #!/bin/bash
    dialog --title "List file of directory /home" --msgbox "$(ls /home )" 100 100
    
  • maihabunash
    maihabunash over 9 years
    what is the "realpath" ?? , second how to delete the a(+) from the menu ? ( my script fail on line 11: realpath: command not found )
  • werediver
    werediver over 9 years
    realpath - return the canonicalised absolute pathname
  • Felipe
    Felipe over 9 years
    a(+)? you mean that number? You can use whiptail and then simply '--notags'. I think that it is not possible with dialog. As for realpath: stackoverflow.com/questions/284662/…
  • maihabunash
    maihabunash over 9 years
    I dont have realpath in my linux can you please use other syntax? ( +1 for you )
  • Felipe
    Felipe over 9 years
    Just replace realpath with readlink -f. Readlink is part of gnu coreutils so you must have it.