0x16. C - Simple Shell

A shell, also known as a command interpreter, is a program that provides a command-line interface for users to interact with the operating system. It acts as an intermediary between the user and the operating system, interpreting and executing commands entered by the user.

An alx team project where each team are required to create a command line interpreter that takes a text command and execute other program in response to thet command


Contributors

Name Email
Lerato Mgwangqa ivyratermgwangqa@gmail.com
Francis Eze fhrancorey99@gmail.com

CODING YOUR OWN SHELL

Contents

  1. PID & PPID
  2. Creating a Simple Shell
  3. Executing Programs with execve
  4. Creating Processes with fork
  5. Synchronizing Processes with wait
  6. Working with Environment Variables
  7. Output
  8. Compilation
  9. Testing

PID & PPID

  • Understanding process IDs (PID) and parent process IDs (PPID)
  • Retrieving process IDs using system calls

Creating a Simple Shell

  • Implementing a simple shell interpreter based on bash and sh
  • Parsing user commands and executing them

Executing Programs with execve

  • Using the execve system call to execute another program
  • Replacing the current process with a new program

Creating Processes with fork

  • Understanding the fork system call
  • Creating child processes from a parent process

Synchronizing Processes with wait

  • Using the wait system call to suspend the execution of the parent process
  • Waiting for child processes to terminate before continuing

Working with Environment Variables

  • Accessing and manipulating environment variables in shell programs
  • Retrieving the environment using the environ variable
  • Modifying environment variables using custom functions

Output

  • The shell have exact same output as the sh (/bin/sh)
  • The only difference is when you print an error, the name of the program must be equivalent to your argv[0] (See below)
Example of an error in sh
$ echo "qwerty" | /bin/sh
/bin/sh: 1: qwerty: not found
$ echo "qwerty" | /bin/../bin/sh
/bin/../bin/sh: 1: qwerty: not found
$
Example of an error on the program hsh
$ echo "qwerty" | ./hsh
./hsh: 1: qwerty: not found
$ echo "qwerty" | ./././hsh
./././hsh: 1: qwerty: not found
$

Compilation

The program should be compiled with

gcc -Wall -Werror -Wextra -pedantic -std=gnu89 *.c -o hsh


Testing

Your code should work like this in interactive mode
$ ./hsh
($) /bin/ls
hsh main.c shell.c
($)
($) exit
$
But also in non-interactive mode
$ 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
$