UNIX shell programming
I'm trying to write a problem in C which can display a prompt,e.g My_shell$ and accept a command entered by the user by using fgets. Also, it needs to execute the command by invoking fork() and execvp() system call. Then, if user type "logout", the shell should exit.
My problem is that I don't know how to combine fgets, fork() and execvp() together inside the program. Can anyone please show me some examples or suggestions? Thank you |
In a while loop
{ First accept the input using fgets ..... Use strtok to split the fgets into arguments for use in execvp() Check if command is "logout" and exit from the program . Then fork to create a child . In the child process , use exec to run the command requested by the user In the parent , wait until the child has exited n then print "Command successful or something " } I suppose this should do it for you unless u want to include background , redirection and other stuff.... If you still aren't able to get it I'll post the program. Welcome to LQ. |
hey, i wrote this program just a few weeks ago for my OS course. it is online at http://gdit.iiit.net/~vamsee_k/myshell.c
hope it helps you out. it supports redirection but doesn't run a process in the background( the `&' thing ). Hope it helps you out... |
Quote:
I've already startd a little bit. But I did differently from what you wrote. In my program , I used functions and still don't know how to implement fgets() in the UNIX environment. Can you take a look at it and give me some suggestions? Thank you Code:
#include <stdio.h> To vanquisher: I found little bit hard to follow your program. Can you explain your program to me please? Also, unforunately, I need to run a process in the background (the '&' thing). Can you tell me how to implement into my above code. Thank you |
You are going on the right track .
If ever you need help with this , post a message ..... I'll be there to help you. P.S :- Do you need help with strtok ?? |
Quote:
thanks again |
sorry..I clicked it wrong.
Quote:
|
Yeah you can use printf to print the prompt .....
After that you can use either "scanf or fgets" The problem with scanf is that the default delimiter is " " and so for an input of "ls -l -a --color" only "ls" will be stored in the var So u need to use scanf("%[^\n]",string); Here we have are going to accept characters until a "\n" is encountered Check man scanf for more details In case of fgets the syntax is as follows fgets(buf,MAX_SIZE,stdin); where buf is a character array , MAX_SIZE is the maximum no. of characters you want to accept , stdin for reading from the stdin You can use either of these for reading for your shell program.... |
Hello
I am new to shell programmin and need to learn how to implement a simple unix shell. Any help(code sinppets,learning material etc) would be greatly appreciated. thanks |
hai,
I need myshell.c program, anybody please help me for this |
Hey guys,
Just so you know I am just about to start working on a very similar if not the same project. I have done alot of research and looking on forums such as this one, and I see alot of the people creating a shell using C are students. I am no different :P This is a project that I have chosen to take up. So I have read alot of the questions and answers on this subject and it all seems very helpful. So im already thankful to all the people that have helped me with out even knowing. I have alot of idea's on how i will implement this, and I will work on this throughout the next month and keep everyone updated with anything I learn... or any problems I have :) |
Hi guys,
Well here is my first update of code. Anyone can use it, and i would be very thankful for any experts to look at my code and suggests improvments. Of course its not finished yet, but it does have the basic functionality of: ls with args mkdir rmdir most basic shell commands except cd Code:
#include <stdlib.h> |
The first problem I had was actually getting commands that were input to run. But that was fixed however i think it might be a bit dodgy? What i actually did was the array of arguements for execvp() did not include the command as the first entry of the array. Once the first entry of this array was set to the command, and the arguments were set to the following array entries, the execute function seemed to work fine.
So the next step in my shell is to allow directory walking with the chdir() command... which i am having trouble with and researching. And also to add redirection of input and output. If anyone has any tips on either of these, that would be great! :P And this might be getting ahead of myself, but does anyone know much about changing the colour of the text output? Or perhaps making the output bold? |
Object not found
Quote:
The link is not working i suppose ... :scratch: Are you sure it was the correct link? |
yeah, i also tried to look at that and the link seemed broken.
So now i have fixed my code so the ability to walk thur directories is now complete. Now my shell has most basic functions including exit and cd and generally acts as a shell (Even a function to change the shell prompt!!! hehe). From there I am going to add in re-direction... then see where i get from there. is it strange that i am enjoying this project? (no im not a nerd! :P) |
All times are GMT -5. The time now is 06:01 PM. |