Reputation: 7
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.
Upvotes: 0
Views: 217
Reputation: 1867
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.
Upvotes: 1