A simple UNIX command interpreter written in C programming language.
Respond to text commands and function calling
simple_shell
simple_shelluses the executable filehsh- has the exact same output and error as
sh (/bin/sh) simple_shellis able to read user input, parse it and execute the command- can be used in both interactive and non-interactive modes
- interactive mode can be exited using the command
exitorctrl+c
- interactive mode can be exited using the command
- handles command lines with arguments and errors
- displays a prompt and waits for the user to type a command
- commmand line always ends with a new line
- a prompt is displayed again each time a command has been executed
- handles the
PATH - implements the
envbuilt in - simple commands such as
/bin/ls,echoandlsare supported - Betty coding style is followed
Interactive
$ ./hsh
($) /bin/ls
hsh main.c shell.c
($)
($) exit
$
Non-interactive
$ echo "/bin/ls" | ./hsh
hsh main.c shell.c test_ls_2
$
$ cat test_ls_2
/bin/ls
/bin/ls
$
$ cat test_ls_2 | ./hsh
hsh main.c shell.c test_ls_2
hsh main.c shell.c test_ls_2
$
Files supporting the simple_shell
| File | Synopsis | Description | Return |
|---|---|---|---|
main.h |
Header file header file containing functions declarations to support simple_shell |
||
prompt.c |
void prompt(void) |
Shows the shell prompt This function prints a dollar sign and a space ("$ ") to the screen to show the user that the shell is ready for a command. It uses fflush(stdout) to make sure the prompt is shown right away. |
This function does not return anything (void). |
parse_line.c |
void parse_line(char *line, char **arg) |
Splits a line into words (arguments) This function takes a line of text (like a command line) and splits it into separate words using strtok() with DELIM as the separator. Each word is saved in the arg array. It keeps splitting until there are no more words or until it reaches the maximum number of arguments. The last element of arg is set to NULL to mark the end. |
This function does not return anything (void). The arguments are stored in the arg array. |
command.c |
int command(char **arg, char *line, int status) |
Handles built-in shell commands |
Returns 1 if a built-in command was executed Returns 0 otherwise. |
*find_in_path.c |
char *find_in_path(char *cmd) |
Searches for a command in the PATH find_in_path() checks if the given command has a '/' in it. If it does, it treats the command as a path and returns a duplicate of it. If not, it looks through each directory listed in the PATH environment variable to find an executable file with the same name. If it finds one, it returns the full path as a new string. If the command can't be found or there's an error, it returns NULL. |
Returns a new string with the full path to the command if found, or NULL if not. |
_getenv.c |
char _getenv(const char *name) |
Gets the value of an environment variable This function looks through all the environment variables and tries to find one that matches the name you give it. If it finds it, it gives you back a pointer to the value part (the stuff after the = sign). If it can't find it, it just returns NULL. |
Returns a pointer to the value if it finds the variable. Returns NULL if it doesn't exist. |
getline.c |
ssize_t _getline(char lineptr, size_t *n) |
Custom implementation of getline to read a line from stdin. _getline() reads a whole line from standard input (stdin) and puts it into a buffer that you give it with lineptr. If you pass in NULL for lineptr or set *n to 0, the function will create a buffer for you, starting at 1024 bytes. It keeps reading one character at a time until it hits a newline ('\n') or the end of the file (EOF). If the buffer isn't big enough, it gets resized automatically with realloc. The line it reads will always be null-terminated, so you can use it as a regular C string. |
The function returns the number of characters it read, including the newline if there was one, but not counting the null terminator at the end. If something goes wrong (like an error or EOF before reading anything), it returns -1. |
read_input.c |
int read_input(char **line, size_t *len, int interactive) |
Gets a line of input from the user This function reads a line of input from the user. If interactive is true, it shows the prompt first. It uses _getline() to get the input and saves it in *line. If there is an error or the user presses Ctrl+D, it returns -1. If the line ends with a newline character, it replaces it with '\0' to remove it. |
Returns 0 if it reads the input successfully. Returns -1 if there is an error or end of file. |
fork_execve.c |
int fork_execve(char *cmd_path, char **args, char *line) |
Forks and executes a command using execve This function makes a new process using fork(). The child process tries to run the command given by cmd_path with execve(). If execve() fails, it prints an error, frees memory, and stops the child. If fork() fails, it prints an error and returns -1. The parent process waits for the child to finish, frees memory, and returns the child's exit status. |
Returns the child's exit status if the command runs okay. Returns -1 if there is an error with fork() or execve(). |
Advanced tasks
Simple shell 0.1.1 using our own getline function, using buffers, and static variables
Simple shell 0.2.1 shell that does not use strtok
Simple shell 0.4.1 handling arguments for built in exit
Simple shell 0.4.2 handling ctrl+c without quitting shell when user inputs ^C
Simple shell 1.0 + implementing setenv and unsetenv
Simple shell 1.0 + implementing built in command cd
Simple shell 1.0 + handling command separator ;
Simple shell 1.0 + handling logical operators && and ||
Simple shell 1.0 + implimenting built in command alias
Simple shell 1.0 + handling variables replacement such as $? and $$
Simple shell 1.0 + handling comments (#)
Simple shell 1.0 + implimenting built in command alias
Simple shell 1.0 + implementing built in command help
Simple shell 1.0 + impementing built in command history
Simple shell 1.0 + taking a file as a command line argument\
Holberton’s Sandbox - Ubuntu 22.04
Requirements:
- Windows 10
- Chrome 77.0.3865.120 or superior
- 16GB Memory
Steps:
- Clone respository using
https://github.com/JonathanCharalambous/holbertonschool-simple_shell.git - Change directories to simple_shell
cd holbertonschool-simple_shell - Compile the files and create an executabe
gcc -Wall -Werror -Wextra -pedantic *.c -o hsh - Run the simple shell using the following options:
- Interactive
./hsh- Non-interactive
use commandecho "[command][option]" | ./hshexitorctrl+cto leave shell in interactive mode
Everything you need to know to start coding your own shell
Basic writing and formatting syntax
Linux man pages