所以我正在用 Python 写一个 shell,文件名是jsh.py
. 当我运行代码时,由于某种原因它不起作用。弹出一个窗口,但立即关闭。我的代码有什么问题?
import os
import random
import time
import path
import string
import sys
commandAvail = 'ls where chdir mk mkdir cp mv rm rmdir view ext shutdown edit unix dos man-all man-unix man-dos'
commandAvailUnix = 'exec permission group'
commandAvailDos = ' '
print('Welcome to jsh alpha 1!')
commandPlatform = input('System: (UNIX/DOS):')
commandLine()
def commandLine():
while 1 == 1:
command = input('$ ')
if command in commandAvail:
if command.startswith('ls'):
commandKeyword = 'ls'
commandOptions = command - commandKeyword
ls(commandOptions)
elif command.startswith('where'):
commandKeyword = 'where'
commandOptions = command - commandKeyword
where()
elif command.startswith('chdir'):
commandKeyword = 'chdir'
commandOptions = command - commandKeyword
chdir()
elif command.startswith('mk'):
commandKeyword = 'mk'
commandOptions = command - commandKeyword
mk()
elif command.startswith('mkdir'):
commandKeyword = 'mkdir'
commandOptions = command - commandKeyword
mkdir()
elif command.startswith('cp'):
commandKeyword = 'cp'
commandOptions = command - commandKeyword
cp()
elif command.startswith('mv'):
commandKeyword = 'mv'
commandOptions = command - commandKeyword
mv()
elif command.startswith('rm'):
commandKeyword = 'rm'
commandOptions = command - commandKeyword
rm()
elif command.startswith('rmdir'):
commandKeyword = 'rmdir'
commandOptions = command - commandKeyword
rm()
elif command.startswith('view'):
commandKeyword = 'view'
commandOptions = command - commandKeyword
rm()
elif command.startswith('edit'):
commandKeyword = 'edit'
commandOptions = command - commandKeyword
edit()
elif command == 'man-all':
print('Commands that work for all underlying platforms:')
print(commandAvail)
elif command == 'man-unix':
print('Commands that only work on Unix systems:')
print(commandAvailUnix)
elif command == 'man-dos'
print('Commands that only work on DOS systems:')
print(commandAvailDos)
elif command.startswith('unix'):
commandType = 'unix'
unix()
elif command.startswith('dos'):
commandType = 'dos'
dos()
elif command.startswith('ext'):
commandKeyword = 'ext'
commandOptions = command - commandKeyword
ext()
elif command == 'shutdown':
sys.quit(0)
else:
print('jsh has experienced an internal error and has to shutdown.')
sys.quit(10)
else:
print('Command \'' + command + '\' not recognized as internal or external.')
def ls(options):
if commandPlatform == 'UNIX':
os.system('ls' + options)
else:
os.system('dir' + options)
commandLine()