Thread: Help creating Mini-shell??

  1. #1
    Unregistered
    Guest

    Help creating Mini-shell??

    I am trying to create a C-language mini UNIX shell that (for right now) enters my own shell and displays a prompt (i.e., READY:$> ), accepts a command from the command line, displays what the command is (The command you entered was: ) and any parameters that were passed along with it (i.e., -ls), and executes the command. I have SOME code, but I highly doubt it is correct. Can ANYONE help me?

  2. #2
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    I think your goal can be accomplished without much fuss.
    Please post what you've written and we can steer you in the right direction.
    Jason Deckard

  3. #3
    Unregistered
    Guest

    my code

    this is what i have so far....

    mars:$ cat shell2.c
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <iostream.h>


    main()
    {
    cout<<"You have entered my 1814 shell"<<endl;
    bool exit=true;
    char *line[10];
    int n=0;
    getline(line[n]);
    while (!(exit))
    {
    line[n]=0;
    cout<<"READY:#";
    cout<<"The command you typed was: " <<line[0];
    execv(line[0],NULL);
    }

    if (gets(line)="exit")
    exit=false;
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > char *line[10];
    You don't allocate any memory

    Start with this for now...
    char line[10][80];

    > bool exit=true;
    exit is a reserved function name, pick something else

    And it also needs to be set to false, if this is supposed to loop
    > while (!(exit))

    > execv(line[0],NULL);
    execv has more than this to it
    In particular, you need to break line[0] into individual words, and pass each word as a separate parameter to execv
    Also, there are several exec* functions, read about each one to decide which is best for you

    > if (gets(line)="exit")
    should be
    if ( strcmp( line[0], "exit" ) == 0 ) {
    }

  5. #5
    Unregistered
    Guest
    Thank you...i appreciate it Salem. I used this code:

    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <iostream.h>
    #include <string>
    #include <iomanip.h>
    main()
    {
    cout<<"You have entered my 1814 shell"<<endl;
    string command;
    char *line[20];

    while (command !="exit")
    {

    cout<<"GIMME:#";
    cin>>command;
    cout<<"The command you typed was: " <<command<<endl;
    if (command == "exit")
    return 0;

    }//end of while

    line[0] = "ls";
    line[1] = "-la"
    line[2] = NULL;

    execve("/bin/ls", line, NULL);


    if (command =="exit")
    return 0;
    }//end of main

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Creating Shell based game..Few questions
    By Saintdog in forum Game Programming
    Replies: 1
    Last Post: 12-01-2004, 08:14 PM
  3. What shell is more powerful?
    By xddxogm3 in forum Tech Board
    Replies: 10
    Last Post: 07-24-2004, 10:47 PM
  4. Creating shell in C
    By LinMach in forum C Programming
    Replies: 8
    Last Post: 02-19-2003, 07:40 PM