-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.sh
executable file
·63 lines (50 loc) · 1.3 KB
/
example.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
#!/bin/sh
#
# An example cat implementation using petopts.sh option parsing.
options="h n v w:"
usage="[-hnv] [-w width] [file ...]"
version="cat version 1.0"
main() {
[ "$opt_h" ] && usage
[ "$opt_v" ] && version
[ "$opt_w" ] &&
case $arg_w in *[!0-9]*)
die "invalid width: $arg_w"
esac
# Both options enable line numbers.
if [ "$opt_n" ] || [ "$opt_w" ]; then
withln=true
else
withln=false
fi
# Read from stdin if no files are specified.
[ $# = 0 ] && set /dev/stdin
for file do
[ "$file" = - ] &&
file=/dev/stdin
! [ -e "$file" ] &&
die "$file: No such file or directory"
! [ -r "$file" ] &&
die "$file: Permission denied"
ln=1
while IFS= read -r line || [ "$line" ]; do
if "$withln"; then
printf '%*s\t%s\n' "${arg_w:-6}" \
"$ln" "$line"
ln=$((ln+1))
else
printf '%s\n' "$line"
fi
done <"$file"
done
}
die() { printf '%s: %s\n' "${0##*/}" "$*" >&2; exit 1; }
usage() { printf 'usage: %s %s\n' "${0##*/}" "$usage"; exit; }
version() { printf '%s\n' "$version"; exit; }
for o in $options; do unset "opt_${o%:}"; done
while getopts ":$options" o; do case $o in
:) die "option requires an argument -- $OPTARG" ;;
\?) die "unknown option -- $OPTARG" ;;
*) eval "opt_$o=\$((opt_$o + 1)) arg_$o=\$OPTARG" ;;
esac; done; shift "$((OPTIND - 1))"
main "$@"