1

I am working on making my own shell but what I want it to do what the user input.

For Example:

while 1:
    c = input()
    do c

By do c I want it to then do the command that the person put in.

Any Ideas,

Thanks In Advance!

1
  • 1
    What type of command are you talking about, python commands or shell commands? Commented Jun 8, 2013 at 9:51

2 Answers 2

1

Assuming you are using Python 3, on Python 2 you would use raw_input instead

while 1:
    c = input()
    exec(c)

note that you can't trust that people won't enter malicious code here

You may also want to wrap this in a try/except to print the traceback when an Exception occurs and continue the loop:

import traceback
while 1:
    try: 
        c = input()
        exec(c)
    except:
        print(traceback.format_exc())
Sign up to request clarification or add additional context in comments.

Comments

0

You can use the following:

system

system() from os moduule executes a command supplied as an argument and returns the return code.

import os
cmd=raw_input()
os.system(cmd)    

popen

popen is used to create a subprocess.The advantage is that popen can be used to read the output of a command executed.

import os
cmd=raw_input()
l=os.popen(cmd)
print l.read()

1 Comment

I don't believe OP wants a shell command, rather OP wants Python code command

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.