-1

I have this shell command that I want to call from python

frontend='test'
instance_list = subprocess.call(['sudo gcloud compute instances list | grep -v TERMINA | grep +'frontend'+ | awk '{ print $1 }''])

I'm getting this error

    instance_list = subprocess.call(['sudo gcloud compute instances list | grep -v TERMINA | grep +'frontend'+ | awk '{ print $1 }''])
                                                                                                           ^
SyntaxError: invalid syntax

What I'm doing wrong?

Jack
  • 642
  • 2
  • 7
  • 19
  • Drop the `+` concatenation (old syntax) and use [string formatting](https://www.w3schools.com/python/ref_string_format.asp). – S3DEV Jul 09 '20 at 11:04
  • 1
    Does this answer your question? [Calling an external command from Python](https://stackoverflow.com/questions/89228/calling-an-external-command-from-python) – Roshin Raphel Jul 09 '20 at 11:05

3 Answers3

1

How about this:

frontend='test'
instance_list = subprocess.call(['sudo gcloud compute instances list | grep -v TERMINA | grep '+frontend+' | awk \'{ print $1 }\''])

You just did the string concatenation wrong: the plus needs to be outside of the quotes... And the quotes for awk probably need to be escaped...

ynotzort
  • 156
  • 5
0

You should put the plus signs outside the quotation marks:

instance_list = subprocess.call(['sudo gcloud compute instances list | grep -v TERMINA | grep '+frontend+' | awk '{ print $1 }''])
Baraa
  • 176
  • 7
0

I Think the problem is in the way you arranged your quotations and concatenation sign (+)

Concatenation works like: 'Hello' + variable + 'world'

Or when it is about escaping quotes inside (Which is probably not your case) You can use triple quotes like xxx.call([''' You are free to use single quotes inside here ''']);


For your case, this can help:

instance_list = subprocess.call(['sudo gcloud compute instances list | grep -v TERMINA | grep ' + frontend + ' | awk '{ print $1 }''])