-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.
11.2k11 gold badges22 silver badges46 bronze badges
asked Oct 25, 2022 at 21:35
3
  • 3
    Please do not upload images of code/data/errors when asking a question. Commented Oct 25, 2022 at 21:41
  • You don't need curl for something this simple; just use something like urllib.request or the third-party requests library. Commented Oct 25, 2022 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'). Commented Oct 25, 2022 at 21:56

1 Answer 1

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''')
 --------------
answered Oct 25, 2022 at 21:49
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.