Here is a c Program I have tried to create a custom shell in linux with limited commands
#include
#include
#include
#include
#include
using namespace std;
int main(int argc, char * argv[])
{
string command;
cout << "\t This is a custom developed shell \n\n";
while (command != "myexit")
{
if ( command == "myexit")
system("exit");
else if ( command == "myfinger" )
{
char username[40];
char buf[32];
printf ("Enter the username:\n");
scanf ("%s",&username);
sprintf(buf, "finger %s", username);
system (buf);
}
else if ( command == "mydel" )
{
char filename[40];
char cmd1[40];
printf ("Enter the filename to delete:\n");
scanf ("%s",&filename);
sprintf(cmd1, "rm -rf %s", filename);
system (cmd1);
printf ("The file %s deleted \n", filename);
}
else if ( command == "mywhich" )
{
char cmmnd[40];
char cmd2[40];
printf ("Enter the command:\n");
scanf ("%s",&cmmnd);
sprintf(cmd2, "which %s", cmmnd);
system (cmd2);
}
else if ( command == "mycp" )
{
char target1[30];
char target2[30];
char cmd3[30];
printf ("Enter the source and then destination:\n");
scanf ("%s %s", &target1,&target2);
sprintf(cmd3, "cp %s %s", target1,target2);
system (cmd3);
}
else if ( command == "myhelp" )
cout << "\n Available options \n 1)mydel \n 2)myfinger \n 3)mywhich \n 4)mycp \n 5)myhelp \n \n";
else if ( command != "\0")
cout << "command not found \n";
cout << "myprompt >";
cin >> command;
}
return 0;
}
save this to a file... g++ filename.cpp -o filename
#include
#include
#include
#include
#include
using namespace std;
int main(int argc, char * argv[])
{
string command;
cout << "\t This is a custom developed shell \n\n";
while (command != "myexit")
{
if ( command == "myexit")
system("exit");
else if ( command == "myfinger" )
{
char username[40];
char buf[32];
printf ("Enter the username:\n");
scanf ("%s",&username);
sprintf(buf, "finger %s", username);
system (buf);
}
else if ( command == "mydel" )
{
char filename[40];
char cmd1[40];
printf ("Enter the filename to delete:\n");
scanf ("%s",&filename);
sprintf(cmd1, "rm -rf %s", filename);
system (cmd1);
printf ("The file %s deleted \n", filename);
}
else if ( command == "mywhich" )
{
char cmmnd[40];
char cmd2[40];
printf ("Enter the command:\n");
scanf ("%s",&cmmnd);
sprintf(cmd2, "which %s", cmmnd);
system (cmd2);
}
else if ( command == "mycp" )
{
char target1[30];
char target2[30];
char cmd3[30];
printf ("Enter the source and then destination:\n");
scanf ("%s %s", &target1,&target2);
sprintf(cmd3, "cp %s %s", target1,target2);
system (cmd3);
}
else if ( command == "myhelp" )
cout << "\n Available options \n 1)mydel \n 2)myfinger \n 3)mywhich \n 4)mycp \n 5)myhelp \n \n";
else if ( command != "\0")
cout << "command not found \n";
cout << "myprompt >";
cin >> command;
}
return 0;
}
save this to a file... g++ filename.cpp -o filename
Comments
Post a Comment