Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Suppose for example I have script like this:

(The example depicts an rysnc use case)

#!/bin/bash
echo -n "Enter Source Directory:"
read srcdir
echo -n "Enter Destination Directory:"
read dstdir
rsync -av --delete "$srcdir" "$dstdir"

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: screem


What commands can I use to prompt the user with a GUI selection window that returns the file path to the command-line?

Because If scripted file run directly (by double-click and not run in terminal) then I want to provide GUI selection window.

share|improve this question
2  
You can use Zenity: help.gnome.org/users/zenity/stable/file-selection.html.en –  TuKsn Jun 26 at 8:57
3  
Please remember to add warning about usage of GUI windows. Unnecessary windows popping up can induce rage on advanced users. –  progo Jun 26 at 9:54
4  
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 Jun 26 at 12:52
1  
Since we are mixing paradigms, why not get the user input the paths via a web app ? –  user1598390 Jun 26 at 17:36
3  
@terdon Because if we run script directly & not Run in terminal then I want to provide GUI window. –  Pandya Jun 27 at 16:26

2 Answers 2

up vote 17 down vote accepted

You can use this for files:

zenity --file-selection

and this for folders:

zenity --file-selection --directory

for usage, run:

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

Generally it matches the current theme, on my machine 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"
share|improve this answer
2  
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. –  DocSalvager Jul 3 at 3:16
    
@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 Jul 3 at 3:48
    
Yad looks quite good - it has more options than zenity :) –  Wilf Jul 3 at 3:54

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

Syntax:

dialog --title "text" --fselect /path/to/dir height width

Example:

FILE=$(dialog --stdout --title "Please choose a file" --fselect $HOME/ 14 48)
echo "${FILE} 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))
share|improve this answer
1  
Though it is right alternative solution but it doesn't provide GUI window as mentioned in question! –  Pandya Jun 26 at 9:16
1  
I know, but someone else might find it useful. I posted the screenshot to avoid any confusion. –  kraxor Jun 26 at 9:24
4  
And, to be fair, it qualifies as a GUI IMHO. –  kraxor Jun 26 at 9:26
1  
The commonly used line between GUIs and TUIs (textual UI) is the size of the "atom": is it a pixel or a character? –  progo Jun 26 at 9:52
1  
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. –  Wilf Jun 26 at 14:58

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.