Skip to content

akshatmittal2002/linux-mini-shell

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

Linux-Mini-Shell

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.



List Of Operations:

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.



NOTE:

  1. 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 in LINUX Shell.
    Example: For cat < file1.txt, you can use:
    • cat < file1.txt
    • cat <file1.txt
    • cat< file1.txt
    • cat<file1.txt
  2. Run the command only in LINUX Terminal since some pre- installed libraries of LINUX are used in code.
  3. To view all logics and process followed, see the .c file.
    Code is properly documented.


A Brief Description:

  1. An infinite while loop is created, printing current directory and waiting for single line user input.
  2. If user calls for cd, then directory is changed to given path if the path exists.
  3. 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.
  4. 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.
  5. If user calls history, then the history file is printed.
  6. If user type exit/quit/x, history file is deleted, all memory is freed, loop is broken and program terminates.
  7. If there is piping in input, after all validity checks, two child processes are created and then both internal and external commands are executed.
  8. 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.
  9. In all other cases, user input is executed using a child process and if anything invalid, error is displayed.
  10. In all these cases, the input is saved in the history file in sequeunce.
  11. Then all memory is freed and loop continues from step 1.

About

Linux Shell Implementation In C - Language

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages