0
import sys
import os
import subprocess
        
app_name = sys.argv[1]
s = subprocess.call(
    "docker ps --filter name=$app_name| awk '{print $2}' > docker_tag.txt",
    shell=True
)

How can I access the value of the variable inside subprocess.call().

1 Answer 1

1

I believe you need f string

Ex:

import sys
import os
import subprocess

app_name=sys.argv[1]
s=subprocess.call(['docker', 'ps', '--filter', f"name={app_name}", '|', 'awk', "'{print", "$2}'", '>', 'docker_tag.txt'], shell=True)
# OR
#s=subprocess.call(f"docker ps --filter name={app_name}| awk '{print $2}' > docker_tag.txt", shell=True)
Sign up to request clarification or add additional context in comments.

3 Comments

getting syntax error when do use above code [centos@exe93-aws-mum:/exedge-volume/exedge]# python test.py upf-fpm File "test.py", line 6 s=subprocess.call(['docker', 'ps', '--filter', f"name={app_name}", '|', 'awk', "'{print", "$2}'", '>', 'docker_tag.txt'], shell=True) ^ SyntaxError: invalid syntax
is your Python version 3.6 and above?
[centos@exe93-aws-mum:~]# python3 --version Python 3.6.8

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.