-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeta.sh
executable file
·96 lines (75 loc) · 1.71 KB
/
meta.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
#!/bin/bash
SCRIPT_NAME=$(basename $0)
# Function to output usage information.
usage() {
cat << EOF
Usage: $SCRIPT_NAME [OPTIONS] COMMAND [PATTERN]... FILE...
Find or edit single line docpad metadata (YAML).
Options:
-a, --all
aply to the whole file and not just the meta.
-h, --help
display this help and exit
Commands:
list Lists the metadata that match a BRE pattern. Leave
blank to list all metadata.
edit Perform a sed command on the metadata.
Patterns:
A basic regular expression (BRE) without the escaping.
EOF
exit 1
}
# Function to ouput an error message.
error() {
echo $SCRIPT_NAME: $1. See "$SCRIPT_NAME --help".
exit 1
}
# Read option.
OPT=$1
case $OPT in
--all|-a) APPLY="all"
shift 1
;;
--help|-h) usage
;;
-*) error "$OPT is not a valid option"
;;
esac
# Validate arguments.
# If no parameter is specified
if [ $# == 0 ]; then
usage
fi
CMD=$1
shift 1
case $CMD in
list) SED_OPT='-n'
if [ "$1" == "" ]; then PATTERN=p
else PATTERN=/$1/p; fi
;;
edit) SED_OPT='-i.bak'
if [ "$1" == "" ]; then error "Missing pattern"; fi
PATTERN=$1
;;
*) error "$CMD is not a valid command"
;;
esac
if [ "$APPLY" == "all" ]; then
SED_SCRIPT="$PATTERN"
else
SED_SCRIPT="/^\-\-\-/,/^\-\-\-/{ ${PATTERN} }"
fi
shift 1
FILE_LIST=$*
if [ "$*" == "" ]; then
FILE_LIST="."
fi
#set -o posix
for FILE in $FILE_LIST
do
if [ -f $FILE ]; then
sed $SED_OPT "$SED_SCRIPT" $FILE
elif [ -d $FILE ]; then
find $FILE -type f -name '*.html*' -exec sed $SED_OPT "$SED_SCRIPT" {} \;
fi
done