它是可能的管道到/从剪贴板在Bash?
无论是连接到设备手柄还是使用辅助应用程序,我都找不到任何东西。
例如,如果/dev/clip是一个连接到剪贴板的设备,我们可以这样做:
cat /dev/clip # Dump the contents of the clipboard
cat foo > /dev/clip # Dump the contents of "foo" into the clipboard
它是可能的管道到/从剪贴板在Bash?
无论是连接到设备手柄还是使用辅助应用程序,我都找不到任何东西。
例如,如果/dev/clip是一个连接到剪贴板的设备,我们可以这样做:
cat /dev/clip # Dump the contents of the clipboard
cat foo > /dev/clip # Dump the contents of "foo" into the clipboard
当前回答
几年前我写的几个Windows程序。它们允许您转储,推送,追加和打印剪贴板。它是这样工作的:
dumpclip | perl -pe "s/monkey/chimp/g;" | pushclip
它包括源代码:cmd_clip.zip
其他回答
下面是一个可以在多个平台上使用的Bash脚本,用于读取剪贴板。
如果您要添加功能(例如,更多的平台),请在这里编辑脚本。
#!/bin/bash
# WF 2013-10-04
#
# Multi-platform clipboard read access
#
# Supports
# Mac OS X
# Git shell / Cygwin (Windows)
# Linux (e.g., Ubuntu)
#
# Display an error
#
error() {
echo "error: $1" 1>&2
exit 1
}
#
# getClipboard
#
function getClipboard() {
os=`uname`
case $os in
# Git Bash (Windows)
MINGW32_NT-6.1)
cat /dev/clipboard;;
# Mac OS X
Darwin*)
pbpaste;;
# Linux
Linux*)
# Works only for the X clipboard - a check that X is running might be due
xclip -o;;
*)
error "unsupported os $os";;
esac
}
tmp=/tmp/clipboard$$
getClipboard >$tmp
cat $tmp
# Comment out for debugging
rm $tmp
Try
xclip
xclip - command line interface to X selections (clipboard)
man
这是一个简单的Python脚本,可以满足你的需要:
#!/usr/bin/python
import sys
# Clipboard storage
clipboard_file = '/tmp/clipboard.tmp'
if(sys.stdin.isatty()): # Should write clipboard contents out to stdout
with open(clipboard_file, 'r') as c:
sys.stdout.write(c.read())
elif(sys.stdout.isatty()): # Should save stdin to clipboard
with open(clipboard_file, 'w') as c:
c.write(sys.stdin.read())
将其保存为路径中的可执行文件(我将其保存到/usr/local/bin/clip.)你可以输入要保存到剪贴板上的内容。
echo "Hello World" | clip
你可以将剪贴板中的内容传输到其他程序……
clip | cowsay
_____________
< Hello World >
-------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
单独运行它只会输出剪贴板中的内容。
确保使用别名xclip="xclip -selection c" 否则你将无法使用Ctrl+v进行粘贴。
例子: 执行echo -n test | xclip命令后,Ctrl+v将粘贴test
在Linux中,这是可行的:
cat filename | xclip