0% found this document useful (0 votes)
6 views

assignment5

The document outlines the implementation of a simple shell, detailing its basic structure, steps for implementation, and a basic example in C. Key components include command prompt display, input parsing, command execution, and error handling. The project provides practical experience with operating system concepts and serves as a foundation for advanced systems programming.

Uploaded by

yennamvishal1883
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

assignment5

The document outlines the implementation of a simple shell, detailing its basic structure, steps for implementation, and a basic example in C. Key components include command prompt display, input parsing, command execution, and error handling. The project provides practical experience with operating system concepts and serves as a foundation for advanced systems programming.

Uploaded by

yennamvishal1883
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

S I M P L E S H E L L I M P L E M E N TAT I O N

PRESENTATION BY

GROUP MEMBERS
1. Vishal Chakradhari Yennam
2. Soham Rajesh Pitale
3. Shreyash Ashok Vekhande
4. Ayyan Safi Bardi
2

CONTENTS
o Basic Structure of a Shell

o Steps to implement a Basic Shell

o Basic example in C

o Compiling and Running

Crypto: investing & trading


BA S I C S T RU C T U R E O F A S H E L L
o Command Prompt: Displays a prompt where the user can type commands.Steps to implement a
Basic Shell.

o Input Parsing: Reads the command input from the user and parses it into executable commands
and arguments.Additional Features for EnhancemenSummary.

o Execution of Commands: Uses system calls to run the specified commands.

o Loop: A shell typically runs in a loop, reading commands, executing them, and returning to the
prompt until an exit command is given.
STEPS TO IMPLEMENT A BASIC SHELL

1. Initialize the Shell.


o Start with a main function that loops to keep the shell active.

o Display a prompt (e.g., $ ) where the user can enter commands.

2. Read User Input


o Use functions like getline in C (or input in Python) to read user input.

o Store the input command in a string.


STEPS TO IMPLEMENT A BASIC SHELL
3. Parse the Input
o Split the input into the command and its arguments.

o Tokenize the string (using strtok in C, or split() in Python) to separate each word based on spaces.

o Identify the command and store arguments separately for execution.

4. Execute Commands
o Use system calls to run the parsed commands.

o For Linux: Use fork, exec, and wait functions.

o Handle built-in commands like cd, exit, or any shell-specific commands manually in the shell code.

4. Error Handling and Exit


o Check for invalid commands and handle errors (e.g., “Command not found”).

o Allow the user to exit the shell by typing commands like exit or quit .
BASIC EXAMPLE IN C
#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <sys/types.h>

#include <sys/wait.h>

#define MAX_INPUT 1024

#define MAX_ARGS 64

void shell_loop() {

char input[MAX_INPUT];

char *args[MAX_ARGS];
BASIC EXAMPLE IN C
int should_run = 1;

while (should_run) {

printf("$ ");

fgets(input, MAX_INPUT, stdin);

// Remove newline character from input

input[strcspn(input, "\n")] = '\0';

// Parse input

int arg_count = 0;

args[arg_count] = strtok(input, " ");

while (args[arg_count] != NULL) {

arg_count++;
BASIC EXAMPLE IN C
args[arg_count] = strtok(NULL, " ");

// Check for built-in commands

if (strcmp(args[0], "exit") == 0) {

should_run = 0;

continue;

// Fork and execute command

pid_t pid = fork();

if (pid < 0) {

perror("Fork failed");
BASIC EXAMPLE IN C
} else if (pid == 0) {

execvp(args[0], args);

perror("Execution failed");

exit(1);

} else {

wait(NULL); // Wait for the child process to complete

}}}

int main() {

shell_loop();

return 0;

}
C O M P I L I N G A N D RU N N I N G
gcc -o simple_shell simple_shell.c

./simple_shell
CONCLUSION
In summary, implementing a simple shell offers hands-on experience with key operating system
concepts like process management, command parsing, and system calls. This project strengthens your
understanding of how commands interact with the system and builds a foundation for more
advanced OS features, making it an excellent step toward mastering systems programming.

REFERENCES
https://chat.open.ai/

https://geeksforgeeks.com/

https://www.inuxfromscratchguide.com/
T H A N K YO U

You might also like