Python 脚本的 Bash 别名——有可能吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2279749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash alias to Python script -- is it possible?
提问by silversleevesx
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.
我希望“分类”到 Python 脚本中的特定别名恰好是使用 cUrl -o(输出到文件)选项的别名。我想我可以很容易地把它变成一个 BASH 函数,但是有人告诉我,我可以通过接受我的想法并将它们制作成 Python 脚本来避免 BASH 不同版本和“风格”的怪癖和陷阱。
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.
与这个想法一致的是另一个概念,我必须使传统 Mac OS(正式称为“OS 9”或“经典”)的一个特性与平台无关的下载相关:将 URL 写入文件的某些部分,从文件中可见导航器 {Konqueror、Dolphin、Nautilus、Finder 或 Explorer}。我知道只有少数文件类型使用其他一些命令行工具(exiv2、wrjpgcom 等)支持这种事情。这对我来说非常好,因为我只使用这个别名来下载单页图像文件,例如 JPEG。
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.
我认为我也可以通过让脚本将作为下载源 URL 的字符串(由用户输入并首先由 cUrl 使用)传递给像 exiv2 这样的东西,它可以将它写入到注释块、EXIF 用户注释块和(作为第一个也是最坏的例子)Windows XP 的文件描述字段。从小处着手有时是一个很好的开始方式。
Hope someone has advice or suggestions.
希望有人给点意见或建议。
BZT
BZT
回答by unwind
The relevant sectionfrom the Bash manual states:
Bash 手册中的相关部分指出:
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:
然后你可以像使用任何其他 shell 命令一样使用它:
$ geturl http://example.com/excitingstuff.jpg
And this would simply call your Python program.
这将简单地调用您的 Python 程序。
回答by silversleevesx
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)
我认为 Pycurl 可能是答案。Ahh Daniel Sternberg 和他天真无邪的假设,即每个人都知道他在做什么。我在列表中询问 pycurl 是否有“curl -o”类似物,然后问“如果是这样:如何在 Python 脚本中对其/它们进行编码?” 他的回复如下:
“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?
curl.setopt(pycurl.WRITEFUNCITON, callback) "
...连同 Sourceforge 链接到两个版本的retrier.py。我几乎不记得easy_install 把我的那个放在哪里了;我应该如何比较它们?
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
BZT

