EasyShell is a basic bash-like shell implemented in C that that provides a subset of features from bash. It features include running commands, I/O redirection, background processing, and built-in signal handling.
- Prompt for Commands: Provides a colon (
:) as the prompt for user input. - Command Execution: Runs any valid command using
fork(),exec(), and/orwaitpid(). - Built-in Commands:
exit: Exits the shell, terminating all child processes.cd: Changes the current working directory.status: Displays the exit status or the signal of the last foreground process.
- Input and Output Redirection: Supports both input and output redirection using
<and>. - Foreground and Background Execution: Runs commands in the foreground or background depending on the presence of
&. - Signal Handling:
SIGINT(Ctrl+C): Terminates foreground processes but does not affect the shell.SIGTSTP(Ctrl+Z): Toggles between allowing and disallowing background execution.
The shell uses : as the prompt. The general command format is:
command [arg1 arg2 ...] [< input_file] [> output_file] [&]- Commands are words separated by spaces.
- Redirection symbols
<,>, and&must be surrounded by spaces. $$(PID expansion)replaces instances of$$with the process ID of the shell.
Example:
ls -l > output.txt &Exit the shell and terminate all background processes.
exitChange the current directory. Without arguments, changes to the home directory.
cd /path/to/directoryDisplays the exit status or terminating signal of the last foreground command.
statusRedirect input and output of commands using < and >. For example, to read from input.txt and write to output.txt:
sort < input.txt > output.txt- Foreground: Default for commands without
&. The shell waits for these commands to finish.sleep 5
- Background: Append
&to run commands in the background.sleep 5 &
- Foreground processes are terminated.
- Background processes and the shell ignore the signal.
- Toggles background execution. When background execution is disabled, commands with
&run in the foreground.
Message for background suspension:
Entering foreground-only mode (& is now ignored)Message to resume background execution:
Exiting foreground-only modeTo compile EasyShell, use the following command:
gcc -std=gnu99 main.c -o EasyShellRun the shell by executing:
./EasyShell: ls -l > directory_listing.txt
: cat < directory_listing.txt
: sleep 10 &
: status
: exit