Question: Implement a simple shell in C that supports the following commands: 1 . echo - print a string to the console myshell > echo 2

Implement a simple shell in C that supports the following commands:
1. echo - print a string to the console
myshell> echo
2. cat - print the contents of a file to the console
myshell> cat
3. grep - search for a pattern in a file
myshell> grep
:
Example:
# Contents of file.txt
This is a test file
This is an example file
This is a sample file
This is a test file
myshell> grep test file.txt
1:This is a test file
4:This is a test file
4. head - print the first n lines of a file
myshell> head -n
Example:
# Contents of file.txt
This is a test file
This is an example file
This is a sample file
This is a test file
myshell> head -n 2 file.txt
This is a test file
This is an example file
5. tail - print the last n lines of a file
myshell> tail -n
Example:
# Contents of file.txt
This is a test file
This is an example file
This is a sample file
This is a test file
myshell> tail -n 2 file.txt
This is a sample file
This is a test file
6. wc - print the number of lines, words, and characters in a file
myshell> wc
Example:
# Contents of file.txt
This is a test file
This is an example file
This is a sample file
This is a test file
myshell> wc file.txt
42086 file.txt
7. touch - create a new empty file
myshell> touch
8. Pipes (|)- support the use of pipes to chain commands together
myshell> ls | wc
Explaining the above example:
- ls lists the contents of the current directory
- wc counts the number of characters, words, and lines in the output of ls
- The output of ls is piped to the input of wc and not to the console.
- The output of wc is printed to the console as the result of the command
Example 2:
# Contents of file.txt
This is a test file
This is an example file
This is a sample file
This is a test file
myshell> cat file.txt | grep test | wc
2
Explaining the above example:
-cat file.txt prints the contents of file.txt to the console.
-grep test searches for the pattern test in the output of cat file.txt
-wc counts the number of characters, words, and lines in the output of grep test
-The output of cat file.txt is piped to the input of grep test and not to the console.
-The output of grep test is piped to the input of wc and not to the console.
-The output of wc is printed to the console as the result of the command
-The result of the command is the number of lines in file.txt that contain the pattern test
9. Redirection (>)- support the use of redirection to write the output of a command to a file
myshell> ls > files.txt
Explaining the above example:
-ls lists the contents of the current directory
-The output of ls is written to the file files.txt and not to the console
Example 3:
myshell> echo "Hello, World!" > hello.txt
Explaining the above example:
-The output of echo "Hello, World!" is written to the file hello.txt and not to the console
-The file hello.txt is created if it does not exist, and overwritten if it does exist
Note that the head and tail commands should support the -n flag to specify the number of lines to print. If the -n flag is not provided, the default number of lines to print should be 10.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!