The file selection box

From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to: navigation, search
← A progress bar (gauge box)HomeThe form dialog for input →
  • The fselect (file-selection) dialog box shows a text data entry window in which operator can type or select a filename / directory name from the directory or filepath.
  • An operator can use tab or arrow keys to move between the windows. Press the space-bar to select the filename into box.

Syntax

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

Where,

  • filepath - can be a filepath in which case the file and directory windows will display the contents of the path and the text-entry window will contain the preselected filename.
  • height - set dialog box height.
  • width - set dialog box width.

Example

  • Create a script called deletefile.sh:
#!/bin/bash
# deletefile.sh - Remove the file using dialog box
# -------------------------------------------------
# purpose - remove file
#  $1 - filename 
function delete_file(){
	local f="$1"
	local m="$0: file $f failed to delete."
	if [ -f $f ] 
	then
		/bin/rm $FILE && m="$0: $f file deleted."
	else
		m="$0: $f is not a file."
	fi 
	dialog --title "Remove file" --clear --msgbox "$m" 10 50
}
 
# select filename using dialog
# store it to $FILE
FILE=$(dialog --title "Delete a file" --stdout --title "Please choose a file to delete" --fselect /tmp/ 14 48)
 
# delete file
[ ! -z $FILE ] && delete_file "$FILE"

Save and close the file. Run it as follows:

chmod +x deletefile.sh
./deletefile.sh

Sample outputs:

External links

← A progress bar (gauge box)HomeThe form dialog for input →