-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfilebrowse.sh
110 lines (95 loc) · 3.76 KB
/
filebrowse.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/bash
: '
filebrowse.sh written by Claude Pageau
This is a whiptail file browser demo that allows navigating through a directory
structure and select a specified file type per filext variable.
It Returns a filename path if selected. Esc key exits.
This sample code can be used in a script menu to perform an operation that
requires selecting a file.
'
startdir="/home/pi"
filext='jpg'
menutitle="$filext File Selection Menu"
#------------------------------------------------------------------------------
function Filebrowser()
{
# first parameter is Menu Title
# second parameter is optional dir path to starting folder
# otherwise current folder is selected
if [ -z $2 ] ; then
dir_list=$(ls -lhp | awk -F ' ' ' { print $9 " " $5 } ')
else
cd "$2"
dir_list=$(ls -lhp | awk -F ' ' ' { print $9 " " $5 } ')
fi
curdir=$(pwd)
if [ "$curdir" == "/" ] ; then # Check if you are at root folder
selection=$(whiptail --title "$1" \
--menu "PgUp/PgDn/Arrow Enter Selects File/Folder\nor Tab Key\n$curdir" 0 0 0 \
--cancel-button Cancel \
--ok-button Select $dir_list 3>&1 1>&2 2>&3)
else # Not Root Dir so show ../ BACK Selection in Menu
selection=$(whiptail --title "$1" \
--menu "PgUp/PgDn/Arrow Enter Selects File/Folder\nor Tab Key\n$curdir" 0 0 0 \
--cancel-button Cancel \
--ok-button Select ../ BACK $dir_list 3>&1 1>&2 2>&3)
fi
RET=$?
if [ $RET -eq 1 ]; then # Check if User Selected Cancel
return 1
elif [ $RET -eq 0 ]; then
if [[ -d "$selection" ]]; then # Check if Directory Selected
Filebrowser "$1" "$selection"
elif [[ -f "$selection" ]]; then # Check if File Selected
if [[ $selection == *$filext ]]; then # Check if selected File has .jpg extension
if (whiptail --title "Confirm Selection" --yesno "DirPath : $curdir\nFileName: $selection" 0 0 \
--yes-button "Confirm" \
--no-button "Retry"); then
filename="$selection"
filepath="$curdir" # Return full filepath and filename as selection variables
else
Filebrowser "$1" "$curdir"
fi
else # Not correct extension so Inform User and restart
whiptail --title "ERROR: File Must have $filext Extension" \
--msgbox "$selection\nYou Must Select a $filext file" 0 0
Filebrowser "$1" "$curdir"
fi
else
# Could not detect a file or folder so Try Again
whiptail --title "ERROR: Selection Error" \
--msgbox "Error Changing to Path $selection" 0 0
Filebrowser "$1" "$curdir"
fi
fi
}
Filebrowser "$menutitle" "$startdir"
exitstatus=$?
if [ $exitstatus -eq 0 ]; then
if [ "$selection" == "" ]; then
echo "User Pressed Esc with No File Selection"
else
whiptail --title "File was selected" --msgbox " \
File Selected information
Filename : $filename
Directory: $filepath
\
" 0 0 0
echo ""
echo "---- $0 variable output values -----"
echo ""
echo '$filename = '$filename
echo '$filepath = '$filepath
echo ""
echo "You can combine variables per"
echo 'echo $filepath/$filename'
echo "result is"
echo "$filepath/$filename"
echo ""
echo "Variables can be used in command execution"
fi
else
echo "User Pressed Cancel. with No File Selected."
fi
echo ""
echo "This is demo code that can be used in your own projects"