再分享一个bash script 读入文件的行到剪贴板 附剪贴板相关知识
-
- 帖子: 77
- 注册时间: 2008-12-31 17:45
再分享一个bash script 读入文件的行到剪贴板 附剪贴板相关知识
因为最近要批量从一个论坛下载音乐 下载需使用win下专门的软件但软件不支持批量下载 但可以监视剪贴板 遇到可下载的东西会自动弹出下载窗口 我在虚拟机virtualbox下先拍照(类似于win的还原点)然后安装专用的下载软件 vb上设置双向剪贴板开启win下下载程序(如迅雷(设置自动下载的话 就可以批量下载了)) lin下以下载地址文件作参数运行该脚本
#!/bin/bash
#功能: 每隔17s更新剪贴板的内容 内容来自于文件 每次更新一行
#用法: 命令后接要剪贴板内容读取源文件
#license: GPL v3
#Contributor: yjqg6666, astolia
#版本: v0.1 alpha2
#参数检查
if [ "$#" -eq "0" ];then
echo "请指定读入的文件名。"
echo "用法: $0 要读入的文件名"
exit 1
fi
if [ ! -f "$1" ];then
echo -e "读入的下载文件不存在。\n"
exit 2
elif [ ! -s "$1" ];then
echo -e "文件是空文件。\n"
exit 3
fi
#依赖检查
if ! $(which xclip >/dev/null);then
echo "请安装依赖包xclip。"
exit 4
fi
#帮助信息
if [ "$1" == "-help" ];then
echo -e "用法: $0 要读入的文件名\n"
echo -e "返回值: 1 未加上包含批量下载地址的文件名;\n\
2 包含批量下载地址的文件不存在;\n\
3 文件存在但是是空文件; \n\
4 依赖包xclip未安装。"
fi
#主程序
count=1
while read n
do
echo $n | xclip -selection clipboard
echo 剪贴板第$count次更新
sleep 17s
clear
(( count +=1 ))
done <$1
------------------分隔线-------------------------------
Historically, X clients have not handled cut-and-paste in a consistent
way, leading users to believe that X doesn't even have a working
clipboard. This isn't really accurate; there is a conventional
behavior, somewhat standardized in the ICCCM. But many apps don't
follow the conventional behavior.
X has a thing called "selections" which are just clipboards,
essentially (implemented with an asynchronous protocol so you don't
actually copy data to them). There are three standard selections
defined in the ICCCM: PRIMARY, SECONDARY, and CLIPBOARD.
The ICCCM defines these as follows:
"The selection named by the atom PRIMARY is used for all com-
mands that take only a single argument and is the principal
means of communication between clients that use the selec-
tion mechanism.
The selection named by the atom SECONDARY is used:
o As the second argument to commands taking two arguments
(for example, "exchange primary and secondary selec-
tions")
o As a means of obtaining data when there is a primary
selection and the user does not want to disturb it
The selection named by the atom CLIPBOARD is used to hold
data that is being transferred between clients, that is,
data that usually is being cut and then pasted or copied and
then pasted."
In addition, the ICCCM has a thing called "cut buffers" which most
clients no longer support.
There are two historical interpretations of the ICCCM:
a) use PRIMARY for mouse selection, middle mouse button paste, and
explicit cut/copy/paste menu items (Qt 2, GNU Emacs 20)
b) use CLIPBOARD for the Windows-style cut/copy/paste menu items;
use PRIMARY for the currently-selected text, even if it isn't
explicitly copied, and for middle-mouse-click (Netscape, Mozilla,
XEmacs, some GTK+ apps)
No one ever does anything interesting with SECONDARY as far as I can
tell.
The current consensus is that interpretation b) is correct. Qt 3 and
GNU Emacs 21 will use interpretation b), changing the behavior of
previous versions.
The correct behavior can be summarized as follows: CLIPBOARD works
just like the clipboard on Mac or Windows; it only changes on explicit
cut/copy. PRIMARY is an "easter egg" for expert users, regular users
can just ignore it; it's normally pastable only via
middle-mouse-click.
The rationale for this behavior is mostly that behavior a) has a lot
of problems, namely:
- inconsistent with Mac/Windows
- confusingly, selecting anything overwrites the clipboard
- not efficient with a tool such as xclipboard
- you should be able to select text, then paste the clipboard
over it, but that doesn't work if the selection and
clipboard are the same
- the Copy menu item is useless and does nothing,
which is confusing
- if you think of PRIMARY as the current selection,
Cut doesn't make any sense since the selection simultaneously
disappears and becomes the current selection
Application authors should follow the following guidelines to get
correct behavior:
- selecting but with no explicit copy should only set PRIMARY,
never CLIPBOARD
- middle mouse button should paste PRIMARY, never CLIPBOARD
- explicit cut/copy commands (i.e. menu items, toolbar buttons)
should always set CLIPBOARD to the currently-selected data (i.e.
conceptually copy PRIMARY to CLIPBOARD)
- explicit paste commands should paste CLIPBOARD, not PRIMARY
- possibly contradicting the ICCCM, clients don't need to support
SECONDARY, though if anyone can figure out what it's good
for they should feel free to use it for that
- cut buffers are evil; they only support ASCII, they don't
work with many clients, and they require data to be
copied to the X server. Therefore clients should avoid
using cut buffers and use only selections.
Apps that follow these guidelines give users a simple mental model to
understand what's going on. PRIMARY is the current selection. Middle
button pastes the current selection. CLIPBOARD is just like on
Mac/Windows. You don't have to know about PRIMARY if you're a newbie.
I don't think there's a sane mental model if we collapse everything
into PRIMARY and ignore clipboard, see rationale above.
A remaining somewhat odd thing about X selections is that exiting the
app you did a cut/copy from removes the cut/copied data from the
clipboard, since the selection protocol is asynchronous and requires
the source app to provide the data at paste time. The solution here
would be a standardized protocol for a "clipboard daemon" so that apps
could hand off their data to a daemon when they exit. Or
alternatively, you can run an application such as xclipboard which
constantly "harvests" clipboard selections.
References:
- the ICCCM, obviously
- http://www.xfree86.org/~keithp/talks/selection.ps
- http://www.jwz.org/doc/x-cut-and-paste.html
----------------------from http://standards.freedesktop.org/clipbo ... ----------
#!/bin/bash
#功能: 每隔17s更新剪贴板的内容 内容来自于文件 每次更新一行
#用法: 命令后接要剪贴板内容读取源文件
#license: GPL v3
#Contributor: yjqg6666, astolia
#版本: v0.1 alpha2
#参数检查
if [ "$#" -eq "0" ];then
echo "请指定读入的文件名。"
echo "用法: $0 要读入的文件名"
exit 1
fi
if [ ! -f "$1" ];then
echo -e "读入的下载文件不存在。\n"
exit 2
elif [ ! -s "$1" ];then
echo -e "文件是空文件。\n"
exit 3
fi
#依赖检查
if ! $(which xclip >/dev/null);then
echo "请安装依赖包xclip。"
exit 4
fi
#帮助信息
if [ "$1" == "-help" ];then
echo -e "用法: $0 要读入的文件名\n"
echo -e "返回值: 1 未加上包含批量下载地址的文件名;\n\
2 包含批量下载地址的文件不存在;\n\
3 文件存在但是是空文件; \n\
4 依赖包xclip未安装。"
fi
#主程序
count=1
while read n
do
echo $n | xclip -selection clipboard
echo 剪贴板第$count次更新
sleep 17s
clear
(( count +=1 ))
done <$1
------------------分隔线-------------------------------
Historically, X clients have not handled cut-and-paste in a consistent
way, leading users to believe that X doesn't even have a working
clipboard. This isn't really accurate; there is a conventional
behavior, somewhat standardized in the ICCCM. But many apps don't
follow the conventional behavior.
X has a thing called "selections" which are just clipboards,
essentially (implemented with an asynchronous protocol so you don't
actually copy data to them). There are three standard selections
defined in the ICCCM: PRIMARY, SECONDARY, and CLIPBOARD.
The ICCCM defines these as follows:
"The selection named by the atom PRIMARY is used for all com-
mands that take only a single argument and is the principal
means of communication between clients that use the selec-
tion mechanism.
The selection named by the atom SECONDARY is used:
o As the second argument to commands taking two arguments
(for example, "exchange primary and secondary selec-
tions")
o As a means of obtaining data when there is a primary
selection and the user does not want to disturb it
The selection named by the atom CLIPBOARD is used to hold
data that is being transferred between clients, that is,
data that usually is being cut and then pasted or copied and
then pasted."
In addition, the ICCCM has a thing called "cut buffers" which most
clients no longer support.
There are two historical interpretations of the ICCCM:
a) use PRIMARY for mouse selection, middle mouse button paste, and
explicit cut/copy/paste menu items (Qt 2, GNU Emacs 20)
b) use CLIPBOARD for the Windows-style cut/copy/paste menu items;
use PRIMARY for the currently-selected text, even if it isn't
explicitly copied, and for middle-mouse-click (Netscape, Mozilla,
XEmacs, some GTK+ apps)
No one ever does anything interesting with SECONDARY as far as I can
tell.
The current consensus is that interpretation b) is correct. Qt 3 and
GNU Emacs 21 will use interpretation b), changing the behavior of
previous versions.
The correct behavior can be summarized as follows: CLIPBOARD works
just like the clipboard on Mac or Windows; it only changes on explicit
cut/copy. PRIMARY is an "easter egg" for expert users, regular users
can just ignore it; it's normally pastable only via
middle-mouse-click.
The rationale for this behavior is mostly that behavior a) has a lot
of problems, namely:
- inconsistent with Mac/Windows
- confusingly, selecting anything overwrites the clipboard
- not efficient with a tool such as xclipboard
- you should be able to select text, then paste the clipboard
over it, but that doesn't work if the selection and
clipboard are the same
- the Copy menu item is useless and does nothing,
which is confusing
- if you think of PRIMARY as the current selection,
Cut doesn't make any sense since the selection simultaneously
disappears and becomes the current selection
Application authors should follow the following guidelines to get
correct behavior:
- selecting but with no explicit copy should only set PRIMARY,
never CLIPBOARD
- middle mouse button should paste PRIMARY, never CLIPBOARD
- explicit cut/copy commands (i.e. menu items, toolbar buttons)
should always set CLIPBOARD to the currently-selected data (i.e.
conceptually copy PRIMARY to CLIPBOARD)
- explicit paste commands should paste CLIPBOARD, not PRIMARY
- possibly contradicting the ICCCM, clients don't need to support
SECONDARY, though if anyone can figure out what it's good
for they should feel free to use it for that
- cut buffers are evil; they only support ASCII, they don't
work with many clients, and they require data to be
copied to the X server. Therefore clients should avoid
using cut buffers and use only selections.
Apps that follow these guidelines give users a simple mental model to
understand what's going on. PRIMARY is the current selection. Middle
button pastes the current selection. CLIPBOARD is just like on
Mac/Windows. You don't have to know about PRIMARY if you're a newbie.
I don't think there's a sane mental model if we collapse everything
into PRIMARY and ignore clipboard, see rationale above.
A remaining somewhat odd thing about X selections is that exiting the
app you did a cut/copy from removes the cut/copied data from the
clipboard, since the selection protocol is asynchronous and requires
the source app to provide the data at paste time. The solution here
would be a standardized protocol for a "clipboard daemon" so that apps
could hand off their data to a daemon when they exit. Or
alternatively, you can run an application such as xclipboard which
constantly "harvests" clipboard selections.
References:
- the ICCCM, obviously
- http://www.xfree86.org/~keithp/talks/selection.ps
- http://www.jwz.org/doc/x-cut-and-paste.html
----------------------from http://standards.freedesktop.org/clipbo ... ----------
上次由 yjqg6666 在 2011-02-09 11:06,总共编辑 8 次。
- astolia
- 论坛版主
- 帖子: 6703
- 注册时间: 2008-09-18 13:11
Re: 再分享一个bash script 读入文件的行到剪贴板 附剪贴板相关知识
最好明确写成bash或dashyjqg6666 写了: #!/bin/sh
你的本意应该是用$0,而非$*。exit后面最好改成exit 1yjqg6666 写了: #参数检查
if [ "$#" -eq "0" ]
then
echo "请指定读入的文件名."
echo "用法: $* 要读入的文件名"
exit
fi
$1应该加上引号。echo后面的输出最后一个字符是中文的引号。echo后面也应该有个exityjqg6666 写了: if [ ! -f $1 ]
then
echo "读入的下载文件不存在。”
fi
这种写法实在太丑陋了。跟上面的一样用个!就不必留个光秃秃的then了。还有exit的问题yjqg6666 写了: #依赖检查
if $(which xclip)
then
:
else
echo "请安装依赖包xclip."
fi
你这个count又不是要给子程序用,干吗要export?yjqg6666 写了: #主程序
export count=1
while read n
do
echo $n | xclip -selection clipboard
echo 剪贴板第$count次更新
sleep 17s
clear
count=$(expr $count + 1 )
done <$1
用clear也不是很礼貌,活用一些控制字符可以不换行重写
也没必要用expr,bash和dash有$(())运算符,
代码: 全选
$(($count+1))
$((count+1))
代码: 全选
((count+=1))
- ebok
- 帖子: 852
- 注册时间: 2008-02-15 0:09
Re: 再分享一个bash script 读入文件的行到剪贴板 附剪贴板相关知识
LZ这个脚本能正常运行么?
$1不是本地的文件也可以判断么?
代码: 全选
if [ ! -f $1 ]
then
echo "读入的下载文件不存在。”
fi
Somebody think they are full of niubility, so they play a zhuangbility, but only reflect their shability.
-
- 帖子: 77
- 注册时间: 2008-12-31 17:45
Re: 再分享一个bash script 读入文件的行到剪贴板 附剪贴板相关知识
除了主程序外 其它都是临时加的 主程序测试过没问题 其它的还没测试
- ebok
- 帖子: 852
- 注册时间: 2008-02-15 0:09
Re: 再分享一个bash script 读入文件的行到剪贴板 附剪贴板相关知识
好吧我蛋疼地试过了,如果$1是个网络地址例如:
http://mirrors.163.com/debian/ls-lR.patch.gz
输出是failed,所以LZ的那段代码应该是没用了。
http://mirrors.163.com/debian/ls-lR.patch.gz
代码: 全选
#!/bin/bash
a="http://mirrors.163.com/debian/ls-lR.patch.gz"
if [ -f "$a" ];then
echo "succeed"
else
echo "failed"
fi
Somebody think they are full of niubility, so they play a zhuangbility, but only reflect their shability.
-
- 帖子: 77
- 注册时间: 2008-12-31 17:45
Re: 再分享一个bash script 读入文件的行到剪贴板 附剪贴板相关知识
ebok 写了:好吧我蛋疼地试过了,如果$1是个网络地址例如:
http://mirrors.163.com/debian/ls-lR.patch.gz
输出是failed,所以LZ的那段代码应该是没用了。代码: 全选
#!/bin/bash a="http://mirrors.163.com/debian/ls-lR.patch.gz" if [ -f "$a" ];then echo "succeed" else echo "failed" fi
我的下载地址列表文件是本地文件
内容如下 中间省略若干行
一行一个下载地址
- astolia
- 论坛版主
- 帖子: 6703
- 注册时间: 2008-09-18 13:11
Re: 再分享一个bash script 读入文件的行到剪贴板 附剪贴板相关知识
ebok没看懂别人的代码啊
yjqg6666你改来改去,还是没改对啊。
另外115的东西,可以直接分析页面,获取下载链接,用linux下的下载工具一样的下

yjqg6666你改来改去,还是没改对啊。
仍然是在用中文的单双引号。而且$1不能用单引号。exit也该返回个非0值。不用原因导致的exit,后面的值最好也不要一样if [ ! -f ‘$1’ ]
then
echo "读入的下载文件不存在。”
exit
fi
另外115的东西,可以直接分析页面,获取下载链接,用linux下的下载工具一样的下
-
- 帖子: 77
- 注册时间: 2008-12-31 17:45
Re: 再分享一个bash script 读入文件的行到剪贴板 附剪贴板相关知识
谢谢再次指出astolia 写了:
仍然是在用中文的单双引号。而且$1不能用单引号。exit也该返回个非0值。
另外115的东西,可以直接分析页面,获取下载链接,用linux下的下载工具一样的下
为何不能用单引号而要用双引号呢 单引号的话里面如果有变量也不会转换
115的东西直接用分析页面下载下来的东西中文是%ab类型的

astolia linux下有没有将%ab类型的文件名转换成中文的软件或脚本
- eexpress
- 帖子: 58428
- 注册时间: 2005-08-14 21:55
- 来自: 长沙
- astolia
- 论坛版主
- 帖子: 6703
- 注册时间: 2008-09-18 13:11
Re: 再分享一个bash script 读入文件的行到剪贴板 附剪贴板相关知识
问题在于你的[ -f "$1" ]就是需要shell将$1转换成命令行参数啊,用单引号的话就成了判断当前目录下有没有个名为$1的文件了yjqg6666 写了: 为何不能用单引号而要用双引号呢 单引号的话里面如果有变量也不会转换
在你这个例子里,直接分析网页,从网页上获取文件名,然后下载时指定个输出的文件名就行了,哪里用得着什么转换yjqg6666 写了: 115的东西直接用分析页面下载下来的东西中文是%ab类型的只有用 优蛋下载下载下来的文件名显示才正常
astolia linux下有没有将%ab类型的文件名转换成中文的软件或脚本