0% found this document useful (0 votes)
15 views4 pages

Basic Shell Implementation Assignment

The assignment requires students to build a basic shell similar to the bash shell in Linux, implementing features such as command execution, pipes, and built-in commands like 'cd' and 'history'. Students must ensure error handling and user interruption with the 'exit' command, and submit their well-commented C code in a specified format. Test cases are provided to validate the functionality of the shell, including command execution and piping between commands.

Uploaded by

abhu.krish59
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)
15 views4 pages

Basic Shell Implementation Assignment

The assignment requires students to build a basic shell similar to the bash shell in Linux, implementing features such as command execution, pipes, and built-in commands like 'cd' and 'history'. Students must ensure error handling and user interruption with the 'exit' command, and submit their well-commented C code in a specified format. Test cases are provided to validate the functionality of the shell, including command execution and piping between commands.

Uploaded by

abhu.krish59
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

Assignment 1: Building a Basic Shell

Professor: Ashutosh Rai


TA: Yash Shirke, Bhavik Sankhla, Aakrity Pandey
Deadline: 11th August 11:59 p.m

Introduction
In this assignment, you will create a basic shell similar to the bash shell in Linux.
Your shell will be responsible for executing user commands, handling pipes
between commands, and implementing some built-in functionalities. Follow the
steps below to complete the assignment:

Objectives
1. Basic Shell Implementation:
• Implement a shell that reads user input and executes commands using
the exec system call.
• The shell should display the prompt "MTL458 >" and continuously
process commands until interrupted by the user.
• Handle errors during command execution by displaying appropriate
error messages.

2. Command Execution:
• Your shell must support executing standard Linux commands such
as ls, cat, echo, and sleep.
3. Pipes:

• Implement support for piping between commands. For example, your


shell should correctly handle a command like grep -o foo file |
wc -l, where the output of one command is used as input to the
next. Assume single pipes only and not multiple pipes.
4. Built-in Commands:

1
• In addition to executing external commands, your shell should sup-
port some built-in commands. Implement the following built-in func-
tionalities:
– cd directoryname: Change the current working directory of the
[Link] can assume the folder names do not contain spaces in
them.
– history: Display the history of commands entered by the user.
5. User Interrupt:
• Ensure that the shell program terminates after receiving the com-
mand exit.
6. Error Handling:
• Ensure that your shell handles incorrect arguments or command for-
mats gracefully. Display error messages without crashing the shell
and continue to prompt for the next command.

Submission
• Submit your source code file in a zipped folder named as your entry number
followed by and assignment1, example: 2020MT60867 assignment1.
• The submission should be in C, and the file should be named entrynumber shell.c.
• Ensure your code is well-commented and follows best practices for read-
ability and maintainability.

Test Cases
Assume the following folder structure in your home directory:

/home/user/
[Link]
[Link]
directory1/
[Link]
directory2/
[Link]
subdirectory1/
[Link]
[Link]

Here’s the content of each file for reference:

• [Link]: Contains the text hello world

2
• [Link]: Contains the text foo bar
• [Link]: Contains the text foo foo foo
• [Link]: Contains the text bar bar bar

• [Link]: Contains the text foo bar foo


• [Link]: Contains the text echo "This is a script"

1 Test Cases and Expected Outputs


1.1 Basic Command Execution
Test Case 1.1:
Execute a simple command ls

Input: ls
Expected Output:
[Link]
[Link]
directory1
directory2
[Link]

Test Case 1.2:


Execute a command with arguments cat [Link]

Input: cat [Link]


Expected Output:
hello world

Test Case 1.3:


Execute a command with no arguments echo "Hello"

Input: echo "Hello"


Expected Output:
Hello

1.2 Pipes Between Commands


Test Case 2.1:
Pipe output of ls to grep

3
Input: ls | grep file
Expected Output:
[Link]
[Link]
[Link]

Test Case 2.2:


Pipe output of cat [Link] to grep foo

Input: cat [Link] | grep foo


Expected Output:
foo bar

1.3 Built-in Commands


Test Case 3.1:
Check current directory after cd

Input: pwd
Expected Output:
/home/user/directory2

Test Case 3.2:


Show command history with history

Input: history
Expected Output: A list of recent commands.

Test Case 3.3:


Use cd with invalid directory

Input: cd non_existent_directory
Expected Output:
The File Path Does not exist

Good luck, and happy coding!

Common questions

Powered by AI

Testing the pipe functionality involves executing commands where the output of one command is used as input for another. For instance, running an initial test case like 'ls | grep file', expecting output such as 'file1.txt file2.txt script.sh', helps validate the proper handling of the pipeline between the 'ls' and 'grep' commands. Subsequently, testing with different combinations, such as 'cat file2.txt | grep foo', ensures various scenarios are handled correctly .

To implement a basic shell similar to bash in Linux, the shell must be able to execute user commands using the exec system call, handle errors during command execution, support executing standard Linux commands like ls, cat, echo, and sleep, and handle single pipes between commands. It must also support built-in commands such as 'cd directoryname' to change the current working directory, 'history' to display the history of entered commands, and terminate upon receiving the 'exit' command .

To handle commands with incorrect arguments gracefully, the shell can implement input validation processes before attempting execution. Approaches might involve checking command syntax, verifying argument count, and confirming existence of files or directories where applicable. Error messages should be precise yet user-friendly, indicating the type of error without divulging sensitive information. Logging such errors internally can also be beneficial for debugging purposes, all while maintaining a prompt cycle to ensure consistent user interaction .

Improper handling of the 'exit' command could prevent the shell from terminating correctly, leading to resource leaks or leaving users with an active shell process that cannot be closed without external intervention. Ensuring that the shell program terminates immediately after receiving the 'exit' command is vital for gracefully ending the user session and releasing associated resources .

Handling errors is crucial in a custom shell implementation to ensure that the shell does not crash when encountering incorrect arguments or command formats. Proper error handling provides feedback to the user through error messages and allows the shell to continue processing further commands by prompting for the next instruction. This increases the system's robustness and usability .

The exec system call contributes to command execution by replacing the current process image with a new process image specified by the command to be executed. This allows the shell to delegate the execution of the command to the underlying operating system, ensuring that the command runs with the appropriate system-level handling and resources. This is essential for achieving behavior akin to standard shell environments found in Linux systems .

Best practices for code submission in programming assignments include submitting the source code in a named format aligned with assignment guidelines, such as 'entrynumber assignment1'. The code should be well-commented and follow readability and maintainability standards. This entails using meaningful variable names, structuring the code logically, and including comments to clarify complex code sections, ensuring that graders can easily understand and evaluate the work submitted .

Handling single pipes between commands allows a shell to connect multiple commands together, where the output of one command becomes the input of another. This functionality enables the shell to execute complex tasks that require data manipulation across different commands, enhancing the overall capability of the shell by enabling more sophisticated command chains, such as using 'grep -o foo file | wc -l' .

Implementing the command history feature may involve challenges such as managing memory effectively to store a potentially large history of commands, ensuring quick retrieval and display, and handling concurrent access when displaying or storing new commands. These challenges could be addressed by using efficient data structures like circular buffers or linked lists to balance memory usage and performance, and using locks or concurrent-safe structures if multithreading is involved .

The 'cd' command differs as a built-in command because it modifies the shell's current working directory within the process, which cannot be achieved through a child process spawned by external command execution. External commands are typically executed in separate processes, and changes such as directory navigation would not persist after the subprocess exits. Therefore, built-in commands like 'cd' are essential for directly altering the state of the shell's environment .

You might also like