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:
echo print a string to the console
myshell echo
cat print the contents of a file to the console
myshell cat
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
:This is a test file
:This is a test file
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 file.txt
This is a test file
This is an example file
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 file.txt
This is a sample file
This is a test file
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
file.txt
touch create a new empty file
myshell touch
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 :
# 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
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
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 :
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
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
