Linux 使用参数从 bash 调用 Python 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14155669/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:12:05  来源:igfitidea点击:

Call Python script from bash with argument

pythonlinuxbashshelldebian

提问by Jimmy

I know that I can run a python script from my bash script using the following:

我知道我可以使用以下命令从我的 bash 脚本运行 python 脚本:

python python_script.py

But what about if I wanted to pass a variable / argument to my python script from my bash script. How can I do that?

但是,如果我想从我的 bash 脚本将一个变量/参数传递给我的 python 脚本呢?我怎样才能做到这一点?

Basically bash will work out a filename and then python will upload it, but I need to send the filename from bash to python when I call it.

基本上bash会计算出一个文件名,然后python会上传它,但是当我调用它时我需要将文件名从bash发送到python。

采纳答案by duckman_1991

To execute a python script in a bash script you need to call the same command that you would within a terminal. For instance

要在 bash 脚本中执行 python 脚本,您需要调用与在终端中相同的命令。例如

> python python_script.py var1 var2

To access these variables within python you will need

要在 python 中访问这些变量,您需要

import sys
print sys.argv[0] # prints python_script.py
print sys.argv[1] # prints var1
print sys.argv[2] # prints var2

回答by NPE

Use

python python_script.py filename

and in your Python script

并在您的 Python 脚本中

import sys
print sys.argv[1]

回答by miku

Beside sys.argv, also take a look at the argparsemodule, which helps define options and arguments for scripts.

此外sys.argv,还可以查看argparse模块,该模块有助于定义脚本的选项和参数。

The argparse module makes it easy to write user-friendly command-line interfaces.

argparse 模块可以轻松编写用户友好的命令行界面。

回答by HappyHacking

and take a look at the getoptmodule. It works quite good for me!

并查看getopt模块。它对我来说非常好!

回答by burseaner

use in the script:

在脚本中使用:

echo $(python python_script.py arg1 arg2) > /dev/null

or

或者

python python_script.py "string arg"  > /dev/null

The script will be executed without output.

该脚本将在没有输出的情况下执行。

回答by user77115

Embedded option:

嵌入式选项:

Wrap python code in a bash function.

将 python 代码包装在 bash 函数中。

#!/bin/bash

function current_datetime {
python - <<END
import datetime
print datetime.datetime.now()
END
}

# Call it
current_datetime

# Call it and capture the output
DT=$(current_datetime)
echo Current date and time: $DT

Use environment variables, to pass data into to your embedded python script.

使用环境变量将数据传递到您的嵌入式 python 脚本中。

#!/bin/bash

function line {
PYTHON_ARG="" python - <<END
import os
line_len = int(os.environ['PYTHON_ARG'])
print '-' * line_len
END
}

# Do it one way
line 80

# Do it another way
echo $(line 80)

http://bhfsteve.blogspot.se/2014/07/embedding-python-in-bash-scripts.html

http://bhfsteve.blogspot.se/2014/07/embedding-python-in-bash-scripts.html

回答by Dave Thebuskeruk

I have a bash script that calls a small python routine to display a message window. As I need to use killall to stop the python script I can't use the above method as it would then mean running killall python which could take out other python programmes so I use

我有一个 bash 脚本,它调用一个小的 python 例程来显示一个消息窗口。因为我需要使用 killall 来停止 python 脚本,所以我不能使用上面的方法,因为这意味着运行 killall python 可以取出其他 python 程序,所以我使用

pythonprog.py "$argument"&# The & returns control straight to the bash script so must be outside the backticks. The preview of this message is showing it without "`" either side of the command for some reason.

pythonprog.py "$argument"&# & 将控制权直接返回给 bash 脚本,因此必须在反引号之外。由于某种原因,此消息的预览显示命令两侧没有“`”。

As long as the python script will run from the cli by name rather than python pythonprog.py this works within the script. If you need more than one argument just use a space between each one within the quotes.

只要 python 脚本将按名称从 cli 运行而不是 python pythonprog.py 这在脚本中工作。如果您需要多个参数,只需在引号内的每个参数之间使用一个空格。