PYTHON Script not using OS Commands

52 Views Asked by At

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')
1

There are 1 best solutions below

0
Amadan On

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''')
             --------------