-
Notifications
You must be signed in to change notification settings - Fork 8
/
update_index
executable file
·56 lines (48 loc) · 1.34 KB
/
update_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
#! /bin/sh
# update_index (Bourne shell script) -- adds line(s) for downloads to the appropriate index.txt
# Usage: update_index [ -s | <filename> <url> ] <description>
# Where
# -s accept "filename = url" on stdin instead of on the command line
self=`basename $0`
allowed_options=s
# create_entry() takes a filename, a URL and a description as arguments
function create_entry
{
if [ "$3" ] ; then
echo "$1 <$2> $3"
else
echo "$1 <$2>"
fi
}
# == command-line handling ==
# process options
set -e
eval set -- `getopt --shell=sh -n $self --options=+$allowed_options "$@"`
set +e # getopt would have already reported the error
while [ x"$1" != x-- ] ; do
case "$1" in
-s) use_stdin=true ;;
esac
shift # get rid of the option (or its arg if the inner shift already got rid it)
done
shift # get rid of the "--"
# process remaining args
if [ $use_stdin ] ; then
# accept "filename = url" on stdin
read l
# extract the text from before and after the " = ", respectively
filename=${l% =*}
url=${l#*= }
else
filename="$1"
url="$2"
shift
shift
fi
desc="$*"
# == processing ==
# only the non-directory part of the filename is saved in the index
basename=${filename##*/}
# figure out which index.txt to append to and do so
index=${filename%/*}/index.txt
create_entry "$basename" "$url" "$desc" >> $index