smallsh is a mini shell written in C. It implements a subset of features of well-known shells, such as bash. It handles variable expansion, comments, blank lines, input and output redirection, foreground and background processes, and more.
Table of Contents
- The Command Prompt
- Format:
command [arg1 arg2 ...] [< input_file] [> output_file] [&]
- Use
&
at the end of a command to execute it in the background - Use
>
and<
for standard input or output redirection - Maximum line length is 2048 characters with a maximum of 512 arguments
- Format:
- Comments and Blank Lines
- Lines that begin with # are treated as comments and are ignored
- Blank lines (one without any commands) are ignored
- The shell will re-prompt for another command if it receives a blank line or comment line
- Expansion of the Variable
$$
- The variable
$$
will be expanded into the process ID of smallsh itself
- The variable
- Built-in Commands
- Three built-in commands are supported:
exit
,cd
, andstatus
- All other commands are passed on to a member of the
exec()
family of functions
- Three built-in commands are supported:
- Executing Other Commands
- Non-built-in commands will be forked off into a child process
- Non-built-in commands must be located in the
PATH
variable in order for smallsh to execute them
- Input and Output Redirection
- Input and output redirection uses
dup2()
- Input files redirected via
stdin
are opened for reading only - Output files redirected via
stdout
are opened for writing only stdin
andstdout
for a command can be redirected at the same time
- Input and output redirection uses
- Executing Commands in Foreground and Background
- Commands without
&
at the end will run in the foreground - Commands with
&
at the end will run in the background (except built-in commands which will run in the foreground regardless of an&
at the end of the command) - Background commands will print a process ID of the background process when it begins and a message with the process ID and exit status when the process terminates
- Commands without
- Signals
SIGINT
andSIGTSTP
- A Ctrl-C command from the keyboard will send aSIGINT
signal to smallsh - A child process running in the foreground will terminate itself when it receives aSIGINT
- smallsh itself will ignoreSIGINT
- A Ctrl-Z command from the keyboard will send aSIGTSTP
signal to smallsh - Subsequent commands will no longer run in the background -&
operator will be ignored - A subsequentSIGTSTP
signal will return smallsh to its normal condition
$ smallsh
: ls
junk smallsh smallsh.c
: ls > junk
: status
exit value 0
: cat junk
junk
smallsh
smallsh.c
: wc < junk > junk2
: wc < junk
3 3 23
: test -f badfile
: status
exit value 1
: wc < badfile
cannot open badfile for input
: status
exit value 1
: badfile
badfile: no such file or directory
: sleep 5
^Cterminated by signal 2
: status &
terminated by signal 2
: sleep 15 &
background pid is 4923
: ps
PID TTY TIME CMD
4923 pts/0 00:00:00 sleep
4564 pts/0 00:00:03 bash
4867 pts/0 00:01:32 smallsh
4927 pts/0 00:00:00 ps
:
: # that was a blank command line, this is a comment line
:
background pid 4923 is done: exit value 0
: # the background sleep finally finished
: sleep 30 &
background pid is 4941
: kill -15 4941
background pid 4941 is done: terminated by signal 15
: pwd
/nfs/stak/users/chaudhrn/CS344/prog3
: cd
: pwd
/nfs/stak/users/chaudhrn
: cd CS344
: pwd
/nfs/stak/users/chaudhrn/CS344
: echo 4867
4867
: echo $$
4867
: ^C^Z
Entering foreground-only mode (& is now ignored)
: date
Mon Jan 2 11:24:33 PST 2017
: sleep 5 &
: date
Mon Jan 2 11:24:38 PST 2017
: ^Z
Exiting foreground-only mode
: date
Mon Jan 2 11:24:39 PST 2017
: sleep 5 &
background pid is 4963
: date
Mon Jan 2 11:24:39 PST 2017
: exit
$
- GCC Compiler: https://gcc.gnu.org/
- Clone the repo
git clone https://github.com/cjsidler/smallsh.git
- Build an executable
gcc -std=gnu99 main.c -o smallsh
- Run the executable.
.\smallsh
Contact me via email, LinkedIn, or GitHub: