Wish (short for Wisconsin Shell) is a simple Unix command-line interpreter (CLI), or shell, that mimics the basic functionality of common Unix shells such as bash. This project was designed to familiarize myself with Linux programming, process creation and management, and the essential functionality of shells.
- Interactive mode: The shell operates interactively, allowing users to type commands one at a time.
- Batch mode: The shell can read commands from a file and execute them sequentially.
- Built-in commands: Supports the following built-in commands:
- exit: Terminates the shell.
- cd : Changes the current working directory.
- path ...: Modifies the search path for finding executables.
- Executable commands: The shell searches for executables in the specified directories and executes them.
- Redirection: Allows the redirection of output to a file using the > operator.
- Parallel execution: Supports parallel execution of multiple commands using the & operator.
- Error handling: Any error encountered is reported through a uniform error message.
The Wish shell repeatedly:
- Prints a prompt (wish> ) for user input.
- Reads a command line.
- Parses the command and its arguments.
- Executes the command by either invoking a built-in function or creating a child process using fork() and execv().
- Waits for the command to finish unless it's a parallel command.
- Commands are searched for in the directories specified in the shell's path. The shell starts with /bin/ as the default path.
Run the shell by invoking it without arguments:
./wish
You will see the prompt wish> , after which you can type commands like:
wish> ls -la
wish> cd /usr/local
wish> path /bin /usr/bin
wish> ls > output.txt
wish> cmd1 & cmd2 & cmd3
wish> exit
To run the shell in batch mode, pass a filename containing a list of commands as an argument:
./wish batch.txt
The shell will execute each command in the file sequentially without displaying the prompt.
- exit: Terminates the shell.
- cd : Changes the current working directory. Errors occur if the directory does not exist or if multiple arguments are provided.
- path ...: Modifies the search path for executables.
Use the > operator to redirect the output of a command to a file:
wish> ls -la > output.txt
Use the & operator to execute multiple commands in parallel:
wish> cmd1 & cmd2 args & cmd3 args
The shell will run all commands in parallel and wait for them to complete before returning to the prompt.
If an error occurs (e.g., invalid syntax or failure to execute a command), the shell prints the following error message to stderr:
An error has occurred
The shell continues executing unless the error is critical (e.g., too many arguments on startup or failure to open a batch file).
To compile the shell, use the following command:
g++ -o wish wish.cpp -Wall -Werror
note: -g to enable debugging with gdb