CASH is a Linux BASH like shell written in C. It implements all shell commands and supports pipelining, redirection and chaining.
- Run
make
command to compile and link the shell files. - Run
./shell
to execute the shell.
-
Many commands are inbuilt which are explained in the following section. All the other commands which are to be executed in foreground are implemented using
execvp
. So, all the commands available in Bash can be executed in Cash. -
The prompt for taking the next command displays the
username
,hostname
and thepresent working directory
. The directory in which the shell is run is taken to be the home directory~
of the shell. The present working directory is displayed either in terms of~
(if~
is its ancesstor) or as absolute path. -
Cash provides process management by allowing to control between background and foreground processes and switch between them. The background process control is provided using
fg
,bg
and&
, which is explained in detail later. -
Signals like
SIGSTP
( Ctrl+
Z ) andSIGINT
( Ctrl+
C ) are handled using appropriate signal handlers which is explained later. -
Piping and Redirection are provided and handled as Bash handles them. However, as of now, background process is not supported by piping. Redirection supports background processes.
-
EXIT CODE
(success or failure) of the previous command is displayed along the prompt for the next command. :') denotes success and :'( denotes failure. -
The aforementioned
EXIT CODE
can be computed for multiple commands using chaining of commands usingAND
( @ ) andOR
( $ ), which calculate the resultantEXIT CODE
by applyingAND
andOR
operations on the commands. -
The shell supports semi-colon ; seperated commands. In this case, the
EXIT CODE
is determined by the last command in the sequence.
-
echo
- Implemented in
echo.c
- Takes a string argument and prints it after removing the extra spaces. Does not support
$env_var
.
- Implemented in
-
pwd
- Implemented in
pwd.c
- Prints the current working directory.
- Uses the
getcwd()
system call.
- Implemented in
-
cd [location]
- Implemented in
cd.c
- Changes the current working directory to the mentioned directory. If no parameter is given, it changes the directory to the root directory of the shell.
- If
~
is present in the givenlocation
, it is replaced with the home directory of the shell. - If
location
is-
, it is interpreted as the previous working directory of the shell. - Implemented using
chdir()
system call.
- Implemented in
-
ls [-l -a -al -la] [Directory]
- Implemented in
ls.c
- Lists all the files and directories in the mentioned directory/directories. If no parameters are passed, lists the contents of current directory.
-l
flag lists the long format ofls
, providing additional details such as permissions, owner, time of creation etc.-a
flag includes the hidden files/diectories in the listing.- The flags and directories can be provided in any order.
- Uses the
readdir()
system call.
- Implemented in
-
pinfo [process_id]
- Implemented in
pinfo.c
- Gives the information about
process_id
process. Ifprocess_id
not mentioned, gives information about the current process. - The information includes Process ID, Process Name, State of the process and the exceutable path of the process.
- Uses the files
/proc/process_id/status
and/proc/process_id/exec
to fetch the required information.
- Implemented in
-
history [num]
- Implemented in
history.c
- Gives the
num
number of previous commands run. Ifnum
is not mentioned, 10 is taken as the default value fornum
. - Continous repetitions and blank lines are avoided in the history.
- Implemented in
-
nightswatch -n [seconds] [interrupts/newborn]
- Implemented in
nightwatch.c
- If
interrupts
, gives the number of keyboard interrupts, else ifnewborn
gives the newest process. - Gives the results at an interval of
seconds
, till q + Enter is pressed. - For
interrupts
, the file/proc/interrupts
is used. - For
newborn
, the file/proc/loadavg
is used.
- Implemented in
-
setenv [var] [val]
- Implemented in
execute_inst.c
- Creates the environment variable
var
and assigns the valueval
to it. - If
val
is not provided, it is interpreted asval = NULL
. - Uses the
setenv()
function.
- Implemented in
-
unsetenv [var]
- Implemeted in
execute_inst.c
- Deletes the environment variable named
val
along with its value. - Uses the
unsetenv()
function.
- Implemeted in
-
jobs
- Implemented in
jobs.c
- Lists all the background process along with their
job_id
,pid
andstatus
(Running or Stopped). - Information required is taken from
/proc/pid/stat
file.
- Implemented in
-
kjob [job_id] [signal]
- Implemented in
kjob.c
- Sends
signal
to the process with Job IDjob_id
. - Uses the
kill()
system call.
- Implemented in
-
fg [job_id]
- Implemented in
fg.c
- Makes a stopped background process with Job ID
job_id
continue as a foreground process.
- Implemented in
-
bg [job_id]
- Implemented in
bg.c
- Continous a stopped background process in the background.
- Implemented in
-
overkill
- Implemented in
overkill.c
- Terminates all background processes.
- Uses the
kill()
system call withSIGKILL
.
- Implemented in
-
quit
- Implemented in
quit.c
- Quits the terminal.
- Use this command to ensure proper closing (killing all persisting background processes).
- Ctrl + D provides the same functionality.
- Implemented in
-
Any command ending with
&
is treated as a background process the shell does not wait for its execution. If such a process requests terminal control, it will automatically suspended in the background. The shell keeps track of all background processes and alerts the user on their completion. -
| is used for piping of commands, i.e, Output of one command serves as input for the next.
Example:username@hostname $ grep "new" temp.txt | sort | wc
-
< is used for input redirection. > (for overwriting) and >> (for apending) are used for output redirection.
Example:# Input Redirection username@hostname $ diff file1.txt file2.txt > output.txt # Output Redirection username@hostname $ sort < lines.txt # Input and Output Redirection username@hostname $ sort < lines.txt >> sortedlines.txt
-
Commands are chained using logical
AND
andOR
operators, such that theEXIT CODE
of the entire chain is the logicalAND
orOR
(respectively) of the individual exit codes. These operators short circuit, have equal precedence and are evaluated left-to-right.
Example::') username@hostname > ls $ echo penguins @ echo fail oswald henry pingu fail :') username@hostname > ps -Q $ ps -Q @ ps error: unsupported SysV option error: unsupported SysV option ..... :'( username@hostname >
-
Ctrl + C sends the
SIGINT
signal and terminates any foreground process. -
Ctrl + Z sends the
SIGSTP
signal and suspends any running foreground process. -
Ctrl + D is an
EOF
character and terminates the shell. -
The order of precedence (the order in which the command is broken down is as follows):
; > @,$ (Logical) > Piping (|) > (<,>,>>) Redirection > Background(&)
-
piping.c
Contains the implementation of piping usingpipe
system call anddup
system call. -
redirection.c
Contains the implementation of input redirection and output redirection in two functions. Uses thedup
anddup2
function calls. -
chaining.c
Contains the implementation of command chaining which binds looser than;
but stronger than|,>,<,>> and &
. -
background.c
Implementation of background processes indentified by&
. Creates a new child process usingfork()
and the shell does not wait for its completion. -
basic.c
Conatins some basic functions such asget_val()
for getting the values ofusername
,hostname
andhome directory
of the shell,prompt()
for displaying the prompt andinc_tilda
for substituting the home directory in all the in-built commands which uselocation
. -
execute_inst.c
All commands are processed and corresponding functions called using the functionexecute_inst()
. Also contains aremove_spaces()
for basic string manipulation and space removal. -
input.c
Takes inputget_input()
, processes it (tokenizes on the basis of;
)process_input
(Calls the chaining function) and executes the input after receiving individial instructions (execute_input()
). -
history.c
Along with the implementation of history command, it also containswrite_history
andread_history
which write the history to a file.history.txt
and read from it respectively. This ensures that history is preserved across sessions. -
main.c
Contains themain()
. The shell execution starts from here. -
signal_handlers.c
Contains the signal handlers forSIGCHLD
(used to keep track of background processes),SIGINT
andSIGTSTP
signals. -
header.h
Contains all required headers and global variables
- Arrow keys for command-recall
- Tab for auto completion
&
for piped processes.- Environment variables support
- Aliasing
......