Your Own Linux Shell Solution

$35.00 $30.80

Description

Introduction

 

Most useful interaction with a UNIX/Linux-based system occurs through the shell. Using a series of easy to remember and simple commands, one can navigate the UNIX le system and issue commands to perform a wide variety of tasks. Even though it may appear simple, the shell encapsulates many signi cant components of the operating system.

 

 

Basic Shell Features

 

Environment

 

The shell maintains many variables which allow the user to maintain settings and easily navigate the lesystem. Two of these that are particularly important are the current working directory and the PATH. As its name implies, the current working directory variable keeps track of the user’s current directory. The PATH variable consists of string of colon separated pathnames. Whenever you type a name of a command, the kernel searches in the directories speci ed by the PATH variable starting with the leftmost directory rst. If the executable is not found in any of the speci ed directories, then the shell returns with an error. One may modify the PATH at any time to add and remove directories to search for executables.

 

 

 

shell:~$ pwd

 

/home/tanzir

 

shell:~$ echo $PATH

 

/usr/local/bin:/usr/bin:/usr/local/games:..

 

 

Figure 1: Current directory and the PATH enviromental variable.

 

Pipelining

 

UNIX provides a variety of useful programs for you to use (grep, ls, echo, to name a few). Like instructions in C++, these programs tend to be quite effective at doing one speci c thing (Such as grep searching text, ls printing directories, and echo printing text to the console). However, programmers/OS users would like to accomplish large tasks consisting of many individual operations. Doing such requires using results from previous steps in order to complete a larger problem. The UNIX shell supports this through the pipe operation (represented by the character j). A pipe in between two commands causes the standard output of one to be redirected into the standard input of another. An example of this is provided below, using the pipe operation to search for all processes with the name “bash”.

 

 

 

 

 

1

 

CSCE 313 Spring 2020

 

 

 

 

shell:~$ ps -elf | grep bash | awk ’{print $10}’ | sort

 

3701

 

4197

 

 

Figure 2: Pipelining multiple commands.

 

Input/Output Redirection

 

Many times, the output of a program is not intended for immediate human consumption (if at all). Even if someone isn’t intending to look at the output of your program, it is still immensely helpful to have it print out status/logging messages during execution. If something goes wrong, those messages can be reviewed to help pinpoint bugs. Since it is impractical to have all messages from all system programs print out to a screen to be reviewed at a later date, sending that data to a le as it is printed is desired.

 

Other times, a program might require an extensive list of input commands. It would be an unnecessary waste of programmer time to have to sit and type them out individually. Instead, pre-written text in a le can be redirected to serve as the input of the program as if it were entered in the terminal window.

 

In short, the shell implements input redirection by redirecting the standard input of a program to an le opened for reading. Similarly, output redirection is implemented by changing the standard output (and sometimes also standard error) to point to a le opened for writing.

 

 

 

 

shell:~$ echo ‘‘This text will go to a file’’ > temp.txt

 

shell:~$ cat temp.text

 

This text will go to a file

 

shell:~$ cat < temp1.txt

 

This text came from a file

 

 

Figure 3: I/O redirection.

 

 

Assignment

 

For this assignment, you are to design a simple shell which implements a subset of the functionality of the Bourne Again Shell (Bash). The requirements for your shell are as follows:

 

 

Continually prompt for textual user input on a command line. Parse user input according the provided grammar (see below)

 

When a user enters a well formed command, execute it in the same way as a shell. You must use the commands fork and exec to accomplish this. You may NOT use the C++ system() command.

 

Allow users to pipe the standard output from one command to the input of another an arbitrary number of times.

 

2

 

CSCE 313 Spring 2020

 

 

Support input redirection from a  le and output redirection to a  le.

 

Allow users to specify whether the process will run in the background or foreground using an ‘&’. Backgrounding processes should not result in the creation of zombie processes. (Commands to run in the foreground do not have an ‘&’, and commands that run in the background do)

 

Print a custom prompt which supports printing the current directory, user name, cur-rent date, and current time.

 

(Bonus) Allowing $-sign expansion. See the last sample command in the list of com-mands for example.

 

Write a report describing your design for piping, redirection and other special techniques that you are unique to your implementation. No need to restate the obvious.

 

Figure #4: Simple Shell Grammar

 

 

valid string = unix command jj unix command AMP jj special command

 

 

unix command = command name ARGS jj unix command REDIRECTION lename jj unix command PIPE unix command

 

 

special command = cd DIRECTORY jj exit

 

 

command name = any valid executable/interpreted le name

 

 

AMP=&

 

ARG = string

 

ARGS = ARG ARGS jj ARG

 

DIRECTORY = absolute path jj relative path

 

PIPE = j

 

REDIRECTION = < jj >

 

 

  Figure #4: Some Sample Commands
 
echo Here starts our shell …
echo e “<<<<< This message contains a line feed >>>>>nn”
echo <<<<< Start to exercise pipe >>>>>

 

 

echo “<<<<< IO redirection >>>>>

 

ps > test.txt

 

3

 

CSCE 313             Spring 2020
grep pts < test.txt              
pwd > pwd.txt              
mv pwd.txt newpwd.txt            
cat newpwd.txt              
echo “<<<<< 1 pipe >>>>>            
ps a j head  5              
echo “<<<<< 2 pipes >>>>>            
ps a j awk ‘/pts/[0 9]/fprint $1gj tail 5      
ps a j awk ‘/pts/[0 9]/fprint $2gj tail 2      
echo “<<<<< 3 pipes >>>>> j     j    
ls l /proc/sys j awk ‘fprint $9g sort r head 5
ls l /proc/sys j awk ‘fprint $8g j head 3 j sort r
ls l /proc/sys j awk ‘fprint $8$9gj head 10 j sort  
echo “<<<<< 4 pipes with I/O redirection >>>>>  
ls l /proc/sys > test.txt   10 j head   8 j head  7 j sort > output.txt
awk ‘fprint $8$9g< test.txt j head  
cat output.txt              

 

 

echo “<<<<< Background process (ps after some time to make sure the bg’s are not defunct) >>>>>

 

  • if =/dev/zero of=/dev/null bs=1024 count=10485760 & sleep 10 &

 

ps

 

echo “<<<<< Directory Operations >>>>>

 

cd /home/ugrads/

 

pwd

 

cd

 

pwd

 

echo “<<<<< Miscellenous >>>>>

 

rm newpwd.txt

 

jobs

 

sleep 10

 

echo “<<<<< Bonus ($  sign expansion) >>>>>

 

cat /proc/$(psjgrep bashjhead 1jawk ‘fprint $1g‘)/status

 

 

 

 

 

 

 

 

4