-1

Within a UBUNTU VM, using GNS3 I created code that is an attempt to after the user's input perform one of 3 different outcomes, however, the if statements don't work, the python files can't be found which I was trying to point to this the cd/home.. command. And the curl commands are apparently the incorrect syntax even though that is what I would enter for them to work. please help me out and get this working.

enter image description here

This is what I tried:

#!/usr/bin/python3

import os
import subprocess


Code = input("Enter RYU, ONOS or CURL:")
print("Command entered was: " + Code)

if input == 'RYU':
    os.system('rest_router.py')
    os.system('gui_topology.py')

elif input == "ONOS":
    os.system('sudo /opt/onos/bin/onos-service start')
Michael M.
  • 10,486
  • 9
  • 18
  • 34
  • 3
    [Please do not upload images of code/data/errors when asking a question.](//meta.stackoverflow.com/q/285551) – Michael M. Oct 25 '22 at 21:41
  • You don't need `curl` for something this simple; just use something like `urllib.request` or the third-party `requests` library. – chepner Oct 25 '22 at 21:51
  • It is also better to import Python code (`import rest_router` then use its code) than spawn a whole new Python interpreter with `os.system('rest_router.py')`. – Amadan Oct 25 '22 at 21:56

1 Answers1

0

You are using single quotes to quote something that already has single quotes. By doing so, what should be an opening quote in your curl command is now effectively a closing quote in your Python, and Python doesn't understand why there is now a random ( there where Python code should continue.

I underlined what is quoted in the following examples. Note that even syntax highlighting in most any editor (and also here on Stack Overflow) is helping you see what is inside a string and what is not, colouring them differently (though syntax highlighting can be fallible):

echo 'foo' bar
      ---

But:

os.system('echo 'foo' bar')
           -----     ----

To fix, you can escape the inner quotes, so Python treats them as any regular character inside the string:

os.system('echo \'foo\' bar')
           ----------------

Or you can change the outer quotes. Python has several sets of quotes; you can't use ' or " since you're already using both of them inside the string, but you can use ''' or """:

os.system('''echo 'foo' bar''')
             --------------
Amadan
  • 191,408
  • 23
  • 240
  • 301