仅当提供参数时,Python 脚本才在 Bash 脚本中返回空字符串

标签 python linux bash shell

如果我有一个 Python 脚本,它只打印给定的第一个参数

#!/usr/bin/env python
import sys
try:
    print sys.argv[1]
except IndexError:
    print "No args"

我可以运行它找到并获得预期的输出。现在,如果我编写一个 Bash 脚本来简单地运行 python 脚本并回显其输出。

#!/bin/bash
test="Test"
echo `python test.py`

然后 bash 脚本将成功回显“No args”。但是,如果我将第 3 行更改为

echo `python test.py $test`

然后我将简单地得到一个空字符串作为 Bash 脚本的输出,我不确定为什么。当我将它更改为

时,我什至得到空字符串
echo `python test.py "test"`

最佳答案

将反引号更改为 $()​​ 似乎有效

关于仅当提供参数时,Python 脚本才在 Bash 脚本中返回空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32022224/

相关文章:

Bash 导出多个函数

python - 属性错误: 'module' object has no attribute 'locals'

python - 高速公路无法导入名称错误

python - 类型错误 : bufsize must be an integer?

bash - 如何使 bash 函数在任何错误时返回 1

linux - 交错按列排序的行

python - flask 管理员 : Customize table name for the admin interface

python - 基于特定字符串的抓取 [python selenium]

linux - 我如何从 Linux 服务器(无图形用户界面)上的命令行测试网速?

linux - 一个进程如何在 Linux 上拦截另一个进程的 stdout 和 stderr?