简体   繁体   English

从shell脚本返回值到python脚本

[英]Return value from shell script to python script

I am trying to achive following: 我正在尝试实现以下目标:

  1. I am having a python script which is calling a shell script and passing a parameter as well. 我有一个python脚本,它正在调用shell脚本并传递参数。 Shell script creates a tar.gz file using that parameter passed in some location. Shell脚本使用在某个位置传递的参数创建tar.gz文件。

  2. Now shell script should pass the name and location of the tar.gz so created. 现在,shell脚本应传递如此创建的tar.gz的名称和位置。 Python script uses that to form a JSON and pass to some other code. Python脚本使用它来形成JSON并传递给其他代码。

  3. Along with this I want to add some check to make sure if tar.gz is generated then only value is returned to python otherwise not. 与此同时,我想添加一些检查以确保是否生成tar.gz,然后仅将值返回给python,否则不返回。

Here is the code: 这是代码:

Python script: Python脚本:

#!/usr/bin/python

import json
import subprocess

json_data='{"name": "StackOverflow", "uid": "8fa36334-ce51"}'

data = json.loads(json_data)
for keys,values in data.items():
  print(keys)
  print(values)

 UID = data.get('uid')

 rc = subprocess.check_output(["/home/cyc/Cyc-    
        Repo/cyc_core/cyc_platform/src/package/cyc_bsc/scripts/test.sh",    
        UID])
 print rc
 if rc != 0:
   print "failed for passed in uid"

data_op = {}
data_op['pathTOCompressfile'] = 'value_should_be_return_from_shell'
data_op['status'] = 'OK'
json_data_op = json.dumps(data_op)

print json_data_op

shell script: 外壳脚本:

#!/bin/bash

if [ "$1" != "" ]; then
   uid=$1
   echo "Positional parameter 1 contains something $1"
else
   echo "Positional parameter 1 is empty"
fi

LOG_TMP="tmp_log_file_location"

log_location="log_file_location"

filename="${log_location}/$uid.tar.gz"
echo $filename
tar -zcvf $filename -C $LOG_TMP/dc .

This is what i am not able to understand: 这是我无法理解的:

  1. How to pass back the value of variable "filename" back to python script if tar -zcvf command is successful. 如果tar -zcvf命令成功,如何将变量“文件名”的值传递回python脚本。
  2. In python script how can i verify take value of filename and create JSON using that 在python脚本中,如何验证文件名的值并使用该值创建JSON
  3. In case value cannot be generated STATUS becomes fail in JSON ( within python ) so capture that as well. 如果无法生成值,则STATUS在JSON中(在python中)会失败,因此也请捕获该值。

Your shell script writes the name of the generated file to standard output, so your question boils down to how to catch stdandard output of a subprocess started from Python. 您的shell脚本将生成的文件的名称写入标准输出,因此您的问题归结为如何捕获从Python启动的子进程的标准输出。 This has been ansered here . 这已经在这里了 BTW, when asking questions about Python, it would be a good idea to always specify the Python version you are using. 顺便说一句,当问有关Python的问题时,最好始终指定您使用的Python版本。

In your case however, I would redesign your shell script a bit: 但是,就您的情况而言,我将重新设计您的shell脚本:

Your script outputs not only the generated filename, but also messages about the "positional parameter", and this means that you would have to fiddle them apart in your script, whether it is an message or a valid output. 您的脚本不仅输出生成的文件名,还输出有关“位置参数”的消息,这意味着无论是消息还是有效输出,您都必须在脚本中摆弄它们。 You could send the messages to standard error, to keep them apart. 您可以将消息发送到标准错误,以使其分开。

BTW, if there is no positional parameter, the generated file name is just .tar.gz . 顺便说一句,如果没有位置参数,则生成的文件名为.tar.gz Is this really what you want to have? 这真的是您想要的吗? Wouldn't it better to do a exit 1 , if there is no parameter? 如果没有参数,执行exit 1会更好吗?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM