shell ?? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

shell ??

generalengeneralen Posts: 68Member
HI!!

I wonder if some of you in here know how to build a simple command shell i Perl? It has to be very simple and are just going to list the directories in the library etc..

thanks..

Comments

  • JonathanJonathan Posts: 2,914Member
    : I wonder if some of you in here know how to build a simple command shell
    : i Perl? It has to be very simple and are just going to list the
    : directories in the library etc..
    :
    A command shell basically:-
    1) Prints a prompt.
    2) Read a command in.
    3) Parses it.
    4) Acts upon it.
    5) If the command wasn't to exit, go back to 1.

    So, just do a big wihle loop...

    [code]my $exit = 0;
    while (!$exit) {
    # Print a prompt.
    print '$ ';

    # Wait for the user to enter a command, then read it.
    my $command = <>;
    chomp $command;

    # Action handlers.
    # ################

    # Exit.
    if ($command =~ /^s* exit s*$/x) {
    $exit = 1;

    # List files.
    } elsif ($command =~ /^s* ls s*$/) {
    opendir(DIR, ".");
    my $entry;
    print "$entry
    " while $entry = readdir(DIR);
    closedir(DIR);

    # Unknown command.
    } else {
    print "Erm...what does that mean?
    ";
    }
    }[/code]

    You may also need to turn on auto-flush. If the prompt isn't printing properly, try doing this at the top of your script:-

    $| = 1;

    Code untested, sorry if I've made any slips.

    Hope this helps,

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

Sign In or Register to comment.