bash 如何将此 Python 脚本转换为 shell 脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18321458/
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
How to convert this Python script to a shell script?
提问by user2697529
This is a python script.
这是一个python脚本。
with open('a') as f:
a, b = f
a, b = a.strip(), b.strip()
it simply opens the file "a" and then first line becomes a, second line becomes b
它只是打开文件“a”,然后第一行变成a,第二行变成b
now i can use a and b anywhere in the python script.
现在我可以在 python 脚本的任何地方使用 a 和 b。
example:
例子:
print a
print b
I need code that is exactly like this but for Bash.
我需要与此完全相同的代码,但用于 Bash。
回答by dare
set permision as execution and add this at first line
将权限设置为执行并将其添加到第一行
#!/usr/bin/env python
回答by Bryan
Firstly you will need to add a shebang line to the top of your script like so:
首先,您需要在脚本的顶部添加一条 shebang 行,如下所示:
#!/bin/python
#!/bin/python
Where the portion after the shebang "#!" Is the path to your python environment which is often /bin/python on Linux platforms.
shebang“#!”之后的部分在哪里 是你的 python 环境的路径,在 Linux 平台上通常是 /bin/python 。
After that you will need to use the chmod command to make the file executable.
之后,您将需要使用 chmod 命令使文件可执行。
chmod u+x /path/to/file
chmod u+x /path/to/file
This command will make the file executable for the user. You will then be able to run the script by just typing the path to the file into the command line.
此命令将使文件对用户可执行。然后,您只需在命令行中键入文件的路径即可运行该脚本。