Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将此Python脚本转换为shell脚本?_Python_Bash_Shell_Terminal_Executable - Fatal编程技术网

如何将此Python脚本转换为shell脚本?

如何将此Python脚本转换为shell脚本?,python,bash,shell,terminal,executable,Python,Bash,Shell,Terminal,Executable,这是一个python脚本 with open('a') as f: a, b = f a, b = a.strip(), b.strip() 它只是打开文件a,然后第一行变成a,第二行变成b 现在我可以在python脚本的任何地方使用a和b with open('a') as f: a, b = f a, b = a.strip(), b.strip() 例如: print a print b 我需要的代码与此完全相同,但用于Bash。将permission

这是一个python脚本

with open('a') as f:
    a, b = f
    a, b = a.strip(), b.strip()
它只是打开文件a,然后第一行变成a,第二行变成b

现在我可以在python脚本的任何地方使用a和b

with open('a') as f:
    a, b = f
    a, b = a.strip(), b.strip()
例如:

print a
print b

我需要的代码与此完全相同,但用于Bash。

将permission设置为execution,并将其添加到第一行

#!/usr/bin/env python

首先,您需要添加一个shebang行 到脚本的顶部,如下所示:

!/bin/python

在那里的部分后,舍邦!是指向python环境的路径,在Linux平台上通常是/bin/python

之后,需要使用chmod命令使文件可执行

chmod u+x/path/to/file


此命令将使该文件可供用户执行。然后,只需在命令行中键入文件的路径,就可以运行脚本。

python代码为什么不是Linux可执行脚本?它在Linux上运行,如果您设置了执行权限,它是可执行的。@ThisSuitesBlackNot,我需要它用于bash,我不会通过python脚本命令执行它。现代发行版需要/usr/bin/env python2,因为/usr/bin/env python指向python3到python环境的路径将取决于操作系统和/或OP是否从源代码构建python。So/usr/bin/python2对于OP可能正确,也可能不正确。