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.
04-16-2005, 01:36 AM
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...
04-16-2005, 02:09 PM
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
As for environment variables, you can find more information here .
04-17-2005, 08:27 PM
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.
04-17-2005, 08:33 PM
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?
04-17-2005, 11:09 PM
Your probably going to have to look at the source code for the existing shells and see what is required then.
04-18-2005, 12:05 AM
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...
04-18-2005, 12:31 AM
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.
04-18-2005, 02:32 PM
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.
04-18-2005, 06:16 PM
near-code, you have to fill in around it but the general idea:
case "quit" :
cleanup(); //do whatever you need to before exit
done = true;
case "command" :
04-19-2005, 02:07 AM
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.
04-19-2005, 09:01 AM
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...
04-19-2005, 05:35 PM
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.