This project is a simple shell implemented in C that provides a command-line interface for executing commands. It supports built-in commands, process execution, input/output redirection, background processes, and custom signal handling.
-
Command Prompt:
- Displays a prompt for user input.
-
Handling Blank Lines and Comments:
- Ignores empty input.
- Ignores lines beginning with
#
(treated as comments).
-
Built-in Commands:
exit
: Terminates the shell.cd [directory]
: Changes the current working directory.status
: Displays the exit status or termination signal of the last foreground process.
-
Executing Other Commands:
- Uses functions from the
exec()
family to create new processes for executing external commands.
- Uses functions from the
-
Input and Output Redirection:
- Supports
>
for redirecting standard output to a file. - Supports
<
for redirecting standard input from a file.
- Supports
-
Foreground and Background Processes:
- Allows commands to run in the background by appending
&
to the command. - Foreground processes run normally and block further input until completion.
- Allows commands to run in the background by appending
-
Signal Handling:
- SIGINT (Ctrl+C): Custom handler to manage termination of foreground processes.
- SIGTSTP (Ctrl+Z): Custom handler to toggle between normal mode and foreground-only mode.
Use gcc
to compile the program:
gcc -o my_shell my_shell.c
Execute the compiled program:
./my_shell
- Run a command:
ls -l
- Run a command in the background:
sleep 10 &
- Redirect output to a file:
ls > output.txt
- Redirect input from a file:
sort < data.txt
- Change directory:
cd /home/user
- Check the status of the last process:
status
- Exit the shell:
exit
- Background processes print their process ID when started.
- In foreground-only mode (triggered by SIGTSTP), background execution is disabled.
- The
status
command reports exit codes or termination signals of the last foreground process.
Natalie Dettmer