[英]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.