LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-29-2002, 06:52 AM   #1
razza
LQ Newbie
 
Registered: Apr 2002
Posts: 4

Rep: Reputation: 0
Question write own linux shell in C


Hi, Im trying to find good online examples of how to write my own 'shell' program, while I've written the odd basic shell script using the plentiful info on the net, Im finding very little info on writing my own shell, Id prefer info that is C specific since thats what I plan to write it in.


Thanks for your time


razza
 
Old 04-29-2002, 07:41 AM   #2
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 46
Why don't you look at the source code of some of the existing shells. The source code is all freely available.
 
Old 05-07-2002, 11:55 AM   #3
tyler_durden
Member
 
Registered: May 2001
Posts: 125

Rep: Reputation: 15
It depends on how complicated you want to get. If you want to have it just parse commands run them and pipe stuff, there are two things you need to do.

First, parse the command line input in a loop. Then use some of the string functions to parse the input. Here is some info on string functions

Then you need to pipe data, open close files and run exec system calls. The basic idea is, that if you have a command, you first fork a new process (fork()), then execute that processs (exec() or a like syscall). If necessary you overwrite the stdin and stdout with either a pipe or output file. You may want to look at this fuctions/syscalls:

exec()
execv()
execl() (there are a lot more, type man exec)'
pipe()
open()
dup2() -- copies a file descriptor (this allows you to overwrite stdin or out with a pipe or file)
fork()
wait()

you can type man to look these up or do some searches on google. also, may favorite quick and easy c tutorial is
here.
 
Old 05-14-2002, 02:02 PM   #4
vfs
Member
 
Registered: Apr 2002
Location: Brazil
Distribution: Slackware
Posts: 184

Rep: Reputation: 30
Try the following shells, for source code and ideas:

- pysh (python shell -- givew it a chance)
- ash
- ae
- rc

They're small and you can find them with http://www.google.com

HTH,

vfs.
 
Old 12-10-2011, 08:43 AM   #5
alpharomeo95
LQ Newbie
 
Registered: Dec 2011
Posts: 1

Rep: Reputation: Disabled
You just have to learn about system calls and signal handling

---------- Post added 12-10-11 at 02:43 PM ----------

Have a look at http://www.gr00ve-hack3r.com/tutorials/shell.html
 
Old 12-10-2011, 12:22 PM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
The essential purpose of a shell is to allow a user to start processes. Conventionally, this is done by entering the name of the executable filespec, as well as an ordered list of arguments to pass to the process (a commandline). To launch the executable, the commandline is passed to one of the fork() + exec() family of functions. The readline library provides a ready-made method of managing commandline editing and user interaction. These two ingredients alone will allow you to construct a working shell.

Most modern shells provide some programmability. I would submit that this is an optional extension to a shell's core purpose. The language and syntax making up a shell's programmable aspect can be whatever you want. There is nothing to say (except POSIX, perhaps) that one shell's language is correct and others are wrong. To create a programming language, you can use the tools Lex & Yacc (or more commonly in the GNU world, flex & bison). While this should ease the pain of crafting a usable programming language, it will be a significant effort in its own right.

In Linux, an important idiom is the ability to use the standard IO model to pass information between programs efficiently, and to efficiently write programs that use this model. A very useful aspect of a shell is to support the use of this model. While much of the syntax used to effect this support has become widely standardized across many shells (even MS-DOS), there is nothing inherently 'right' about what has become the standard. You may choose to find some other method to support the use of the standard IO model, or you may choose to mimic the existing standards, or you may choose not to support the idiom at all. If you do choose to support it, the underlying machinery will make use of a small family of system calls: pipe()/popen(), dup(), and a few related functions that can be found by visiting the 'See Also' section of the man pages for the respective functions.

All of this is a long-winded way of saying that the functionality of most shells is defined by the underlying collections of system calls. In some sense, the shell simply provides an interactive way for the user to make use of the system calls present in the kernel and embodied in the standard C library. Start by examining the standard C library, locate those functions that are necessary to expose to the user, and design whatever kind of program you feel does a good job of giving the user access to those system calls and functions. Don't necessarily limit yourself to the style of shell we've all become accustomed to.


--- rod.

EDIT: Grrr.... sucked into a ten year old thread again...

Last edited by theNbomr; 12-13-2011 at 01:20 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need to write a Bash Shell any advice? CyberEd55 Programming 3 05-12-2005 01:52 PM
Linux shell command for makefile.in to create a text file and write to it alix123 Programming 8 01-07-2005 08:18 AM
learning how to write shell ashley75 Linux - General 3 05-07-2004 02:05 AM
[SHELL SCRIPT] Write at the right of the shell window Creak Linux - General 2 04-02-2004 03:00 PM
Wanna write a shell benjaminrtz Programming 12 10-19-2003 02:00 PM


All times are GMT -5. The time now is 03:35 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration