0

How do I pass random generated number in a python script eg

state = random.randint(3, 9)

os.system('bash script.sh $(state) ') #doesn't compile

to a shell script eg.

echo '{"Name": "Boom", "State": '$1'}'
8
  • What does "doesn't compile" mean? Is there some error message? Commented Oct 10, 2022 at 17:03
  • 1
    sh: state: command not found {"Name": "Boom", "State": } That's the output Commented Oct 10, 2022 at 17:05
  • 1
    subprocess.run(['bash', 'script.sh', str(state)]) Commented Oct 10, 2022 at 17:05
  • ...but don't do that; bash script.sh is itself an antipattern. Scripts should choose their own interpreters by having a shebang line, and bash scripts shouldn't have sh extensions (bash and sh are two different interpreters); ideally they should have no extension at all. See also Commandname extensions considered harmful. Commented Oct 10, 2022 at 17:06
  • (also, if you're going to make a shell script write JSON, use a format-aware tool like jq to do it; think about what happens now if someone runs yourscript '39, "foobar": "baz"' -- they can inject arbitrary data into your document) Commented Oct 10, 2022 at 17:09

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.