How do we create a shell?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How do we create a shell?
# 1  
Old 09-23-2007
How do we create a shell?

Alright. I hate to be "that guy" but i seriously need some guidance with creating a UNIX shell. I have it as an assignment for my current sem. We are expected to use C and the shell should be capable of executing around 20 basic commands that we use in UNIX.

I am seriously lost with this. I've read a tutorial about using system calls such as execve, fork, wait, etc. but that was implemented using java. Since i don't really know java, that didn't help with anything other than giving me a vague idea about what i have to do.

Can anyone help? Any kind of C specific tutorial would be highly appreciated. If there's anyone who could point me in the right direction and suggest some reading which might directly or indirectly help me in my efforts, then that would be great too.

Thanks a lot...
# 2  
Old 09-23-2007
Since you haven't asked for a solution, but where to find information I will allow this question.

A good starting point would be the first two C/C++ tutorials here: https://www.unix.com/answers-to-frequ...tutorials.html
# 3  
Old 09-23-2007
Lightbulb Do my homework?

Quote:
Originally Posted by wraith
Alright. I hate to be "that guy" but i seriously need some guidance with creating a UNIX shell. I have it as an assignment for my current sem. We are expected to use C and the shell should be capable of executing around 20 basic commands that we use in UNIX.

I am seriously lost with this. I've read a tutorial about using system calls such as execve, fork, wait, etc. but that was implemented using java. Since i don't really know java, that didn't help with anything other than giving me a vague idea about what i have to do.

Can anyone help? Any kind of C specific tutorial would be highly appreciated. If there's anyone who could point me in the right direction and suggest some reading which might directly or indirectly help me in my efforts, then that would be great too.

Thanks a lot...
You need to write a program, in C, which has to operate as a simplified UNIX shell. You're expected to implement "around 20 basic commands" --- which which I suspect you mean there are about twenty syntactic features.

So let's ask the following question: What is the absolute minimum functionality that a UNIX shell can have?

If you answered something like: read lines of input, parse them into commands and arguments, and then execute those commands with those arguments ... you win!

I hope your C programming skills are up to the simplest portion of this task. You better know how safely read one line of input after another ... and detect an EOF (end-of-file) condition. So that sounds like an outer while loop to me. It's can be terminated by EOF and, if you implement an exit built-in command then that can break out of the loop as well.

Parsing these input lines is, of course, the most complex part of the task at hand. In the simplest case you could seek for the first bit of whitespace (perhaps using the strtok() function -- but note the caveats in the man pages for that). From there you could determine if it's a simple command or if it includes a path. In the first case you'd scan a list of your own built-ins then search the PATH for an executable instance of such a command, in the latter you'd just try to build and execve() with that as the "argv[0]." Then you parse the rest of the command to create the rest of your execve() argument vector (list of NUL terminated strings).

That's the very basics. Then you get into a myriad details about handling globbing (expansion of wildcard patterns into lists of matching filenames) and handling the "plumbing" (pipes and redirection). You'd have to ask your instructor about whether things like the popen(), and system(), or even the execl*() library functions are allowed in your program. The problem with all of those is that they implicitly involve the use of a subshell --- which does its own parsing and plumbing for you. You won't learn much about the implementation of a shell in C if you use C wrapper functions that call on some other shell under the hood.

Normally you'd start by defining your own parser (using lex or its GNU version flex. You feed one of these a of regular expressions and rules about what each sort of token can look like) and it spits out a set of C programs which implements a lexical scanner. Then you'd use that to implement a grammar which is a set of rules about which functions are invoked to handle each sort of token.

If your teacher hasn't discussed lexical scanners and parser generators and hasn't assigned reading material that covers them; then I have to assume either that you're supposed to make a very simple program, devising your own parsing; or that he or she is incompetent.

You might find something like:

The LEX & YACC Page

or:

COMP3610: Principles of Programming Languages - Flex & Bison

To be handy overviews and tutorials on using these sorts of tools.

JimD (former Linux Gazette AnswerGuy)
This User Gave Thanks to AnswerGuy For This Post:
# 4  
Old 10-05-2007
Thanks a lot. Looks like i have everything in place to get started....
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

How to create one's own pipe in one's own shell?

Good evening, I'm trying to do my own shell and I encountered some issues while creating it. Indeed when I try such a command I have DAUPHINE> DAUPHINE> ls -l | grep terminal > fichier DAUPHINE> cat fichier My shell is named DAUPHINE Whereas I should have: Code: ... (0 Replies)
Discussion started by: AntoineCompagni
0 Replies

2. Shell Programming and Scripting

How I can create this Shell scripts?

Hi friends you can solve my question about make 3 shell scripts? Thanks for all! (1 Reply)
Discussion started by: dakota
1 Replies

3. Shell Programming and Scripting

Create Shell Script

Create a script to do the following : a. Poll for ctl file abc.ctl b. if the ctl file is found, then check for corresponding dat file(abc.dat) c. if dat file is not found then fail the process e. if dat file is found do file validation File Validation: a. Check the... (1 Reply)
Discussion started by: vivek1489
1 Replies

4. Shell Programming and Scripting

Need help to create shell script.

When i run the following command it shows me following o/p # prtpicl -v -c temperature-sensor | sed -n '/T_TCORE/,/:name/ p' | grep Temperature 61 Temperature 62 i want to put this command in shell script so that when i run the script it says ********************* Proc1 ... (4 Replies)
Discussion started by: fugitive
4 Replies

5. UNIX for Dummies Questions & Answers

how to create a shell

hi , this is girish, i need an help for creating a shell (2 Replies)
Discussion started by: mcgirishbabu
2 Replies

6. Shell Programming and Scripting

How to create a unix shell

Hello, Can somebody help me find tutorials or example for creating a basic unix shell with minimal functionalities. I am a beginner and want to know about how really the shell works. Thanks. (2 Replies)
Discussion started by: pleaseteachmeun
2 Replies

7. Shell Programming and Scripting

create a report using shell

hi suppose I want to create a report where it will shows the machine name, the date & time when the report is produced. can anyone please help me to write such shell script? requesting all. Thanks (10 Replies)
Discussion started by: moco
10 Replies

8. Shell Programming and Scripting

create a shell script

create a shell script that process a file file contain f2f_100.txt 1234 kkk 12345 f2f_101.txt 1234 mmm 11111 retire_200.txt 2222 rrr 22222 retire_201.txt 1112 qqr 12122 output needed if first field is f2f then new file fb_$1 contain $2|$4 ... (3 Replies)
Discussion started by: maykap100
3 Replies

9. UNIX Desktop Questions & Answers

How do I create desktop icons for the shell programs I create???

I am a bash shell programmer and I create programs on occasional basis. Now, I dont want my programs to be run by typing out its name at a command line. I want to make it as user friendly as possible. I want to create icons on the desktop so users can click on it. mind you, I said "desktop... (7 Replies)
Discussion started by: TRUEST
7 Replies
Login or Register to Ask a Question