PDA

View Full Version : Help creating Mini-shell??



Unregistered
02-04-2002, 12:06 AM
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?

Deckard
02-04-2002, 07:08 AM
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.

Unregistered
02-04-2002, 09:06 AM
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;
}

Salem
02-04-2002, 03:43 PM
> 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 ) {
}

Unregistered
02-04-2002, 11:44 PM
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