-2

所以我正在用 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()
4

2 回答 2

1

你忘了在 elif command == 'man-dos' 之后加上 ':'

只需运行:python3 yourscript 即可进行调试。

于 2013-01-25T05:49:45.670 回答
1

您当前的代码中有几个错误:

  1. 首先,您有一些混乱的缩进,包括用于缩进的制表符和空格的混合。这在 Python 中非常糟糕,因为不正确的缩进是语法错误。我已修复问题中的代码以正确显示(每个选项卡使用 8 个空格),但您也需要在文件中修复它。另外,请注意 Python 约定是每个块缩进四个空格,因此您可能希望将编辑器设置为默认执行此操作。

  2. 其次,您的顶级检查的逻辑不正确。您正在测试整个命令字符串是否包含在commandAvail变量中的命令字符串中。如果命令除了命令名之外还有参数,这将做错事。我建议您split使用命令字符串来获取术语列表,然后仅针对可用命令测试第一个术语。我建议不要进行子字符串搜索,而是拆分commandAvail为一组字符串:

    # make a set of commands that we recognize
    commandAvail = set('ls where chdir mk mkdir cp'.split()) # trimmed for space
    
    # then later, test against it
    command = input().split()
    if command[0] in commandAvail: # this is much more efficient!
        # do stuff
    
  3. 最后,您尝试从命令字符串中删除命令本身的方式是错误的。你不能从另一个字符串中减去一个字符串(TypeError如果你尝试,你会得到一个)。幸运的是,我为上一期建议的解决方案在这里会有所帮助。由于split调用,您将获得一个术语列表,而不是进行字符串操作。现在,您只需针对第一项(命令)进行测试,并将其余的作为参数传递。

    实际上,这表明实现比您当前拥有的if/块的大链要简单得多。elif使用字典在命令字符串和实现它们的函数之间进行映射:

    # map command names to the functions that implement them
    command_dict = {"ls":ls, "where":where, "chdir":chdir} # snipped
    
    # the main loop
    while True:
        # command is first term, the rest go into args
        command, *args = input().split()
    
        try: # use "easier to ask forgiveness than permission" idiom
            command_dict[command](*args) # call function from command_dict
        except KeyError:
            print("Unknown command: {}".format(command))
    
于 2013-01-25T05:50:00.563 回答