/bin/sh:第62行:至:找不到命令 [英] /bin/sh: line 62: to: command not found

查看:272
本文介绍了/bin/sh:第62行:至:找不到命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Python代码,在其中我调用一个shell命令.我执行shell命令的代码部分是:

I have a python code in which I am calling a shell command. The part of the code where I did the shell command is:

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条目,并且失败并出现错误:

Now, when i run this code on a local file with some sample input (I did cat /home/sree/example.json | python parse.py) it works fine and I get the required output. But I am trying to run the code with an input on my HDFS (the same cat command but input file path is from HDFS) which contains exactly the same type of json entries and it fails with an error:

/bin/sh: line 62: to: command not found
list index out of range

我在Stack Overflow上读过类似的问题,解决方案是在被调用的Shell脚本中加入Shebang行.我在demo.sh脚本中确实有shebang行#!/usr/bin/bash.

I read similar questions on Stack Overflow and the solution was to include a Shebang line for the shell script that is being called. I do have the shebang line #!/usr/bin/bash in demo.sh script.

此外,which bash给出/usr/bin/bash.

请详细说明.

推荐答案

您很少(如果有的话)想要结合使用传递列表参数和shell=True.只需传递字符串即可:

You rarely, if ever, want to combine passing a list argument with shell=True. Just pass the string:

synnet_output = subprocess.check_output("echo '%s' | syntaxnet/demo.sh 2>/dev/null"%(text,), shell=True)

但是,这里您实际上并不需要shell管道.

However, you don't really need a shell pipeline here.

from subprocess import check_output
from StringIO import StringIO  # from io import StringIO in Python 3
synnet_output = check_output(["syntaxnet/demo.sh"],
                             stdin=StringIO(text),
                             stderr=os.devnull)

这篇关于/bin/sh:第62行:至:找不到命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆