-
Notifications
You must be signed in to change notification settings - Fork 2
/
index
executable file
·78 lines (68 loc) · 1.44 KB
/
index
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
#!/bin/bash
# Set up same defaults
set -o errexit # Exit on error
set -o nounset # It is an error to use an unset variable
# Store program name in program var
program=`basename $0`
# Main is called from the end of the script
function main() {
parse_options
index_files
}
# Sets variables full and dir
function parse_options() {
full=false
while getopts "hf" opt; do
case "${opt}" in
h)
usage
exit 0
;;
f)
full=true
;;
*)
usage
exit 1;
;;
esac
done
shift $((OPTIND-1))
# Only one argument is allowed
if [[ $# -gt 1 ]]; then
usage
exit 1
fi
dir=${1:-.}
}
# Usage text
# A description of the program, followed by an empty line
#
# Usage text
# options described
#
function usage() {
cat <<EOT
"$program" prints a list of commands in the given directory. It prints the header of
the help text (the lines up to the first newline).
Usage: $program [-h] [-f] [dir]
-h - Prints this help text
-f - Display the full help text, not only the header.
dir - directory (default is .)
EOT
}
# The script specific code is called from main
function index_files() {
for file in $dir/*; do
name=`basename $file`
if [ -x "$name" ]; then
usage=`$name -h`
if [ $full ]; then
echo $usage
else
echo $usage | sed -e '/^$/,$d'
fi
fi
done
}
main