首页 > shell 脚本中无法通过xclip获取剪贴板中的内容

shell 脚本中无法通过xclip获取剪贴板中的内容

#!/usr/bin/env sh

url=`xclip -o` # 什么都没有获取到
if [[ ! "${url}" ]]; then
    exit 2
fi

`which chromium`  $weibo_url

终端执行的话, xclip -o 是有内容输出的; 但执行脚本的话, 只会输出 stderr 2;

xclip 手册中对 -o 的描述: print the selection to standard out( generally for piping to a file or pragram).


xclip需要连接Xorg服务器,所以需要正确设置DISPLAY值。
设置DISPLAY值的方法有两种,一种是从gnome-terminal或者konsole之类的图形界面终端里运行你的脚本,这样脚本会自动继承图形终端设置的DISPLAY值,另一种是在脚本里手动设置DISPLAY值,方法是在xclip -o的上一行,加上DISPLAY=:0,如果你的Xorg服务器设置不一样的话,有可能需要修改:0

类似的代码:

url=`xclip -d $DISPLAY -o` # or url=`xsel -b`
【热门文章】
【热门文章】