Linux dialog select file

dialog menu to disply files and select one of them

Please advise me on how I can do the following with the dialog utility: 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 

3 Answers 3

Code & copy/paste not really available on the stack exchange android app.

CHOICE=’dialog —title «List » —radiolist «$(ls /home)» 100 100′

Note: i have used ‘ for back-tick my keyboard has no back-tick.

Yeah your right, i can only think otf cheesey solutions which’ll give functionality. What is the point of dialog at best you can detect button pressed.

I know this is 8 year old post, but it’s top of my google search. I tried hard to get this working but Dialog gives me errors when using ls. So I asked chatgpt and it came back with some alternatives that worked. So I modified it for my own use and it works. So for anyone who wants to see another way to do it:

files=() #blank the variable so its empty for next use # Loop folder, add files to array while IFS= read -r -d $'\0' file; do files+=("$file" "") done < <(find "/home" -maxdepth 1 -type f -name "*.sh" -print0) # or for all files: done < <(find "/home" -maxdepth 1 -type f -print0) # Check it has at least 1 file to show (otherwise dialog errors) if [ $<#files[@]>-eq 0 ]; then clear echo "No .sh files found in $directory" else file=$(dialog --stdout --title "Select a file" --cancel-label "Back" --menu "Choose a file:" 0 0 0 "$") clear echo "Selected file: $file" fi 

Источник

dialog menu to display files and select one of them

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 

2 Answers 2

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 100×100 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 .

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 "$" 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

find /home -maxdepth 1 -type f 

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

Источник

How do I prompt users with a GUI dialog box to choose file/directory path, via the command-line?

screem

The idea here is to prompt the user to enter the «Source» and «Destination» directories for rsync to work with. As is, the user will have to manually enter /path/to/directory/ via the command-line. Instead, I want to prompt the user to enter the paths through a GUI interface. Something like this: What commands can I use to prompt the user with a GUI selection window that returns the file path to the command-line?

Please remember to add warning about usage of GUI windows. Unnecessary windows popping up can induce rage on advanced users.

Why, oh why would you ever want to implement such an annoying «feature»? Remember that if we enter the directories at the command line we can use tab completion, and don’t need to wait for some gui to load. Why anyone would want to add a GUI to a perfectly good shell script is beyond me.

@terdon Because if we run script directly & not Run in terminal then I want to provide GUI window.

4 Answers 4

You can use this for files:

zenity --file-selection --directory 
zenity --help-general zenity --help-file-selection 

Generally it matches the current theme (for GTK window managers anyway), on my machine with a modded version of Zukitwo 3.8 it looks like this:

One way of using it is like this:

echo "you selected $(zenity --file-selection)" 

Which would result in you selected /path/to/file .

You can also use options to set an appropriate title, and the directory it starts in — With your rsync use case, for example:

zenity --file-selection --directory --title="Choose rsync source directory" --filename=$HOME/Desktop/ 

For files, you can also specify a filetype to select — e.g:

zenity --file-selection --file-filter='PDF files (pdf) | *.pdf' --title="Select a PDF file" 

NOTE: You can also use YAD, a fork of Zenity that has loads more features.

sudo add-apt-repository ppa:webupd8team/y-ppa-manager sudo apt-get update sudo apt-get install yad 

For the most part you can use it the same way — for the file browser:

Though at the time (around version 26?), it had not been updated to match the new GTK 3.14+ interface (zenity had) — it has more features, but check compatibility (based on documentation it should work on GTK+ >= 2.16.0

Yad is a dramatically enhanced fork of Zenity and has largely replaced it since the Zenity project went dormant. I see that Zenity is now back in development at Gnome.org (Gnome3 only?) but I see no way download it.

@DocSalvage — that page is fairly ancient — it says next release ‘3.2’ — I have version 3.8 on a now-fairly old Fedora 19 system (with Gnome 3.8 — quite a few bits of gnome are updated for each version of Gnome shell (so its probably been updated for 3.10 + 3.12)). It last deals with bug reports from 2005-2009 as well. You can get the latest stable version (3.8 again) from the Ubuntu repos — packages.ubuntu.com/trusty/zenity. You should also be able to find compiled versions of Yad here.

@Wilf Yes, yad is Zenity on steroids. Once you get to grips with it, it rocks as it’s so flexible. I am a convert. I yadded (couldn’t resist that, sorry) a response to the OPs question below, btw.

Just for the record, you can use dialog for a Text-based User Interface (TUI) solution.

dialog --title "text" --fselect /path/to/dir height width 
FILE=$(dialog --stdout --title "Please choose a file" --fselect $HOME/ 14 48) echo "$ file chosen." 

The output will be something like this:

Example

As pointed out by @Wilf, you can use the $LINES and $COLUMNS variables to make it fill the terminal:

$(dialog --stdout --title "Please choose a file" --fselect $HOME/ $(expr $LINES - 15) $(expr $COLUMNS - 10)) 

Though it is right alternative solution but it doesn’t provide GUI window as mentioned in question!

The commonly used line between GUIs and TUIs (textual UI) is the size of the «atom»: is it a pixel or a character?

Nice answer — the width and height of some terminals is defined by varibles such as $LINES and $COLUMNS — so you run $(dialog —stdout —title «Please choose a file» —fselect $HOME/ $(expr $LINES — 15) $(expr $COLUMNS — 10)) to make it fill the terminal/screen window.

I know this is 8 months old and also that the OP’s question has been answered. However, yad has been mentioned but no example has been offered. Here’s my solution using yad.

DIR="/home" \ i=0;for location in source destination do ((i++));selection[$i]=$(yad --center \ --width 350 \ --form \ --title="yad example" \ --text="Select $location directory" \ --field=:LBL "" \ --field=Path:DIR "$DIR" \ --separator='' ) done;\ echo "Command to run is \"rsync -av --delete $ $\"" 

The way it works is like this. We put yad in a for loop, setting the variable $location to source for the first pass and destination for the second. The output is placed in the array selection[] for which the variable i is used as the index. This is set to 0 at the start and incremented with each pass. Hence the source is saved as $ and the destination $ .

The DIR=»/home» on the first line sets the dialog default. The yad command options can be found from the terminal by typing yad —help .

Источник

Читайте также:  Oracle linux virtualbox guest addition
Оцените статью
Adblock
detector