简体   繁体   English

Bash别名为Python脚本 - 是否可能?

[英]Bash alias to Python script — is it possible?

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. 我想要“归类”成Python脚本的特定别名恰好是使用cUrl -o(输出到文件)选项的别名。 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. 我想我可以很容易地把它变成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}. 与此想法一致的另一个概念是我必须制作与下载平台无关的传统Mac OS(官方称为“OS 9”或“Classic”)的功能:将URL写入文件的某些部分可见文件navigator {Konqueror,Dolphin,Nautilus,Finder或Explorer}。 I know that only a scant few file types support this kind of thing using some other command-line tools (exiv2, wrjpgcom, etc). 我知道只有少数文件类型使用其他一些命令行工具(exiv2,wrjpgcom等)支持这种事情。 Which is perfectly fine with me as I only use this alias to download single-page image files such as JPEGs anyways. 这对我来说非常好,因为我只使用这个别名来下载单页图像文件,例如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. 我认为我可以通过让脚本传递字符串传递字符串来充分利用Python的强大功能,该字符串是下载的源URL(由用户输入并首先由cUrl使用)到exiv2这样可以将其写入到注释块,EXIF用户注释块,以及(作为第一个也是最差的例子)Windows XP的文件描述字段。 Starting small is sometimes a good way to start. 从小做起有时是一个很好的开始。

Hope someone has advice or suggestions. 希望有人有意见或建议。

BZT BZT

The relevant section from 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 eg 所以,应该没有什么可以阻止你这样做

$ 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程序。

I thought Pycurl might be the answer. 我以为Pycurl可能就是答案。 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?' 我在列表中询问pycurl是否有一个“curl -o”类似物,然后询问'如果是这样:如何在Python脚本中编写它/它们?' His reply was the following: 他的答复如下:

"curl.setopt(pycurl.WRITEDATA, fp) “curl.setopt(pycurl.WRITEDATA,fp)

possibly combined with: 可能结合:

curl.setopt(pycurl.WRITEFUNCITON, callback) " curl.setopt(pycurl.WRITEFUNCITON,callback)“

...along with Sourceforge links to two revisions of retriever.py. ...以及Sourceforge链接到两个版本的retrieve.py。 I can barely recall where easy_install put the one I've got; 我几乎无法回想起easy_install把我得到的那个放在哪里; 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 BZT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM