0

i am writing a shell script test.sh inside which i am defining a json_str variable as

json_str='{"ecommerce": "master","app_compat":"master"}'

and i am passing this variable to a python script command - >

sudo python3 release.py $json_str

inside python script i am printing the value of input it is printing this

{"ecommerce":

not whole string. I can not change the input string as its coming from server which cant be changed. Solution of this is

json_str='{\"\ecommerce": \"\master",\"
\app_compat":\"\master"\}'

Can you suggest another method to do this as i cant change input string.

Inside python script

input_release=sys.argv[1]

print("here input %s" %input_release)

shell script

#!/usr/bin/env bash

echo "Inside bash script"
json_str='{"ecommerce": "master","app_compat":"master"}'

echo "$json_str"
sudo python3 release.py $json_str
1
  • 4
    It looks like a shell quoting issue: try sudo python3 release.py "$json_str" Commented Dec 12, 2017 at 9:37

2 Answers 2

2

It breaks because of the spaces in your JSON. You can pass it as a single argument by quoting it:

sudo python3 release.py "$json_str"

But if you cannot alter the CLI you might try to recreate it within Python:

sudo python3 release.py $json_str

...

import sys

json_data = " ".join(sys.argv[1:])
print("JSON data: ", json_data)
# JSON data: {"ecommerce": "master","app_compat":"master"}

Although beware that you cannot account for all shell expansions this way and CLI is not really intended for passing large JSON-like structures so why don't you just pass it as an environment variable, i.e. in your shell script:

#!/usr/bin/env bash

json_str='{"ecommerce": "master","app_compat":"master"}'

export $json_str
sudo python3 release.py

and in your Python script:

import os

json_data = os.environ["json_str"]
print("JSON data: ", json_data)
# JSON data: {"ecommerce": "master","app_compat":"master"}
Sign up to request clarification or add additional context in comments.

4 Comments

This method removed the double quotes from string and that string is not able to parse in json format . {ecommerce: master,app_compat:master}
@sudhanshusaini - which one?
json_data = " ".join(sys.argv[1:]) . when i am sending a string "{"ecommerce": "master","app_compat":"master"}" as this from server into python script it removes the double quotes and when i am printing the input in python file it prints as like this {ecommerce: master,app_compat:master}
@sudhanshusaini - unfortunately, that's shell-dependent - most shells will use quotes to denote different arguments which may interfere with the data being passed through (this can happen if you quote the argument due to shell expansion order, too). That's why it's much safer to pass the data either as an environment variable or pipe it to the other script's STDIN.
0

The problem is neither with Python nor with Json but with the shell itself. When you feed the shell with the following line:

sudo python3 release.py $json_str

here is what happens in that order:

  • the variable is replaced by its value giving sudo python3 release.py {"ecommerce": "master","app_compat":"master"
  • the line is tokenized in words: 1:sudo 2:python3 3:release.py 4:{"ecommerce": 5:"master","app_compat":"master" because of the space between : and "master"

The common way to avoid that splitting is to enclose the variable to be substituted in quotes. Unfortunately you cannot do it here because the string already contains quotes.

I can only imagine 2 solutions:

  1. ensure that the substitution string contains no unescaped space (one single \ because it occurs inside simple quotes):

    json_str='{"ecommerce":\ "master","app_compat":"master"}'
    
  2. use the standard input to read the string

    Python3: input_release = input() or Python2: input_release = rawinput()

    shell: echo $json_str | sudo python release.py, or with a here document:

    sudo python release.py <<END
    $json_str
    END
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.