Skip to content

Latest commit

 

History

12 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

OShell

A minimal Unix-like command shell implemented in C.

✅ Features

  • Built-in commands: exit, cd, env, setenv, unsetenv, alias, path, man 🔧
  • Alias support with expansion and automatic alias creation for successfully executed external commands ✨
  • Command sequencing (;), conditionals (&&, ||), parallel execution (&) and output redirection (>) 🔗
  • Interactive mode (prompt) and batch mode (run commands from a file, return last command's status) 📝
  • Signal handling: interactive shell ignores Ctrl+C (SIGINT) while child processes get default behavior; Ctrl+D (EOF) exits the shell 📵
  • Manual included (specialized man description) 📓
  • Error handling 🪄 + White Space handling

Requirements

  • POSIX-like environment (Linux, macOS) or compatible toolchain
  • GCC or Clang
  • make (optional)

Hawassa University

Operating System Project

Group Members

* Student Name-------------------------- Id
- Astawus Amsalu ------------------ NaScR/1352/16
- ABREHAM ENGIDA ------------------ NaScR/1142/16
- MESERET TAKELE ------------------ NaScR/2919/16
- HAWI KEBEDE --------------------- NaScR/2345/16
- BESUFIKAD TILAHUN --------------- NaScR/1499/16

Compilation instruction ( Build )

From the project root:

make
# or compile manually
gcc -std=c11 -Wall -Wextra -o oshell code/*.c 

#better to compile with make useing provided Makefile

Execution instructions ( Usage )

Interactive:

./oshell
$  # prompt appears

Batch (run commands from a file):

./oshell myscript.sh
# No prompt is printed; the program exits with the last command's exit status

Aliases

  • Create or update an alias:
alias ll='ls -l'
  • List aliases:
alias    # prints all
alias ll # prints the `ll` alias
  • Alias expansion details:
    • Alias values may be quoted with single or double quotes.
    • Only the first token is expanded and any remaining arguments are appended to the expanded command.
    • Recursive expansions are limited to 10 levels to avoid infinite loops.
    • Automatic alias creation: when an external command is executed successfully, OShell creates an alias mapping the command name to the resolved full path (e.g., ls → /bin/ls) if not already present.

Note: There is no unalias builtin yet; you can overwrite an alias by re-defining it.

Signals / Keyboard behavior

  • Ctrl+C (SIGINT): ignored by the interactive shell itself (so it won't exit the shell). Child processes restore default SIGINT and therefore can be interrupted.
  • Ctrl+D (EOF): ends interactive session and exits the shell gracefully.

Quick manual tests

  • Builtins affecting shell process:

    • cd /tmp then pwd should show /tmp.
    • setenv TEST yes then env | grep TEST should show TEST=yes.
    • exit 5 should exit the shell with code 5 (for batch files, check return value).
  • Aliases:

    • alias ll='ls -l' → ll /tmp should list /tmp in long format.
    • Run ls and then alias ls should show a mapping to the resolved path.
  • Redirection and builtin interaction:

    • echo hi > out.txt creates out.txt and the shell should continue to print normally after.
  • Signals:

    • Start a long-running command: sleep 10 and press Ctrl+C to interrupt the child but remain in shell.
    • Press Ctrl+D at the prompt to exit.

File Structure

  • Key sources:
    • code/main.c — entrypoint and interactive/batch handling
    • code/parser.c — tokenization, parsing, execution, redirection, and conditionals
    • code/builtin.c, code/builtin.h — builtins and alias storage
    • code/main.c - shell entry point
    • Makefile - easy compilation
  • parser.h exports parse_line() which now returns an integer exit status for use in batch files.
  • Aliases are stored in an in-memory table; persistence and unalias can be added later.

Quick tests

  • Builtins affecting shell process:

    • cd /tmp then pwd shows /tmp.
    • setenv TEST yes then env | grep TEST shows TEST=yes.
    • exit 5 exits the shell with code 5 (for batch files, check return value).
  • Aliases:

    • alias ll='ls -l' → ll /tmp lists /tmp in long format.
    • Run ls and then alias ls shows a mapping to the resolved path.
  • Redirection and builtin interaction:

    • echo hi > out.txt creates out.txt and the shell continue to print normally after.
  • Signals:

    • Start a long-running command: sleep 10 and press Ctrl+C to interrupt the child but remain in shell.
    • Press Ctrl+D at the prompt to exit.

Contributing

About

A minimal Unix-like command shell implemented in C.

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages