如何使可执行文件在shell中使用 您所在的位置:网站首页 shell如何调用函数 如何使可执行文件在shell中使用

如何使可执行文件在shell中使用

2023-03-16 04:46| 来源: 网络整理| 查看: 265

这是我如何制作可执行脚本。它不考虑eggs或类似的东西。这只是一个简单的脚本,我希望能够执行。我假设你正在使用Linux。

#! /usr/bin/env python import sys def main(): # # Do something ... Whatever processing you need to do, make it happen here. # Don't shove everything into main, break it up into testable functions! # # Whatever this function returns, is what the exit code of the interpreter, # i.e. your script, will be. Because main is called by sys.exit(), it will # behave differently depending on what you return. # # So, if you return None, 0 is returned. If you return integer, that # return code is used. Anything else is printed to the console and 1 (error) # is returned. # if an_error_occurred: return 'I\'m returning a string, it will be printed and 1 returned' # Otherwise 0, success is returned. return 0 # This is true if the script is run by the interpreter, not imported by another # module. if __name__ == '__main__': # main should return 0 for success, something else (usually 1) for error. sys.exit(main())

现在,如果您的权限设置正确,您可以执行此脚本。

有一件事要认识到,当你的脚本被处理时,每一行都在解释器中执行。无论处理器如何“得到它”,情况都是如此。这就是将脚本作为模块导入并作为脚本执行,基本上它们都是相同的,因为它们都执行模块的每一行。

一旦您意识到您的脚本在运行时只是执行,您会意识到函数的顺序并不重要。函数声明是一个函数声明。这是当你致电重要的功能。

所以,一般情况下,你的脚本的布局看起来像这样

def func1(): pass def func2(): pass def main(): return 0 if __name__ == '__main__': sys.exit(main())

你创建你首先要使用的功能,那么你使用它们。希望能帮助到你。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有