简体   繁体   English

从Bash调用的Python脚本无能为力

[英]Python Script Called from Bash does Nothing

I have a weird problem. 我有一个奇怪的问题。 I am calling a python script within a shell script like this: 我在这样的shell脚本中调用python脚本:

---filename: dl.sh---
#/bin/bash
...
metaData=`python /var/www/music/getMetaData.py "$artist" "$title" | tail -n 1`
...

It is a python script that reads metadata from a json resource. 这是一个python脚本,可从json资源读取元数据。 In most cases it is working as desired, filling the variable metaData. 在大多数情况下,它会按需运行,填充变量metaData。

For some cases it simply returns nothing. 在某些情况下,它什么也不返回。 Even print commands from the python scripts are not being called. 甚至没有调用来自python脚本的打印命令。 It seems to me that the python script simply does not get executed, because when I run the command manually it always works: 在我看来,python脚本根本无法执行,因为当我手动运行命令时,它始终有效:

python /var/www/music/getMetaData.py "Artist Name" "Title Name" | tail -n 1

In the cases the script return nothing manually executing 在这种情况下,脚本不返回任何手动执行的内容

echo $metaData

results in a empty line. 结果为空行。

The first lines of the python script are: python脚本的第一行是:

---filename: getMetaData.py---
import urllib2
import sys
import re
import datetime

print "Testing Output ..."

Not even "Testing Output ..." gets printed in these cases... 在这种情况下,甚至都不会打印“ Testing Output ...”。

Try to change tail to head -1 and see if it starts working. 尝试将tail更改为head -1 ,看看它是否开始起作用。 If the last line of the script output is a blank then that is what your overall command (including tail ) would return. 如果脚本输出的最后一行为空白,则将返回您的总体命令(包括tail )。

The problem was in the passed args... Somehow spaces screw up the call to the python script. 问题出在传递的参数中...不知何故,空格使对python脚本的调用搞砸了。

Before: 之前:

metaData=`python /var/www/music/getMetaData.py "$artist" "$title" | tail -n 1`

Now: 现在:

artist=${artist//[ ]/;}
title=${title//[ ]/;}
metaData=`python /var/www/music/getMetaData.py "$artist" "$title" | tail -n 1`

With replacing the spaces with + it works now like a charm! 用+替换空格后,它现在就像一个魅力! Thx everybody! 谢谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当使用 eval() 从 php 脚本调用脚本以及从 bash 脚本调用相同的函数时,python 如何使用路径? - How does python use paths when a script is called using eval() from a php script and when the same function is called from a bash script? bash脚本在调用表单python脚本时不读取行 - bash script does not read lines when called form python script Bash 脚本可以从命令行调用,但不能从 Python 脚本调用 - Bash script can be called from command line, but not from Python script Python脚本调用从bash脚本执行而不处理信号 - Python Script called executed from bash script not handling signals 从bash脚本调用时获取Python命令行输出 - Get Python command line output when called from a bash script 从php或python调用时,Bash脚本会挂起 - Bash script hangs when called from php or python 在从 crontab 调用的 python 脚本中执行 bash 命令 - Executing bash command within a python script called from crontab 获取调用python脚本的bash脚本的位置 - Get location of bash script that called python script 当从 python 调用然后从 bash 调用时,脚本具有不同的 output - Script has different output when called from python then when called from bash 当从python脚本调用时,为什么我的bash脚本不能从文件中读取行? - Why doesn't my bash script read lines from a file when called from a python script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM