Shell Program


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 14 of 14

Thread: Shell Program

  1. #1
    Join Date
    Mar 2005
    Posts
    27

    Shell Program

    Hello everyone! I was wondering if anyone can assist me in writing a linux shell in c++. I am having trouble writing this simple shell.The Shell must support these commands:

    i. cd <directory> - change the current default directory to <directory>. If the <directory> argument is not present, report the current directory. If the directory does not exist an appropriate error should be reported. This command should also change the PWD environment variables.
    ii. clr - clear the screen
    iii. dir <directory> - list the contents of directory <directory>
    iv. environ - list all the environment strings
    v. echo <comment> - display <comment> on the display followed by a new line (multiple spaces/tabs may be reduced to a single space)
    vi. help - display the user manual using the more filter
    vii. pause - pause operation of the shell until 'Enter' is pressed/
    viii. quit - quit the shell
    ix. The shell environment should contain shell = <pathname>/myshell where <pathname>/myshell is the full path for the shell executable (not a hardwired path back to your directory, but the one from which it was executed)



    If anyone can assis me with this I would appreciate it. Thanks.

  2. #2
    Join Date
    Oct 2004
    Location
    Baltimore MD
    Posts
    69
    Is this shell going to run on top of BASH or is it made to be a stand-alone shell?

    The reason I ask this is because if it runs on top of bash you can just use the system() funtion to call bash commands...

    -Zak

  3. #3
    Join Date
    Nov 2003
    Posts
    4,118
    If the shell is meant to support one command, you can use command line arguments. Your program essentially has to read the arguments, prase and validate them.

    You can read about command line arguments here
    and here

    As for environment variables, you can find more information here .
    Danny Kalev

  4. #4
    Join Date
    Mar 2005
    Posts
    27

    Reply to Zak

    Yes, my program has to be a stand alone program. It must run on it's own without using the commands that are already there. I am uncertain of how it would work as a stand alone program and that is why i need help.
    Last edited by none_none; 04-17-2005 at 07:31 PM.

  5. #5
    Join Date
    Mar 2005
    Posts
    27

    Reply to Danny

    Yes, I am supposed to do exactly that. The thing is that I am not sure how. Can you help me out?

  6. #6
    Join Date
    Dec 2003
    Posts
    3,366
    Your probably going to have to look at the source code for the existing shells and see what is required then.

  7. #7
    Join Date
    Oct 2004
    Location
    Baltimore MD
    Posts
    69
    Yes, I agree with jonnin^. Thats the beauty of Linux. It's all open source. Just pick a distro of linux you like (i.e. Fedora is a good one.) and download the "source" iso. That would be the best way...
    -Zak

  8. #8
    Join Date
    Nov 2003
    Posts
    4,118
    I don't know about this. Reading the source file of a real world professional sheel program can be an overwhelming experience for a beginner. What I suggest is writing a rudeminteray shell that supports one command and one paramter and then extend it to suppoer more paramers. Note that the important thing is to get the commands parsed correctly; the shell doesn't have to support every possible option but it has to detect typos, incorrect parameters etc.
    Nomrally, the program consists of one big loop that reads input, parses it and executes it.
    Danny Kalev

  9. #9
    Join Date
    Mar 2005
    Posts
    27

    Reply to Danny

    Yes, I understand what you are telling me but the thing is that I am unable to traslate it into code. Is there any way that you can start me off by showing me the quit and clear commands. I am totally lost and do not know what I am doing. Thanks.

  10. #10
    Join Date
    Dec 2003
    Posts
    3,366
    near-code, you have to fill in around it but the general idea:

    int main()

    {

    while(!done)
    {
    cin.getline(in);
    switch(in)
    {
    case "quit" :
    {
    cleanup(); //do whatever you need to before exit
    done = true;
    break;
    }
    case "command" :
    {
    do_command();
    break;
    }
    }
    }

    }

  11. #11
    Join Date
    Mar 2005
    Posts
    27

    Reply to jonnin

    Thanks man! I really appreciate it much. I will see what i can do and then ask you more questions if I am stuck. This helps some. Thanks.

  12. #12
    Join Date
    Dec 2003
    Posts
    3,366
    Personally, I would just mask the commands that exist (for example, dir is just ls-la or some similar commandline option on ls) -- linux lets you alias (is that spelled right?) commands to a new name. That way, you still have a 'real' shell behind you (environment variables, paths, etc). Just a thought, if you only want to emulate a few dos commands...

  13. #13
    Join Date
    Mar 2005
    Posts
    27

    Reply to jonnin

    Is there any way that you can show me how to mask the commands. I am still struggling with this shell and need to actually see what you are talking about. Thanks.

  14. #14
    Join Date
    Dec 2003
    Posts
    3,366
    http://www.computerhope.com/unix/ualias.htm

    if you put such a command into your .profile or whatever those strange, sort of hidden files are called, its always there for you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center