Python 脚本的 Bash 别名——可能吗?

发布于 2024-08-21 15:49:29 字数 553 浏览 5 评论 0原文

我想要“分类”到 Python 脚本中的特定别名恰好是使用 cUrl -o(输出到文件)选项的别名。我想我可以很容易地将它变成 BASH 函数,但有人建议我,通过采用我的想法并将其制作为 Python 脚本,我可以避免 BASH 的不同版本和“风格”的怪癖和陷阱。

与这个想法相一致的是我必须制作旧版 Mac OS(正式称为“OS 9”或“Classic”)的一项与平台无关的下载功​​能:将 URL 写入文件中可见的文件的某些部分导航器 {Konqueror、Dolphin、Nautilus、Finder 或 Explorer}。我知道只有少数文件类型使用其他一些命令行工具(exiv2、wrjpgcom 等)支持这种事情。这对我来说完全没问题,因为我只使用这个别名来下载单页图像文件,例如 JPEG。

我想我也可以充分利用Python的强大功能,让脚本将下载的源URL字符串(由用户输入并首先由cUrl使用)传递给类似exiv2的东西,它可以将其写入注释块、EXIF 用户注释块以及(作为第一个也是最糟糕的示例)Windows XP 的文件描述字段。从小事做起有时是一个很好的开始方式。

希望有人给意见或者建议。

BZT

The particular alias I'm looking to "class up" into a Python script happens to be one that makes use of the cUrl -o (output to file) option. I suppose I could as easily turn it into a BASH function, but someone advised me that I could avoid the quirks and pitfalls of the different versions and "flavors" of BASH by taking my ideas and making them Python scripts.

Coincident with this idea is another notion I had to make a feature of legacy Mac OS (officially known as "OS 9" or "Classic") pertaining to downloads platform-independent: writing the URL to some part of the file visible from one's file navigator {Konqueror, Dolphin, Nautilus, Finder or Explorer}. I know that only a scant few file types support this kind of thing using some other command-line tools (exiv2, wrjpgcom, etc). Which is perfectly fine with me as I only use this alias to download single-page image files such as JPEGs anyways.

I reckon I might as well take full advantage of the power of Python by having the script pass the string which is the source URL of the download (entered by the user and used first by cUrl) to something like exiv2 which could write it to the Comment block, EXIF User Comment block, and (taking as a first and worst example) Windows XP's File Description field. Starting small is sometimes a good way to start.

Hope someone has advice or suggestions.

BZT

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

未蓝澄海的烟 2024-08-28 15:49:29

Bash 手册中的相关部分指出:

别名允许字符串
使用时替换某个单词
作为简单命令的第一个单词。

因此,应该没有什么可以阻止您执行以下操作:

$ alias geturl="python /some/cool/script.py"

然后您可以像任何其他 shell 命令一样使用它:

$ geturl http://example.com/excitingstuff.jpg

这将简单地调用您的 Python 程序。

The relevant section from the Bash manual states:

Aliases allow a string to be
substituted for a word when it is used
as the first word of a simple command.

So, there should be nothing preventing you from doing e.g.

$ alias geturl="python /some/cool/script.py"

Then you could use it like any other shell command:

$ geturl http://example.com/excitingstuff.jpg

And this would simply call your Python program.

月依秋水 2024-08-28 15:49:29

我认为 Pycurl 可能就是答案。啊,丹尼尔·斯滕伯格和他天真的假设,即每个人都知道他在做什么。我在列表中询问 pycurl 是否有“curl -o”类似物,然后问“如果有:如何在 Python 脚本中对其进行编码?”他的回复如下:

“curl.setopt(pycurl.WRITEDATA,fp)

可能与:

curl.setopt(pycurl.WRITEFUNCITON,回调)”

...以及 Sourceforge 链接到retrieve.py 的两个修订版。我几乎不记得 easy_install 把我拥有的那个放在哪里了;我该如何比较它们?

很明显,这位先生从未在西半球从事过服务台或电话技术支持工作,你必须假设“客户”昨天刚刚学会了如何使用梳子,并准备好引导他们完成所有事情。单线(或带有深奥链接的三线作为追逐者)不适合我。

BZT

I thought Pycurl might be the answer. Ahh Daniel Sternberg and his innocent presumptions that everybody knows what he does. I asked on the list whether or not pycurl had a "curl -o" analogue, and then asked 'If so: How would one go about coding it/them in a Python script?' His reply was the following:

"curl.setopt(pycurl.WRITEDATA, fp)

possibly combined with:

curl.setopt(pycurl.WRITEFUNCITON, callback) "

...along with Sourceforge links to two revisions of retriever.py. I can barely recall where easy_install put the one I've got; how am I supposed to compare them?

It's pretty apparent this gentleman never had a helpdesk or phone tech support job in the Western Hemisphere, where you have to assume the 'customer' just learned how to use their comb yesterday and be prepared to walk them through everything and anything. One-liners (or three-liners with abstruse links as chasers) don't do it for me.

BZT

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文