99# @online-doc https://github.com/oldratlee/useful-scripts/blob/master/docs/shell.md#beer-c
1010# @author Jerry Lee (oldratlee at gmail dot com)
1111
12+ readonly PROG=" ` basename " $0 " ` "
13+
14+ usage () {
15+ [ -n " $1 " -a " $1 " != 0 ] && local out=/dev/stderr || local out=/dev/stdout
16+
17+ > $out cat << EOF
18+ Usage: ${PROG} [OPTION]... [command [command_args ...]]
19+ Run command and put output to system clipper.
20+ If no command is specified, read from stdin(pipe).
21+
22+ Example:
23+ ${PROG} echo "hello world!"
24+ ${PROG} grep -i 'hello world' menu.h main.c
25+ set | ${PROG}
26+ ${PROG} -q < ~/.ssh/id_rsa.pub
27+
28+ Options:
29+ -k, --keep-eol do not trim new line at end of file
30+ -q, --quiet suppress all normal output, default is false
31+ -h, --help display this help and exit
32+ EOF
33+
34+ exit $1
35+ }
36+
37+ # ###############################################################################
38+ # parse options
39+ # ###############################################################################
40+
41+ quiet=false
42+ eol=-n
43+ while [ $# -gt 0 ]; do
44+ case " $1 " in
45+ -k|--keep-eol)
46+ eol=
47+ shift
48+ ;;
49+ -q|--quiet)
50+ quiet=true
51+ shift
52+ ;;
53+ -h|--help)
54+ usage
55+ ;;
56+ --)
57+ shift
58+ args=(" ${args[@]} " " $@ " )
59+ break
60+ ;;
61+ * )
62+ # if not option, treat all follow args as command
63+ args=(" ${args[@]} " " $@ " )
64+ break
65+ ;;
66+ esac
67+ done
68+
69+ # ###############################################################################
70+ # biz logic
71+ # ###############################################################################
72+
1273copy () {
1374 case " ` uname` " in
1475 Darwin* )
@@ -21,8 +82,12 @@ copy() {
2182}
2283
2384teeAndCopy () {
24- tee >( content=" $( cat) " ; echo -n " $content " | copy)
85+ $quiet && local out=/dev/null || local out=/dev/stdout
86+ tee >(
87+ content=" $( cat) "
88+ echo $eol " $content " | copy
89+ ) > $out
2590}
2691
27- [ $# -eq 0 ] && teeAndCopy ||
28- " $@ " | teeAndCopy
92+ [ ${ # args[@]} -eq 0 ] && teeAndCopy ||
93+ " ${args[@]} " | teeAndCopy
0 commit comments