#!/bin/bash
# Linux version
# Use this script to pipe in/out of the clipboard
#
# Usage: someapp | clipboard # Pipe someapp's output into clipboard
# clipboard | someapp # Pipe clipboard's content into someapp
#
if command -v xclip 1>/dev/null; then
if [[ -p /dev/stdin ]] ; then
# stdin is a pipe
# stdin -> clipboard
xclip -i -selection clipboard
else
# stdin is not a pipe
# clipboard -> stdout
xclip -o -selection clipboard
fi
else
echo "Remember to install xclip"
fi
该脚本的OS X版本依赖于所有Mac上预装的pbCopy和pb粘贴。
#!/bin/bash
# OS X version
# Use this script to pipe in/out of the clipboard
#
# Usage: someapp | clipboard # Pipe someapp's output into clipboard
# clipboard | someapp # Pipe clipboard's content into someapp
#
if [[ -p /dev/stdin ]] ; then
# stdin is a pipe
# stdin -> clipboard
pbcopy
else
# stdin is not a pipe
# clipboard -> stdout
pbpaste
fi
$ date | cb
# clipboard contains: Tue Jan 24 23:00:00 EST 2017
粘贴
# clipboard retained from the previous block
$ cb
Tue Jan 24 23:00:00 EST 2017
$ cb | cat
Tue Jan 24 23:00:00 EST 2017
$ cb > foo
$ cat foo
Tue Jan 24 23:00:00 EST 2017
链
$ date | cb | tee updates.log
Tue Jan 24 23:11:11 EST 2017
$ cat updates.log
Tue Jan 24 23:11:11 EST 2017
# clipboard contains: Tue Jan 24 23:11:11 EST 2017
通过文件重定向复制
(按时间顺序,最后演示这个是有意义的)
# clipboard retained from the previous block
$ cb < foo
$ cb
Tue Jan 24 23:00:00 EST 2017
# note the minutes and seconds changed from 11 back to 00
if grep -q -i microsoft /proc/version; then
# on WSL: version contains the string "microsoft"
alias copy="clip.exe"
alias paste="powershell.exe Get-Clipboard"
elif grep -q -i cygwin $(uname -a); then
# on CYGWIN: uname contains the string "cygwin"
alias copy="/dev/clipboard"
alias paste="cat /dev/clipboard"
elif [[ ! -r /proc/version ]]; then
# on MAC: version is not readable at all
alias copy="pbcopy"
alias paste="pbpaste"
else
# on "normal" linux
alias copy="xclip -sel clip"
alias paste="xclip -sel clip -o"
fi
在所有系统上使用
复制:
# pipe
echo "hello world" | copy
# or for direct file input
copy < file
echo '#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
from tkinter import Tk
r = Tk()
r.withdraw()
r.clipboard_clear()
data = ""
for i in sys.stdin:
data = data + i
r.clipboard_append(data)
r.update()
r.destroy()' | sudo tee /usr/bin/copy > /dev/null
sudo chmod +x /usr/bin/copy
或者没有sudo权限(仅限一个用户):
echo '#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
from tkinter import Tk
r = Tk()
r.withdraw()
r.clipboard_clear()
data = ""
for i in sys.stdin:
data = data + i
r.clipboard_append(data)
r.update()
r.destroy()' > ~/.local/bin/copy
chmod +x ~/.local/bin/copy