Skip to content

Commit 0a8564a

Browse files
committed
add quiet and keep eol option for c
1 parent db43daa commit 0a8564a

File tree

2 files changed

+94
-7
lines changed

2 files changed

+94
-7
lines changed

c

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,67 @@
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+
1273
copy() {
1374
case "`uname`" in
1475
Darwin*)
@@ -21,8 +82,12 @@ copy() {
2182
}
2283

2384
teeAndCopy() {
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

docs/shell.md

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151

5252
更多说明参见[拷贝复制命令行输出放在系统剪贴板上](http://oldratlee.com/post/2012-12-23/command-output-to-clip)
5353

54-
### 示例
54+
### 用法/示例
5555

5656
有3种使用风格,根据需要或是你的偏好选取。
5757

@@ -72,13 +72,35 @@ $ echo -e 'a\nb' | nl | c
7272
$ gb | c
7373

7474
# 3. 从标准输入读取内容。拷贝文件内容时这种方式最直接。
75-
$ c < id_rsa.pub
76-
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAz+ETZEgoLeIiC0rjWewdDs0sbo8c...== [email protected]
75+
$ c < ~/.ssh/id_rsa.pub
76+
ssh-rsa EAAAABIwAAAQEAz+ETZEgoLeIiC0rjWewdDs0sbo8c...== [email protected]
77+
78+
# -q选项:拷贝但不输出。
79+
# 当输出内容比较多、又不关心输出内容和命令执行进展时,可以使用这个选项。
80+
$ c -q < ~/.ssh/id_rsa.pub
81+
82+
# 帮助信息
83+
$ c --help
84+
Usage: c [OPTION]... [command [command_args ...]]
85+
Run command and put output to system clipper.
86+
If no command is specified, read from stdin(pipe).
87+
88+
Example:
89+
c echo "hello world!"
90+
c grep -i 'hello world' menu.h main.c
91+
set | c
92+
c -q < ~/.ssh/id_rsa.pub
93+
94+
Options:
95+
-k, --keep-eol do not trim new line at end of file
96+
-q, --quiet suppress all normal output, default is false
97+
-h, --help display this help and exit
7798
```
7899
79100
### 参考资料
80101
81-
[拷贝复制命令行输出放在系统剪贴板上](http://oldratlee.com/post/2012-12-23/command-output-to-clip),给出了不同系统可用命令。
102+
- [拷贝复制命令行输出放在系统剪贴板上](http://oldratlee.com/post/2012-12-23/command-output-to-clip),给出了不同系统可用命令。
103+
- 关于文本文件最后的换行,参见[Why should text files end with a newline?](https://stackoverflow.com/questions/729692)
82104
83105
:beer: [coat](../coat)
84106
----------------------

0 commit comments

Comments
 (0)