-
Notifications
You must be signed in to change notification settings - Fork 2
/
sortn
executable file
·45 lines (35 loc) · 1.08 KB
/
sortn
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
#!/usr/bin/env bash
help() { cat <<'/help'
Usage: sortn [OPTION...] [FILE...]
Given input(s), write the first line, then pass the rest to `sort`.
Use `sortn -10` for 10 title lines instead. See `sort --help` for other options.
Part of misc-scripts: https://github.com/adamhotep/misc-scripts
sortn 0.6.20230413.1, Copyright 2005+ by Adam Katz, GPL v2+
/help
exit
}
args=()
skip=
default_skip=1
if [ "$*" = -h ]; then help; fi # special case: just `-h` => `--help`
# filter arguments for -NUMBER and --help, otherwise save them for sort
while getopts bcCdfghik:mnNo:rRsS:t:T:uVz1234567890-: arg; do
case "$arg" in
( [0-9] ) skip="$skip$arg" ;;
( [koStT] ) args+=("-$arg" "$OPTARG") ;;
( - ) [ "$OPTARG" = help ] && help || args+=("--$OPTARG") ;;
( \? ) echo "Try --help" >&2; exit 2 ;;
( * ) args+=("-$arg") ;;
esac
done
shift $((OPTIND-1))
main() {
for (( i=1; $i <= ${skip:-$default_skip}; i++ )); do
IFS= read -r title
printf "%s\n" "$title"
done
sort ${args[@]+"${args[@]}"}
}
# Calling `cat` enables us to accept files or pipes
cat "$@" |main
exit $?