Skip to content

simple_shell is a project on the UNIX Shell. The 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.…

Notifications You must be signed in to change notification settings

Mohamed-Mostafaaa/simple_shell

Repository files navigation

Implementation of sh in C programming language

simple_shell

Alx School

Overview

simple_shell is a project on the UNIX Shell. The 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.…

access, execve, exit, fork, free, malloc, read, signal, wait, write.

Content

Installation

git clone https://github.com/Mohamed-Mostafaaa/simple_shell.git
cd simple_shell
gcc *.c -o hsh

Compilation

gcc -Wall -Werror -Wextra -pedantic *.c -o hsh 

Testing

  • Interactive mode:
$ ./hsh
($) /bin/ls
hsh main.c shell.c
($)
($) exit
$

General Requirements

  • Allowed editors: vi, vim, emacs
  • All your files will be compiled on Ubuntu 14.04 LTS
  • Your C programs and functions will be compiled with gcc 4.8.4 using the flags -Wall -Werror -Wextra and -pedantic
  • All your files should end with a new line
  • A README.md file, at the root of the folder of the project is mandatory
  • Your code should use the Betty style. It will be checked using betty-style.pl and betty-doc.pl
  • No more than 5 functions per file
  • All your header files should be included guarded
  • Use system calls only when you need to

Target output

  • Unless specified otherwise, your program must have the same output as sh (/bin/sh) and the same error output.
  • The only difference is when you print an error, the name of the program must be equivalent to your argv[0]
  • Example of error with sh:
$ echo "qwerty" | /bin/sh
/bin/sh: 1: qwerty: not found
$ echo "qwerty" | /bin/../bin/sh
/bin/../bin/sh: 1: qwerty: not found
$
  • Same error with your program hsh:
$ echo "qwerty" | ./hsh
./hsh: 1: qwerty: not found
$ echo "qwerty" | ./././hsh
./././hsh: 1: qwerty: not found
$

Examples

  1. Absolute path commands
  • noninteractive
$ echo "/bin/pwd" | ./hsh
$ /home/timex/simple_shell
  • interactive mode
$ ./hsh
./hsh$ /bin/echo hello world
hello world
./hsh$ exit
$
  1. short command
  • noninteractive
$ echo "pwd" | ./hsh
$ /home/timex/simple_shell
  • interactive mode
$ ./hsh
./hsh$ echo hello world
hello world
./hsh$ exit
$
  1. built-ins
  • noninteractive
$ echo "exit" | ./hsh
$ echo $?
0
  • interactive mode
$ ./hsh
./hsh$ exit 98
$ echo $?
98

Some error output

$ ./hsh
./hsh$ ls /non_existing_folder
ls: cannot access '/non_existing_folder': No such file or directory
./hsh$ exit
$ echo $?
2
$ echo "non_valid_command" | ./hsh
./hsh: 1: non_valid_command: not found
$ echo $?
127

Tasks

Mandatory:

  1. README, man, AUTHORS
  • Write a README
  • Write a man for your shell.
  1. Simple shell 0.1
  • Write a UNIX command line interpreter.
  • Your Shell should: Display a prompt and wait for the user to type a command. A command line always ends with a new line. The prompt is displayed again each time a command has been executed. The command lines are simple, with no semicolons, pipes, redirections, or other advanced features. The command lines are made only of one word. No arguments will be passed to programs. Print an error message and display the prompt again if an executable cannot be found. Handle errors. You have to handle the “end of file” condition (Ctrl+D)
  • You don’t have to: use the PATH implement built-ins handle special characters: ", ', `, , *, &, # be able to move the cursor handle commands with arguments
  1. Simple shell 0.2
  • Handle command lines with arguments
  1. Simple shell 0.3
  • Handle the PATH
  1. Simple shell 0.4
  • Implement the exit built-in, that exits the shell
  • Usage: exit
  • You don’t have to handle any argument to the built-in exit
  1. Simple shell 1.0
  • Implement the env built-in, that prints the current environment
  1. Write a blog post "What happens when you type ls -l in the shell"

Advanced

  1. Test suite
  • Contribute to a test suite for your shell
  1. Simple shell 0.2.1
  • Write your strtok function
  1. Simple shell 0.4.1
  • handle arguments for the built-in exit
  1. Simple shell 0.4.2
  • Handle Ctrl+C: your shell should not quit when the user inputs ^C
  1. setenv, unsetenv
  • Implement the setenv and unsetenv builtin commands
  1. cd
  • Implement the builtin command cd
  1. ;
  • Handle the commands separator ;
  1. alias
  • Implement the alias builtin command
  1. Comments
  • Handle comments (#)
  1. help
  • Implement the help built-in
  1. File as an input
  • Your shell should take a file as a command line argument

List of allowed functions / syscalls

Functions Reference
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

Authors

About

simple_shell is a project on the UNIX Shell. The 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.…

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages