0

I'm trying to understand some basic shell scripting. I have a script.sh, did the chmod, and was messing around with some pretty easy print statements by executing ./script.sh

Now how could I launch the shell displaying a prompt that includes the current working directory, and said prompt should accept a line of input and display a prompt each time?

To sum up the tools I understand so far: os.getcwd(), sys.stdin.readlines(), subprocess.Popen(['ls'], stdout=subproccess.PIPE)

Here is what I have so far.

#!/usr/bin/env python
import os
import sys
import subprocess

proc = subprocess.Popen(['ls'], stdout=subprocess.PIPE)
cwd = os.getcwd()
while True:
user_input = raw_input(str(cwd) + " >> ")
   if user_input == 'ls':
      print proc
   if not foo:
      sys.exit()

So this seems to work. At least the command prompt part, not exiting.

3
  • 3
    Are you using Python to execute a shell script (sh, bash, etc.)? Are you trying to prompt the user from Python or from the shell script? Is this a question asking for Python code or shell script (sh, bash, etc.) code? Commented Dec 2, 2016 at 2:06
  • I'm confused as well and lazy to answer all the possible options hehe :-) Commented Dec 2, 2016 at 2:10
  • I am trying to create a python shell and trying to prompt the user from the script. The user should be able to type a command such as ls with no arguments. The shell will launch it, block until it completes, and then resume. If the command exits with a non-zero exit status code, then display a message to the user. Commented Dec 2, 2016 at 2:14

1 Answer 1

1

If you want to prompt the user, then you probably don't want to be using sys.stdin.readlines() as there isn't really an easy way to put your prompt in after each line. Instead, use input() (or raw_input() on Python 2).

user_input = input("My prompt text> ")

Then the user's input will be stored in a string in user_input. Put that in a while loop, and you can have it repeatedly display, like a regular command prompt.

Sign up to request clarification or add additional context in comments.

2 Comments

basically, while user_input:? I think this helps a lot
You can do that if you want. You'll just have to be sure to set user_input before the loop starts.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.