Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何修复';在bash中,在检查if condition else语句是否也被执行后';?_Python_Bash_Io_Automation - Fatal编程技术网

Python 如何修复';在bash中,在检查if condition else语句是否也被执行后';?

Python 如何修复';在bash中,在检查if condition else语句是否也被执行后';?,python,bash,io,automation,Python,Bash,Io,Automation,我从一个文件中获取URL,让他们使用curl来下载图像,在URLURL=${URL%$'\r'}中进行更改后,我运行循环来读取每一行,在变量中获取输入并通过TensorFlow对图像进行分类,如果它们是信息图形,那么它应该执行if语句,否则它应该执行else语句。 在执行bash脚本时,将同时执行if和else语句 在执行else语句时,在打印echo${var%$'something'}时,它不会打印任何内容。。。 此外,当从键盘获取输入时,脚本运行正常 #!/bin/bash while I

我从一个文件中获取URL,让他们使用curl来下载图像,在URL
URL=${URL%$'\r'}
中进行更改后,我运行循环来读取每一行,在变量中获取输入并通过TensorFlow对图像进行分类,如果它们是信息图形,那么它应该执行if语句,否则它应该执行else语句。 在执行bash脚本时,将同时执行if和else语句

在执行else语句时,在打印
echo${var%$'something'}
时,它不会打印任何内容。。。 此外,当从键盘获取输入时,脚本运行正常

#!/bin/bash
while IFS= read -r file
do
  url=${file%$'\r'}
  var=`python test_python_classify.py $url`
  if [ $var == 1 ]
  then
    echo $var
    curl -o image.png $url
    python description1.py $url
  else
    echo "\n\n\n"
    echo ${var%$'yoyo'}
    echo "lol"
  fi
done < url.txt
#/bin/bash
而IFS=read-r文件
做
url=${file%$'\r'}
var=`python test\u python\u classify.py$url`
如果[$var==1]
然后
echo$var
curl-o image.png$url
python描述1.py$url
其他的
回显“\n\n\n”
echo${var%$'yoyo'}
回声“lol”
fi
完成
编辑:循环执行两次。是因为改变了字符串还是什么引起的,请帮忙

错误:

Traceback (most recent call last):
  File "test_python_classify.py", line 3, in <module>
    URL = sys.argv[1]
IndexError: list index out of range
./pipeline1.sh: line 8: [: ==: unary operator expected
回溯(最近一次呼叫最后一次):
文件“test_python_classify.py”,第3行,在
URL=sys.argv[1]
索引器:列表索引超出范围
./pipeline1.sh:第8行:[:==:应为一元运算符

有几个错误

首先,
$url
为空(脚本中可能为空行),这使得python在尝试访问参数时失败。这就是此错误的含义:

URL = sys.argv[1]
IndexError: list index out of range

然后在脚本中混合返回代码和返回值:

var=`python test_python_classify.py $url`
if [ $var == 1 ]
  then
python脚本以1返回代码退出,它不打印1。事实上,您的脚本不打印任何内容(转到
stderr
),因此
$var
为空,您会得到一个shell语法错误,因为您没有用引号保护变量

./pipeline1.sh: line 8: [: ==: unary operator expected
如果需要测试返回代码,请使用
$?
同时过滤空URL(我的bash已经生锈,但应该可以正常工作):

如果python脚本打印了一个值,首先测试返回代码以查看它是否成功,然后检查打印的值

if [ ! -z "$url" ]
then
   var = $(python test_python_classify.py $url)
   # check if returncode is 0, else there was an error
   if [ $? == 0 ]
   then
      # protecting return with quotes doesn't hurt
      if [ "$var" == 1 ]
      then
正如在评论中所建议的,这可能需要一次大型的完整python重写,这将简化所有那些bash/python接口问题


这听起来像是完整python的工作&删除bash。你怎么能这么说呢?我的意思是:你已经在脚本中使用python了,它只执行循环、文件读取和打印。到处使用python可能会解决你的问题。bash充满陷阱。python不是(当你出错时,会出现干净的异常)。这是一个完全放弃bash而转而使用python的人的建议。我们可能需要查看您的python脚本,以了解它们打印的内容和返回的内容。您在脚本中混合了返回代码和返回输出。谢谢,但我在url文件中错误地添加了一些新行,这导致了错误,谢谢您的时间,这个帖子这太神奇了
if [ ! -z "$url" ]
then
   var = $(python test_python_classify.py $url)
   # check if returncode is 0, else there was an error
   if [ $? == 0 ]
   then
      # protecting return with quotes doesn't hurt
      if [ "$var" == 1 ]
      then
import sys,subprocess  # we could use python script functions too
with open("url.txt") as f:
  for line in f:
     url = line.rstrip()
     if url:
         output = subprocess.check_output([sys.executable,"test_python_classify.py",url])
         output = output.decode().strip()  # decode & get rid of linefeeds
         if output == "1":
            print("okay")
            subprocess.check_call(["curl","-o","image.png",url])
            subprocess.check_call([sys.executable,"description1.py",url])
         else:
            print("failed: {}: {}".format(url,output))