【发布时间】:2017-01-29 20:40:35
【问题描述】:
我有一个 python 代码,我在其中调用一个 shell 命令。我执行shell命令的代码部分是:
try:
def parse(text_list):
text = '\n'.join(text_list)
cwd = os.getcwd()
os.chdir("/var/www/html/alenza/hdfs/user/alenza/sree_account/sree_project/src/core/data_analysis/syntaxnet/models/syntaxnet")
synnet_output = subprocess.check_output(["echo '%s' | syntaxnet/demo.sh 2>/dev/null"%text], shell = True)
os.chdir(cwd)
return synnet_output
except Exception as e:
sys.stdout.write(str(e))
现在,当我使用一些示例输入(我做了cat /home/sree/example.json | python parse.py)在本地文件上运行此代码时,它工作正常,我得到了所需的输出。但我试图在我的 HDFS 上输入一个输入来运行代码(相同的 cat 命令,但输入文件路径来自 HDFS),其中包含完全相同类型的 json 条目,它失败并出现错误:
/bin/sh: line 62: to: command not found
list index out of range
我在 Stack Overflow 上阅读了类似的问题,解决方案是为正在调用的 shell 脚本包含一个 Shebang 行。我在demo.sh 脚本中有shebang 行#!/usr/bin/bash。
另外,which bash 提供/usr/bin/bash。
请有人详细说明。
【问题讨论】:
-
看起来您的问题不包括您实际需要帮助的代码。看看你能不能把它减少到minimal reproducible example。
标签: python json shell hadoop subprocess