Page 1 of 4 123 ... LastLast
Results 1 to 10 of 40

Thread: Beginners Challenge #7

  1. #1
    Join Date
    Jan 2007
    Location
    $here ? $here : $there
    Beans
    3,717
    Distro
    Ubuntu 8.04 Hardy Heron

    Beginners Challenge #7

    Sorry for the long delay...

    Beginners Challenge #7: Write a basic bash-like shell

    The idea is to write a simple shell that behaves more or less like a normal Unix shell.

    Requirements:

    - You must present a prompt and take input from the keyboard
    - You must support running of commands based on the $PATH environment variable
    - You must support the following builtin commands: "cd" and "exit"
    - You must wait for and properly cleanup any commands the shell starts
    - You must inform the user if the input is an invalid command/builtin
    - You must cleanly handle the case where users hit return without typing anything

    Though not required, bonus points will be added for clean implementations of any of the following:

    - Supporting ~/ and ~user
    - Supporting proper Ctrl-C behavior
    - Supporting background processes (extra bonus points for properly handling Ctrl-Z, bg, fg and jobs).
    - Supporting history type functionality
    - Supporting manipulation of environment variables
    - Supporting pipes and redirection
    - Supporting tab completion
    - Supporting an rc file
    - Supporting scripting functionality

    Rules:

    - You may use readline if you wish
    - You may not use any languages/libraries that aren't in the Ubuntu repos

    Judging will happen in approximately 2 weeks. At which time I'll appoint someone to write the next challenge.
    Don't try to make something "fast" until you are able to quantify "slow".

  2. #2
    Join Date
    May 2008
    Beans
    Hidden!

    Re: Beginners Challenge #7

    Ok, simple Python implementation.

    PHP Code:
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-

    import os
    import readline
    import subprocess
    import sys

    def CD
    (args):
        if 
    len(args) > 1:
            
    os.chdir(args[1])
        else:
            try:
                
    os.chdir(os.environ["HOME"])
            
    except KeyError:
                
    # This probably shouldn't happen, but after the last bug... ;)
                
    print "Error: No directory specified and cannot determine $HOME"

    def Exit(args):
        
    sys.exit(int(args[1]) if len(args) > and args[1].isdigit() else 0)

    Builtins = {
        
    "cd"CD,
        
    "exit": Exit
        }

    while 
    True:
        try:
            
    line raw_input("snova$ ")
        
    except EOFError:
            
    sys.exit()
        
    except KeyboardInterrupt:
            continue

        
    args line.split()
        if 
    not args:
            continue

        if 
    args[0in Builtins:
            
    Builtins[args[0]](args)
        else:
            try:
                
    process subprocess.Popen(args)
            
    except OSError as error:
                print 
    error
                
    continue

            try:
                
    process.wait()
            
    except KeyboardInterrupt:
                
    pass 
    It meets all of the requirements and these two extras:

    - Ctrl-C
    - History (to a point; while it is available in the shell it does not persist between sessions)
    Last edited by snova; August 16th, 2009 at 08:30 PM. Reason: Bugfix (typo) | Bugfix (string to int conversion) | Bugfix (CD exception)

  3. #3
    credobyte is offline Iced Blended Vanilla Crème Ubuntu
    Join Date
    Jun 2009
    Beans
    1,559
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Beginners Challenge #7

    Quote Originally Posted by snova View Post
    Ok, simple Python implementation.

    It meets all of the requirements and these two extras:

    - Ctrl-C
    - History (to a point; while it is available in the shell it does not persist between sessions)
    Code:
    snova$ cd
    Traceback (most recent call last):
      File "./Shell.py", line 36, in <module>
        Builtins[args[0]](args)
      File "./Shell.py", line 13, in CD
        os.chdir(args[1])
    IndexError: list index out of range

  4. #4
    Join Date
    Jan 2007
    Location
    $here ? $here : $there
    Beans
    3,717
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Beginners Challenge #7

    Quote Originally Posted by credobyte View Post
    Code:
    snova$ cd
    Traceback (most recent call last):
      File "./Shell.py", line 36, in <module>
        Builtins[args[0]](args)
      File "./Shell.py", line 13, in CD
        os.chdir(args[1])
    IndexError: list index out of range
    I didn't explicitly say that "cd" should mean "cd $HOME" but, it should. The basic requirements seem very simple but, the further you delve into them, the more special cases you have to handle. I'm not looking for a full on bash replacement in 50 lines of code but, Snova's submission is a good start. Although, it's -10,000 bonus points for using python...
    Don't try to make something "fast" until you are able to quantify "slow".

  5. #5
    credobyte is offline Iced Blended Vanilla Crème Ubuntu
    Join Date
    Jun 2009
    Beans
    1,559
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Beginners Challenge #7

    Quote Originally Posted by sdennie View Post
    I didn't explicitly say that "cd" should mean "cd $HOME" but, it should. The basic requirements seem very simple but, the further you delve into them, the more special cases you have to handle. I'm not looking for a full on bash replacement in 50 lines of code but, Snova's submission is a good start. Although, it's -10,000 bonus points for using python...
    I wouldn't say a single word in case of an "invalid syntax" message ( let's say, it would be enough to recognize it as a command ) The main point was, it shouldn't end up as an IndexError.

  6. #6
    Join Date
    Jan 2007
    Location
    $here ? $here : $there
    Beans
    3,717
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Beginners Challenge #7

    Quote Originally Posted by credobyte View Post
    I wouldn't say a single word in case of an "invalid syntax" message ( let's say, it would be enough to recognize it as a command ) The main point was, it shouldn't end up as an IndexError.
    Haha. Fair enough. Bad snova! Bad! Minus 10,010 points!
    Don't try to make something "fast" until you are able to quantify "slow".

  7. #7
    Join Date
    Jun 2007
    Location
    Porirua, New Zealand
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: Beginners Challenge #7

    Quote Originally Posted by credobyte View Post
    The main point was, it shouldn't end up as an IndexError.
    Does snova gain extra points for not sweating it and revising the code to avoid the error?
    Forum DOs and DON'Ts
    Never assume that information you find using a search engine is up-to-date.
    Please use CODE tags.
    A low-volume blog

  8. #8
    Join Date
    Jan 2007
    Location
    $here ? $here : $there
    Beans
    3,717
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Beginners Challenge #7

    Quote Originally Posted by lisati View Post
    Does snova gain extra points for not sweating it and revising the code to avoid the error?
    Yes. snova now stands at:

    - +10 points for getting the general idea down
    - -10,000 points for using python
    - -10 points for not handling "cd"
    - +10 points for handling "cd"

    That puts snova at -9990 points. Which, it should be noted, is the highest score thus far.
    Don't try to make something "fast" until you are able to quantify "slow".

  9. #9
    Join Date
    Aug 2007
    Location
    127.0.0.1
    Beans
    1,800
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Beginners Challenge #7

    Quote Originally Posted by sdennie View Post
    - -10,000 points for using python
    And using assembly would yield +1337 points?

    Y no había leído que eras de Buenos Aires .
    "Just in terms of allocation of time resources, religion is not very efficient. There's a lot more I could be doing on a Sunday morning."
    -Bill Gates

  10. #10
    Join Date
    Jan 2007
    Location
    $here ? $here : $there
    Beans
    3,717
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Beginners Challenge #7

    Quote Originally Posted by Can+~ View Post
    And using assembly would yield +1337 points?
    Only if you simultaneously use the SSE and 387 instructions. For a shell.

    Y no había leído que eras de Buenos Aires .
    I'm not. But, I play a Porteño on T.V.
    Don't try to make something "fast" until you are able to quantify "slow".

Page 1 of 4 123 ... LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •