The "hsh" shell works by following these steps:
-
The user launches the shell by running the compiled executable.
-
The shell displays a prompt (e.g.,
$
) indicating that it is ready to receive a command. -
The user enters a command, which is read by the shell.
-
The shell tokenizes the command into individual words, separating them based on whitespace characters.
-
The shell checks if the command is a built-in command. If it is, the corresponding built-in function is executed.
-
If the command is not a built-in, the shell searches for the command in the directories specified by the
PATH
environment variable. If the command is found, it is executed. -
If the command is not found in the directories specified by
PATH
, an error message is displayed. -
The shell waits for the execution of the command to complete.
-
The shell prints the prompt again, indicating that it is ready to receive the next command.
-
The above steps are repeated until the user exits the shell.
Additionally, the shell handles signals such as Ctrl+C
(SIGINT) to ignore the keyboard interrupt and Ctrl+D
(EOF) to exit the shell.
The "hsh" shell provides the following features:
- Execution of system commands.
- Handling command line arguments.
- Environment variable replacement.
- Variable assignment and substitution.
- Control operator (
;
) for command sequencing. - Built-in commands, including
exit
andenv
. - Ignoring keyboard interrupts (SIGINT) with
Ctrl+C
. - Exiting the shell using
Ctrl+D
(EOF).
The "hsh" shell supports the following built-in commands:
exit [STATUS]
: Exit the shell with an optional status.env
: Display the current environment variables.
For more details on the usage and behavior of each built-in command, refer to the Usage section or consult the shell's help using the help
command.
To use the "hsh" shell, follow these steps:
-
Launch the shell by running the compiled executable.
-
The shell will display a prompt (e.g.,
$
). -
Enter a command and press Enter to execute it.
-
The shell will execute the command and display the output, if any.
-
Enter another command or use any of the supported features.
-
To exit the shell, type
exit
and press Enter, or pressCtrl+D
.
Here's an example of how to use the "hsh" shell for executing commands and utilizing its features:
-
Start the shell by running the executable:
$ ./hsh
-
The shell will display a prompt indicating it's ready to accept commands:
$
-
Execute a system command, such as listing the files in the current directory:
$ ls file1.txt file2.txt directory
-
Verify the current directory has changed:
$ pwd /path/to/directory
-
Display environment variables using the
env
command:$ env PATH=/usr/bin:/usr/local/bin:/usr/sbin HOME=/home/user ...
-
Exit the shell:
$ exit
The "hsh" shell can also be used in non-interactive mode, where commands are read from a file or provided directly as input. Here's an example of using the shell in non-interactive mode:
-
Create a file named
commands.txt
containing the commands you want to execute:$ echo "ls" > commands.txt $ echo "pwd" >> commands.txt $ echo "echo Hello, world!" >> commands.txt
-
Run the "hsh" shell with the input file:
$ ./hsh commands.txt
The shell will read the commands from the file and execute them sequentially.
-
The output will be displayed for each command:
file1.txt file2.txt directory /path/to/current/directory Hello, world!
You can directly pass commands as input using command substitution or by piping the commands from another command's output. Here's an example:
-
Using command substitution:
$ ./hsh <<< "ls -l"
This command passes the
ls -l
command as input using command substitution (<<<
). The shell will execute the command and display the output. -
Piping commands from another command's output:
$ echo "pwd" | ./hsh
This command pipes the
echo "pwd"
command's output as input to the "hsh" shell. The shell will execute the command and display the output.
Both methods allow you to provide commands directly as input without the need for an intermediate file. You can customize the commands as per your requirements, and the shell will execute them accordingly.
This is just a brief example of the "hsh" shell usage. Feel free to explore more commands, functionalities, and features provided by the shell. Refer to the documentation or source code for detailed instructions and additional information.
Contributions to the "hsh" shell project are welcome and encouraged. If you would like to contribute, please follow these steps:
- Fork the repository.
- Create a new branch for your contribution.
- Make the necessary changes and commit them.
- Push your
For any questions, issues, or feedback, please contact us at Mahmoud Hamdy or Mohamed Elshafae
Enjoy using our "hsh" shell ^_^! simple_shell