This is a C
code for Linux Shell (a mini version).
The code is designed to work properly in LINUX
terminal.
To compile the code and run, use these commands on a linux terminal:
1) gcc main.c
2) ./a.out
Mini shell will start in current working directory.
1. setenv
- Sets value of given environment variable to given value.
2. cd
- All operations of cd command are replicated:
- cd ..
- cd
- cd sub_directory
- cd real_path
3. history
- Prints complete list of all commands used in mini-shell by user
4. quit / exit / x
- Command to exit from the mini-shell
5. Piping ( | )
- Single level piping can be done in my shell.
- For example:
- ls -lh | echo
- cat file1.txt | wc
6. Redirections
- All sorts of redirections into/from files are handled:
- <input.txt
- >output.txt
- >>append.txt
- <input.txt >output.txt
- <input.txt >>append.txt
7. Reasonable List of External LINUX commands
- man, which, chsh, whereis, passwd, date, cal, clear, sleep, history, apropos, exit, logout, shutdown, ls, cat, more, less, touch, cp, mv, rm, script, find, mkdir, cd, pwd, rmdir, chmod, grep, etc.
- I have managed all whitespaces in input.
You can type your command in any format (with or without extra spaces).
It will be valid as long as it is valid inLINUX Shell
.
Example: Forcat < file1.txt
, you can use:- cat < file1.txt
- cat <file1.txt
- cat< file1.txt
- cat<file1.txt
- Run the command only in
LINUX Terminal
since some pre- installed libraries ofLINUX
are used in code. - To view all logics and process followed, see the .c file.
Code is properly documented.
- An infinite while loop is created, printing current directory and waiting for single line user input.
- If user calls for cd, then directory is changed to given path if the path exists.
- If user calls setenv with proper format, then a new environment varibale is created with specified value. If environment variable already exists, then it's value is changed.
- If user calls printenv, then:
- If no envirionment varibale is given, all envirionment variables is printed with their values.
- Else the values of specified variables are printed.
- If user calls history, then the history file is printed.
- If user type exit/quit/x, history file is deleted, all memory is freed, loop is broken and program terminates.
- If there is piping in input, after all validity checks, two child processes are created and then both internal and external commands are executed.
- If there is redirection, after all validity checks, a child process is created and both internal and external commands are executed with proper redirections to the files.
- In all other cases, user input is executed using a child process and if anything invalid, error is displayed.
- In all these cases, the input is saved in the history file in sequeunce.
- Then all memory is freed and loop continues from step 1.