Home > database >  how to use python variables in bash script inside python script
how to use python variables in bash script inside python script

Time:05-04

I am trying to use my python variables inside the bash script in the python script as below...

import os
import submodule
URL="http://wmqa.blob.core.windows.net..."
os.system(subprocess.call("curl -I --silent GET ",str(URL), "| awk '/x-ms-copy-status/ {print}'"))

# I also tried with 

os.system(subprocess.call("curl -I --silent GET " URL  "| awk '/x-ms-copy-status/ {print}'"))

how can I able to pass the URL after GET in curl also I need to execute some extra commands after the curl to fetch the status

CodePudding user response:

For starters, it is probably best not to use os.system. It was replaced a long time ago (19 years ago, actually; in PEP 324). To answer your question, you can reference a variable using f-strings, or any other formatting allowed in strings in python. Here is an example.

import subprocess

my_var = "hello from my shell"
subprocess.Popen(f"echo {my_var}", shell=True).wait()

Which, when run, outputs:

➜ ./main.py 
hello from my shell

And here is an example that may be closer to your configuration:

import subprocess

url = "http://echo.jsontest.com/var"
my_var = "hello"
subprocess.Popen(f"curl -s {url}/{my_var} | jq .var", shell=True).wait()

output:

➜ ./main.py
"hello"
  • Related