Skip to content

solp22/holbertonschool-simple_shell

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

70 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Holberton School Simple Shell Project

This is the second group project, carried out by Holberton students. The goal of this assignment is to replicate some of the basic functionalities of the standard Unix shell, including executing commands and running scripts. It also encourages group and team work with a randomly assigned partner.

A Unix shell is a command-line interpreter or shell that provides a command line user interface for Unix-like operating systems. The shell is both an interactive command language and a scripting language, and is used by the operating system to control the execution of the system using shell scripts.

Project Requirements

  • Allowed editors: vi, vim, emacs
  • All files will be compiled on Ubuntu 20.04 LTS using gcc, using the options -Wall -Werror -Wextra -pedantic -std=gnu89
  • All files should end with a new line
  • A README.md file, at the root of the folder of the project is mandatory
  • The code should use the Betty style. It will be checked using betty-style.pl and betty-doc.pl
  • Your shell should not have any memory leaks
  • It is not allowed to use global variables
  • No more than 5 functions per file
  • All your header files should be include guarded
  • Use system calls only when you need to

How does it work?

Our shell reads commands provided by a user through Standard Input Stream and attempts to execute them by means of low level system procedures. This is by using built-in functions, or searching for executable programs in the operating system.

The shell has two operating modes: Interactive and Non-interactive.

In Interactive mode, the shell will display a prompt (★) indicating that the user can write and execute a command. After the command is run, the prompt will appear again and wait for a new command. This can go indefinitely as long as the user does not exit the shell.

In Non-interactive mode, the shell will need to be run with a command input piped into its execution so that the command is run as soon as the Shell starts. In this mode no prompt will appear, and no further input will be expected from the user.

In both modes, commands can be written with their absolute path or simply their executable name if they exist in one of the folders defined by the environment variable PATH or as built-in functions of the Shell.

Functions and system calls used

  • access (man 2 access)
  • execve (man 2 execve)
  • exit (man 3 exit)
  • fork (man 2 fork)
  • free (man 3 free)
  • getline (man 3 getline)
  • isatty (man 3 isatty)
  • malloc (man 3 malloc)
  • perror (man 3 perror)
  • printf (man 3 printf)
  • sprintf (man 3 sprintf)
  • strtok (man 3 strtok)
  • strdup (man 3 strdup)
  • strlen (man 3 strlen)
  • strcmp (man 3 strcmp)
  • strncmp (man 3 strncmp)
  • waitpid (man 2 waitpid)

Usage

To use this shell clone this repository and compile the files in the following way:

git clone https://github.com/solp22/holbertonschool-simple_shell.git
gcc -Wall -Werror -Wextra -pedantic -std=gnu89 *.c -o hsh

Using interactive mode it should work this way:

$ ./hsh
★ /bin/ls
hsh main.c shell.c
★

And using non-interactive mode it should work this way:

$ 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
$

Built-ins

You can type 'exit' to exit the shell like so:

$ ./hsh
★ exit
$

And you can also type 'env' to print a list of all current environment variables:

$ ./hsh
★ env
HOSTNAME=41e118c3a4d9
LANGUAGE=en_US:en
PWD=/holbertonschool-simple_shell
TZ=America/Los_Angeles
HOME=/root
LANG=en_US.UTF-8
LESSCLOSE=/usr/bin/lesspipe %s %s
TERM=xterm
LESSOPEN=| /usr/bin/lesspipe %s
SHLVL=1
LC_ALL=en_US.UTF-8
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
OLDPWD=/
_=./hsh
★

Files

This file contains the main code of the shell. It reads the input given and passes it to the other functions to be tokenized, handle the command's path and finally, being executed. It also handles cases such as the exit built-in, sending a NULL input, and giving a NULL path.

This file contains the functions that are vital for handling the path.

  • _getenv - This function searches for an environment variable by iterating through the environ array and comparing each element to the specified string. If a match is found, the function returns a pointer to the value of the variable. If no match is found, the function returns NULL.
  • _which - This function tokenizes the path's value and stores it in an array. It then appends the command to each directory to find which is the correct path for executing the command, it it finds an executable path it returns the value, if not, it returns NULL.
  • print_env - This function is in charge of the env built-in. When the user passes 'env', the shell prints a list of all current environment variables.

This file contains a function that is in charge of tokenizing strings depending on the delimiter given, and returns an array with each token.

This file has the functions that will enable the command to be executed.

  • execute_command - executes the command by using the fork system call to create a child process and then calling the execve function. The parent function will simply wait for the child to finish its process by using the system call waitpid.
  • execute_command_ap - handles the execution of a command when the absolute path is given.

This file has a simple function to free an array and make it easier to save lines of code.

This file contains all the libraries used, as well as the environ variable and the prototypes of each function.

Flowchart

Untitled Diagram

Authors:

Sol Puente

Mariana Echeto

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages