- What is the shell?
- About this project
- Essential Functionalities of the Simple Shell
- File description
- List of allowed functions and system calls for this project
- USAGE
- Example of Usage
- Bugs
- TEAM
The shell is a program that takes commands from the keyboard via the terminal, and gives them to the operating system to perform.
To better understand how the shell actually works, you can read our [Article].
This project is a simple version of the linux shell made for [Holberton School] taking part of the "Low-level programming & Algorithm - Linux and Unix system programming" projects.
It is created using the C programming Language and it can do many functionalities that a real shell does.
Displays a prompt "#cisfun$ " and waits for user input.
Runs all commands of type "executable program" (ls and /bin/ls).
Runs the following build_in commands: exit, env, setenv and unsetenv.
Handles commands with arguments.
Handles the PATH global variable.
Handles The EOF (End Of File) condition.
Handles the Ctrl + C signal -> It doesn't exit the shell
- AUTHORS -> List of contributors to this repository
- man_1_simple_shell -> Manual page for the simple_shell
- shell.h -> Header file
- shell.c -> main function
- sig_handler -> handles the Ctrl + C signal
- _EOF -> handles the End Of File condition
- string.c
- _putchar -> prints a character
- _puts -> prints a string
- _strlen -> gives the length of a string
- _strdup -> copies a string in a newly allocated memory
- concat_all -> concatenates 3 strings in a newly allocated memory
- line_exec.c
- splitstring -> splits a string into an array of words
- execute -> executes a command using execve
- realloc -> reallocates a memory block
- freearv -> frees a 2 dimensional array
- linkpath.c
- _getenv -> returns the value of a global variable
- add_node_end -> adds a node in a singly linked list
- linkpath -> creates a singly linked list for PATH directories
- _which -> finds the pathname of a command
- free_list -> frees the linked list of PATH value
- checkbuild.c
- checkbuild -> checks if a command is a build-in command
- buildin.c
- exitt -> handles the exit buildin command
- _atoi -> converts a string into an integer
- env -> prints the current environment
- _setenv -> Initialize a new global variable, or modify an existing one
- _unsetenv -> remove a global variable. Variable Replacement hsh interprets the $ character for variable replacement.
$ENV_VARIABLE ENV_VARIABLE is substituted with its value.
Example:
$ echo "echo
Example:
$ echo "echo
Example:
$ echo "echo $$" | ./hsh 6494 Comments hsh ignores all words and characters preceeded by a # character on a line.
Example:
$ echo "echo 'hello' #this will be ignored!" | ./hsh 'hello' Operators hsh specially interprets the following operator characters:
; - Command separator Commands separated by a ; are executed sequentially.
Example:
$ echo "echo 'hello' ; echo 'world'" | ./hsh 'hello' 'world' && - AND logical operator command1 && command2: command2 is executed if, and only if, command1 returns an exit status of zero.
Example:
$ echo "error! && echo 'hello'" | ./hsh ./hsh: 1: error!: not found $ echo "echo 'all good' && echo 'hello'" | ./hsh 'all good' 'hello' || - OR logical operator command1 || command2: command2 is executed if, and only if, command1 returns a non-zero exit status.
Example:
$ echo "error! || echo 'but still runs'" | ./hsh ./hsh: 1: error!: not found 'but still runs' The operators && and || have equal precedence, followed by ;.
hsh Builtin Commands cd Usage: cd [DIRECTORY] Changes the current directory of the process to DIRECTORY. If no argument is given, the command is interpreted as cd $HOME. If the argument - is given, the command is interpreted as cd $OLDPWD and the pathname of the new working directory is printed to standad output. If the argument, -- is given, the command is interpreted as cd $OLDPWD but the pathname of the new working directory is not printed. The environment variables PWD and OLDPWD are updated after a change of directory. Example:
$ ./hsh $ pwd /home/projects/alx/simple_shell $ cd ../ $ pwd /home/projects/alx $ cd - $ pwd /home/projects/alx/simple_shell alias Usage: alias [NAME[='VALUE'] ...] Handles aliases. alias: Prints a list of all aliases, one per line, in the form NAME='VALUE'. alias NAME [NAME2 ...]: Prints the aliases NAME, NAME2, etc. one per line, in the form NAME='VALUE'. alias NAME='VALUE' [...]: Defines an alias for each NAME whose VALUE is given. If name is already an alias, its value is replaced with VALUE. Example:
$ ./hsh $ alias show=ls $ show AUTHORS builtins_help_2.c errors.c linkedlist.c shell.h test README.md env_builtins.c getline.c locate.c hsh alias_builtins.c environ.c helper.c main.c split.c builtin.c err_msgs1.c helpers_2.c man_1_simple_shell str_funcs1.c builtins_help_1.c err_msgs2.c input_helpers.c proc_file_comm.c str_funcs2.c exit Usage: exit [STATUS] Exits the shell. The STATUS argument is the integer used to exit the shell. If no argument is given, the command is interpreted as exit 0. Example:
$ ./hsh $ exit env Usage: env Prints the current environment. Example:
$ ./hsh $ env NVM_DIR=/home/projects/.nvm ... setenv Usage: setenv [VARIABLE] [VALUE] Initializes a new environment variable, or modifies an existing one. Upon failure, prints a message to stderr. Example:
$ ./hsh $ setenv NAME Poppy $ echo $NAME Poppy unsetenv Usage: unsetenv [VARIABLE] Removes an environmental variable. Upon failure, prints a message to stderr. Example:
$ ./hsh $ setenv NAME Poppy $ unsetenv NAME $ echo $NAME
$
- access (man 2 access)
- chdir (man 2 chdir)
- close (man 2 close)
- closedir (man 3 closedir)
- execve (man 2 execve)
- exit (man 3 exit)
- _exit (man 2 _exit)
- fflush (man 3 fflush)
- fork (man 2 fork)
- free (man 3 free)
- getcwd (man 3 getcwd)
- getline (man 3 getline)
- isatty (man 3 isatty)
- kill (man 2 kill)
- malloc (man 3 malloc)
- open (man 2 open)
- opendir (man 3 opendir)
- perror (man 3 perror)
- read (man 2 read)
- readdir (man 3 readdir)
- signal (man 2 signal)
- stat (__xstat) (man 2 stat)
- lstat (__lxstat) (man 2 lstat)
- fstat (__fxstat) (man 2 fstat)
- strtok (man 3 strtok)
- wait (man 2 wait)
- waitpid (man 2 waitpid)
- wait3 (man 2 wait3)
- wait4 (man 2 wait4)
- write (man 2 write)
You can try our shell by following these steps:
Step 1: Clone our repository using this command, (you need to have git installed on your machine first)
git clone https://github.com/ayilaraolamilekan/simple_shell
Step 2: Change directory to simple_shell:
cd simple_shell
Step 3: Compile the C files in this way:
gcc -Wall -Werror -Wextra -pedantic *.c -o hsh
Step 4: Run the shell
./hsh
Exiting the shell When you want to exit the shell, you can use one of the following methods:
1: Type the command "exit"
exit
2: Press on Ctrl + D
ubunto@ubuntu:~/Bureau/simple_shell$ gcc -Wall -Wextra -Werror -pedantic *.c -o hsh
ubunto@ubuntu:~/Bureau/simple_shell$ ./hsh
#cisfun$ echo Hello, This is an example
Hello, This is an example
#cisfun$ ls
README.md checkbuild.c line_exec.c shell.c string.c
buildin.c hsh linkpath.c shell.h
#cisfun$ ^C
#cisfun$ ls -l
total 52
-rw-r--r-- 1 ubunto ubunto 3067 Nov 26 04:22 README.md
-rw-r--r-- 1 ubunto ubunto 2183 Nov 24 16:17 buildin.c
-rw-r--r-- 1 ubunto ubunto 574 Nov 24 15:59 checkbuild.c
-rwxr-xr-x 1 ubunto ubunto 18144 Nov 26 04:22 hsh
-rw-r--r-- 1 ubunto ubunto 2091 Nov 24 14:49 line_exec.c
-rw-r--r-- 1 ubunto ubunto 1926 Nov 24 14:30 linkpath.c
-rw-r--r-- 1 ubunto ubunto 951 Nov 24 16:09 shell.c
-rw-r--r-- 1 ubunto ubunto 1351 Nov 24 15:58 shell.h
-rw-r--r-- 1 ubunto ubunto 1727 Nov 24 14:30 string.c
#cisfun$ exit
ubunto@ubuntu:~/Bureau/simple_shell$
What we learned: How a shell works and finds commands Creating, forking and working with processes Executing a program from another program Handling dynamic memory allocation in a large program Pair programming and team work Building a test suite to check our own code
AURTHORS sodiq ayilara ISAIAH RICHMAN