I have to program a simple shell on UNIX in C. It has to cover pipes,
redirection and background operation etc...
Can anyone tell me where I can find an example I can follow to achieve
this? I am really stumped!
Alternatively, can someone give me a good algorithm so I can understand
what's going on?
Thanks
|
|
0
|
|
|
|
Reply
|
J
|
10/13/2003 10:31:20 AM |
|
J Peterman wrote:
> I have to program a simple shell on UNIX in C. It has to cover pipes,
> redirection and background operation etc...
>
> Can anyone tell me where I can find an example I can follow to achieve
> this? I am really stumped!
Take a look at chapter 6 of Marc Rochkind's _Advanced Unix Programming_.
|
|
0
|
|
|
|
Reply
|
T
|
10/13/2003 11:11:40 AM
|
|
>
> I have to program a simple shell on UNIX in C. It has to cover pipes,
> redirection and background operation etc...
>
Sounds like B310 assignment 1 at mudrock. Ahh, memories..
> Can anyone tell me where I can find an example I can follow to achieve
> this? I am really stumped!
>
> Alternatively, can someone give me a good algorithm so I can understand
> what's going on?
Most of the work is in the parsing routine. Understanding
the basics of a shell is the first hurdle:-
How do you tell a command from its arguments? How can you tell if
there's more than one command per line (eg, a "pipeline")?
(Remember `ls & who' is a valid command line)
How do you know when it's valid to run a pipeline, or redirect
input/output? (Think of where arguments are relative to the
redirection/pipe operator)
How do you know when to run a program in the background?
There might also be "shell builtins" to implement, as well as
exec()ing of external programs. These modify the current
process, rather than starting a new one.
Think about how you're going to represent the parsed command
line as data structures. See the exec*() manpage for details
on how processes like their arguments. You might also want
to store information on what you need to do - fork()/exec*()
a single process, or run a pipeline of 2 processes, or run
one process in the background, or...
If you're not familiar with the C library string manipulation
functions, you soon will be. `man string' lists a bunch of
routines which you'll find essential, assuming you're using C,
which most people do (at least, i've never seen anyone use C++).
I haven't seen the Rochkind book; i reckoned it was heresy for them
to drop Rich Steven's _Advanced Programming in the UNIX Environment_
in the first place. A copy should be in the library, or on your
bookshelf if you're serious about Unix. [RIP, Rich. 3 years already?]
Oh, go and grab the comp.unix.programmer FAQ - there's a couple of
sections you should find useful there (namely, the processes part).
Follow the KISS principle, get the basics working and maybe THEN get
fancy (handling escape chars, etc etc). The time/credit ratio
probably isn't worth it though.. voice of experience. Spend the time
drinking instead.
Obviously, DON'T LEAVE THIS 'TIL THE LAST MINUTE or you'll find
yourself screwed - in the bad way...
--
|
|
0
|
|
|
|
Reply
|
whotookmyaccount
|
10/14/2003 1:44:46 PM
|
|
> > I have to program a simple shell on UNIX in C. It has to cover pipes,
> > redirection and background operation etc...
> >
>
> Sounds like B310 assignment 1 at mudrock. Ahh, memories..
Hey! Actually, it's the 2nd assignment...It seems like it's actually a very
common OS unit assignment, except this one goes into a lot more detail with
the inclusion of pipes, redirection and built in functions, which other
people don't seem to have to do. I would have thought it would be easy to
find some code, but it's proving frustrating.
> > Can anyone tell me where I can find an example I can follow to achieve
> > this? I am really stumped!
> >
> > Alternatively, can someone give me a good algorithm so I can understand
> > what's going on?
>
> Most of the work is in the parsing routine. Understanding
> the basics of a shell is the first hurdle:-
We've been given the parser (which was the first assignment).
> How do you tell a command from its arguments? How can you tell if
> there's more than one command per line (eg, a "pipeline")?
> (Remember `ls & who' is a valid command line)
I can break up a command line into tokens, and identify the command. And I
can break up different commands based on the special symbols > < | &.
> How do you know when it's valid to run a pipeline, or redirect
> input/output? (Think of where arguments are relative to the
> redirection/pipe operator)
Haven't looked at pipes yet (will be soon). I'm pretty sure I can tell a
valid command line from an invalid one.
> How do you know when to run a program in the background?
If a command (plus parameters) ends with a &.
> There might also be "shell builtins" to implement, as well as
> exec()ing of external programs. These modify the current
> process, rather than starting a new one.
These shell builtins are a bit of a worry for me. For example, I have to
implement my own version of "cd", but I don't quite understand how my shell
will interact with the existing files.
> Think about how you're going to represent the parsed command
> line as data structures. See the exec*() manpage for details
> on how processes like their arguments. You might also want
> to store information on what you need to do - fork()/exec*()
> a single process, or run a pipeline of 2 processes, or run
> one process in the background, or...
At the moment, this sounds like gibberish to me...... :(
> If you're not familiar with the C library string manipulation
> functions, you soon will be. `man string' lists a bunch of
> routines which you'll find essential, assuming you're using C,
> which most people do (at least, i've never seen anyone use C++).
Yep, doing it in C.
> I haven't seen the Rochkind book; i reckoned it was heresy for them
> to drop Rich Steven's _Advanced Programming in the UNIX Environment_
> in the first place. A copy should be in the library, or on your
> bookshelf if you're serious about Unix. [RIP, Rich. 3 years already?]
I don't think I'm serious about unix programming....just struggling to get
through. Although got a bit of good news in the form of 98% for the first
assignment (command line parser). Dunno how I managed that though.
> Oh, go and grab the comp.unix.programmer FAQ - there's a couple of
> sections you should find useful there (namely, the processes part).
Wicked, will look into that.
> Follow the KISS principle, get the basics working and maybe THEN get
> fancy (handling escape chars, etc etc). The time/credit ratio
> probably isn't worth it though.. voice of experience. Spend the time
> drinking instead.
> Obviously, DON'T LEAVE THIS 'TIL THE LAST MINUTE or you'll find
> yourself screwed - in the bad way...
Yep, trying to tie it up over the next week and a bit. Concentrating on
learning what all the fidly bits do (lab exercises), then will hopefully
have a better idea on what to do with this shell.
|
|
0
|
|
|
|
Reply
|
J
|
10/15/2003 9:41:26 AM
|
|
|
3 Replies
115 Views
(page loaded in 0.131 seconds)
Similiar Articles: newbie question: shell script to monitor server performance - comp ...But asking someone else to write it for you isn't very cool. If you need help, hire a consultant. ... Question - How to use SFTP & to replace FTP in shell script Newbie ... comp.lang.awk - page 45Newbie needs help on pattern matching 8 59 (9/2/2004 8:37:18 PM) Hi ... bit 4-5 years ago, but never needed it till now - I am writing an application). I need ... Newbie with SAS - comp.soft-sys.sas... IBM Mainframe environment and need some help ... satisfy the condition and write the ... Newbie with SAS - comp.soft-sys.sas... and need some help with SAS. Newbie:Cannot Ping Soalris 8 by hostname - comp.unix.solaris ...... Everything looks fine in those files. Can any body help ... settings are wrong (if you are using DNS) or you need an ... address to a socket with Solaris? > You could write a ... Newbie: OpenGL concepts - comp.graphics.api.openglHi, I need to re-write an extensive chunk of DiB graphics code in openGL and would appreciate some help in understanding how ... graphics.api.opengl You need to ... Newbie ... Date-time conversion from "YYYY-MM-DD HH:MI:SS" to unix epoch ...... bh4gqo$fl2$1@lust.ihug.co.nz>, newbie <newbie@newbie.org> wrote: > I need to ... to do that or do I have : to write one ... fromunixtime(), directly on the Unix shell ... Swing accelerator keys - comp.lang.java.guiNEWBIE NEEDS ADVICE AND OR HELP 1 1 arroyoce ... RE:write Document Object to pdf file directly? 0 65 pvii007 awk newbie: how replacing dots with colons? - comp.lang.awk ...... command, with poor results. Thanks for any help! ... wq" | ed -s foo of, if you have BASH as a shell, you could write ... in awk that make solutions terse but make the newbie ... Need help #46 - comp.soft-sys.matlab... maybe this is easy for you but i am just a newbie, so i really need your help soon ... Need help figuring out how to write the script for this problem ... Need help figuring ... Dividing image to sub-blocks by dct2 and for each keeping only ...Could you help me with writing a script. 1)I want to sub-block a 512x512 image to 8x8 ... Matlab Newbie needs to 3D contour plot a couple of vectors vs. a matrix Z 1 163 Need help writing for loop shell script - LinuxQuestions.orgi am trying to write a script (which I havent done since college) that will grep each line from a file named Backlist which contains the titles of Need help with writing a c shell script?Hi All, I want to write a C/perl shell script but I am not sure exactly to go about writing it.This is what how I can expalin you what the script 7/14/2012 3:13:26 PM
|