~dricottone/my-utils

ref: b29d2b4a9bcb2e3b35c41e5dbb43d6e031999a06 my-utils/whichhead -rwxr-xr-x 630 bytes
b29d2b4aDominic Ricottone Restarting with fresh commit history 5 years ago
                                                                                
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
#!/bin/sh

# whichhead
# =========
# USAGE: whichhead PROGRAM [OPTIONS]
#
# Print the first lines of a program

help_msg() {
  cat <<-EOF
	Print the first 10 lines from a program
	Usage: whichhead PROGRAM [OPTIONS]
	Options:
	 -n N, --lines=N: print the first N lines
	 -h, --help: print this message
	EOF
  exit 1
}

err_msg() {
  (>&2 echo "$1")
  exit 1
}

for i in "$@"; do
  case $i in
    -h|--help) help_msg;;
  esac
done

if [ "$#" -lt 1 ]; then
  err_msg "Usage: whichhead PROGRAM [OPTIONS]"
fi

BIN=$(which "$1" 2>/dev/null)
if [ -z "$BIN" ]; then
  err_msg "No program '${1}'"
fi

OPTS="${@:2}"
head "$BIN" ${OPTS[@]}