Thread: how to simulate Unix commands in C++?

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    11

    how to simulate Unix commands in C++?

    Hi
    I have been asked to creat a Shell or Command Line Interpreter to simulate Unix commands like cd,clr,dir,echo,help,pause,quit and it must be able to take its command line input from a file.As well as it must support i/o-redirection on either or both stdin and/or stdout
    I have no clu how to start this . I need some help by recommanding me some useful website or books
    Thanks in Advance

  2. #2
    Registered User
    Join Date
    Feb 2005
    Posts
    59

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    By "simulate", I suppose that you really have to do it??? That's going to depend on your operating system. I hope you have a few weeks to work on this!

    Do you know what a "parser" is? Look that up - Look for some parser sample code., or maybe you can figure it out yourself. A parser breaks a string into parts... and figures out what to do, one step at a time... A decision tree....

    So, if you type "CD A:" The parser figures-out what CD means (what function to call to change directory), then it looks at the A:, and figures-out what to do with that "variable" to change to drive A:

    That should get you started. I'd write the parser first. Once your program can understand the commands, and you have some if-statements or switch-statements to direct program-flow, you can impliment the code to "take action" based on the commands.
    Last edited by DougDbug; 03-30-2006 at 06:18 PM.

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Code:
    #include <iostream>
    #include <string>
    #include <cstdlib>
    
    int main()
    {
    	std::string command;
    	for(;;)
    	{
    		std::cout<<"> ";
    		getline(std::cin,command,'\n');
    		system(command.c_str());
    	}
    	return 0;				
    }


    note: don't do that. it doesn't even work for some commands...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote Originally Posted by silent_eyes
    Hi
    I have been asked to creat a Shell or Command Line Interpreter to simulate Unix commands like cd,clr,dir,echo,help,pause,quit and it must be able to take its command line input from a file.As well as it must support i/o-redirection on either or both stdin and/or stdout
    I have no clu how to start this . I need some help by recommanding me some useful website or books
    Thanks in Advance
    pause? pause is a Unix command? dir? Okay, sometimes. clr? Which unix? quit? Maybe.

    Here are some lecture notes that were used to help prepare students for this kind of project in C.
    http://www.cs.rpi.edu/~hollingd/opsy...hell/shell.pdf
    Note that the requirements for his project might be slightly different from those of your project.

    They give the general idea; you'll also need to read some man pages for specifics, especially with regard to error codes.
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help in Unix commands
    By namasteall2000 in forum Networking/Device Communication
    Replies: 10
    Last Post: 04-02-2009, 03:34 PM
  2. System Commands for UNIX
    By cjohnman in forum C Programming
    Replies: 5
    Last Post: 04-07-2008, 01:39 PM
  3. How to program in unix
    By Cpro in forum Linux Programming
    Replies: 21
    Last Post: 02-12-2008, 10:54 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. About Unix Programming - Making a career desision
    By null in forum C Programming
    Replies: 0
    Last Post: 10-14-2001, 07:37 AM