shell ?? - Programmers Heaven

Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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.