Git Product home page Git Product logo

simple_shell's Introduction

Holberton School logo

Simple Shell project 0x16.c - Sodash -

This is a simple UNIX command interpreter based on bash and Sh.

Overview

Sodashy is a sh-compatible command language interpreter that executes commands read from the standard input or from a file.

Invocation

Usage: Sodash Sodash is started with the standard input connected to the terminal. To start, compile all .c located in this repository by using this command:

gcc -Wall -Werror -Wextra -pedantic *.c -o sodash
./sodash

Sodash is allowed to be invoked interactively and non-interactively. If sodash is invoked with standard input not connected to a terminal, it reads and executes received commands in order.

Example:

$ echo "echo 'holberton'" | ./sodash
'holberton'
$

When sodash is invoked with standard input connected to a terminal (determined by isatty(3), the interactive mode is opened. sodash Will be using the following prompt ^-^ .

Example:

$./sodash
^-^

If a command line argument is invoked, sodash will take that first argument as a file from which to read commands.

Example:

$ cat text
echo 'holberton'
$ ./sodash text
'holberton'
$

Environment

Upon invocation, sodash receives and copies the environment of the parent process in which it was executed. This environment is an array of name-value strings describing variables in the format NAME=VALUE. A few key environmental variables are:

HOME

The home directory of the current user and the default directory argument for the cd builtin command.

$ echo "echo $HOME" | ./sodash
/home/vagrant

PWD

The current working directory as set by the cd command.

$ echo "echo $PWD" | ./sodash
/home/vagrant/holberton/simple_shell

OLDPWD

The previous working directory as set by the cd command.

$ echo "echo $OLDPWD" | ./sodash
/home/vagrant/holberton/bog-062019-test_suite

PATH

A colon-separated list of directories in which the shell looks for commands. A null directory name in the path (represented by any of two adjacent colons, an initial colon, or a trailing colon) indicates the current directory.

$ echo "echo $PATH" | ./sodash
/home/vagrant/.cargo/bin:/home/vagrant/.local/bin:/home/vagrant/.rbenv/plugins/ruby-build/bin:/home/vagrant/.rbenv/shims:/home/vagrant/.rbenv/bin:/home/vagrant/.nvm/versions/node/v10.15.3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/vagrant/.cargo/bin:/home/vagrant/workflow:/home/vagrant/.local/bin

Command Execution

After receiving a command, sodash tokenizes it into words using " " as a delimiter. The first word is considered the command and all remaining words are considered arguments to that command. sodash then proceeds with the following actions:

  1. If the first character of the command is neither a slash (\) nor dot (.), the shell searches for it in the list of shell builtins. If there exists a builtin by that name, the builtin is invoked.
  2. If the first character of the command is none of a slash (\), dot (.), nor builtin, sodash searches each element of the PATH environmental variable for a directory containing an executable file by that name.
  3. If the first character of the command is a slash (\) or dot (.) or either of the above searches was successful, the shell executes the named program with any remaining given arguments in a separate execution environment.

Exit Status

sodash returns the exit status of the last command executed, with zero indicating success and non-zero indicating failure. If a command is not found, the return status is 127; if a command is found but is not executable, the return status is 126. All builtins return zero on success and one or two on incorrect usage (indicated by a corresponding error message).

Signals

While running in interactive mode, sodash ignores the keyboard input ctrl+c. Alternatively, an input of End-Of-File ctrl+d will exit the program.

User hits ctrl+d in the foutrh command.

$ ./sodash
^-^ ^C
^-^ ^C
^-^ ^C
^-^

Variable Replacement

sodash interprets the $ character for variable replacement.

$ENV_VARIABLE

ENV_VARIABLE is substituted with its value.

Example:

$ echo "echo $PWD" | ./sodash
/home/vagrant/holberton/simple_shell

$?

? is substitued with the return value of the last program executed.

Example:

$ echo "echo $?" | ./sodash
0

$$

The second $ is substitued with the current process ID.

Example:

$ echo "echo $$" | ./sodash
3855

Comments

sodash ignores all words and characters preceeded by a # character on a line.

Example:

$ echo "echo 'holberton' #this will be ignored!" | ./sodash
'holberton'

Operators

sodash specially interprets the following operator characters:

; - Command separator

Commands separated by a ; are executed sequentially.

Example:

$ echo "echo 'hello' ; echo 'world'" | ./sodash
'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 'holberton'" | ./sodash
./shellby: 1: error!: not found
$ echo "echo 'my name is' && echo 'holberton'" | ./sodash
'my name is'
'holberton'

|| - OR logical operator

command1 || command2: command2 is executed if, and only if, command1 returns a non-zero exit status.

Example:

$ echo "error! || echo 'wait for it'" | ./sodash
./sodash: 1: error!: not found
'wait for it'

The operators && and || have equal precedence, followed by ;.

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:

$ ./sodash
^-^ pwd
/home/vagrant/holberton/simple_shell
$ cd ../
^-^ pwd
/home/vagrant/holberton
^-^ cd -
^-^ pwd
/home/vagrant/holberton/simple_shell

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:

$ ./sodash
$ exit

env

  • Usage: env
  • Prints the current environment.

Example:

$ ./sodash
$ env
NVM_DIR=/home/vagrant/.nvm
...

setenv

  • Usage: setenv [VARIABLE] [VALUE]
  • Initializes a new environment variable, or modifies an existing one.
  • Upon failure, prints a message to stderr.

Example:

$ ./sodash
$ setenv NAME Holberton
$ echo $NAME
Holberton

unsetenv

  • Usage: unsetenv [VARIABLE]
  • Removes an environmental variable.
  • Upon failure, prints a message to stderr.

Example:

$ ./sodash
$ setenv NAME Holberton
$ unsetenv NAME
$ echo $NAME

$

Authors & Copyrights

More information

Sodash is a simple shell unix command interpreter that is part of the holberton low level programming module at Holberton School and is intended to emulate the basics sh shell. All the information given in this README is based on the sodash and bash man (1) pages.

simple_shell's People

Contributors

edward0rtiz avatar luischaparroc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

simple_shell's Issues

Simple shell 1.0.10

  • Usage: simple_shell [filename]

  • Your shell can take a file as a command line argument

  • The file contains all the commands that your shell should run before exiting

  • The file should contain one command per line

  • In this mode, the shell should not print a prompt and should not read from stdin

Simple shell 1.0.2 CD

Implement the builtin command cd:

  • Changes the current directory of the process.

  • Command syntax: cd [DIRECTORY]

  • If no argument is given to cd the command must be interpreted like cd $HOME

  • You have to handle the command cd -

  • You have to update the environment variable PWD when you change directory

Simple shell 1.0.9 history

  • Implement the history built-in, without any argument

  • The history built-in displays the history list, one command by line, preceded with line numbers (starting at 0)

  • On exit, write the entire history, without line numbers, to a file named .simple_shell_history in the directory $HOME

  • When the shell starts, read the file .simple_shell_history in the directory $HOME if it exists, and set the first line number to the total number of lines in the file modulo 4096

Simple shell 1.0.5 alias

  • Implement the alias builtin command

  • Usage: alias [name[='value'] ...]

  • alias: Prints a list of all aliases, one per line, in the form name='value'

  • alias name [name2 ...]: Prints the aliases name, name2, etc 1 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, replaces its value with value

Simple shell 1.0

  • Implement the env built-in, that prints the current environment

Simple shell 0.4.1

  • handle arguments for the built-in exit

  • Usage: exit status, where status is an integer used to exit the shell

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

Simple shell 1.0.1 setenv, unsetenv

Implement the setenv and unsetenv builtin commands

setenv

  • Initialize a new environment variable, or modify an existing one

  • Command syntax: setenv VARIABLE VALUE

  • Should print something on stderr on failure

unsetenv

  • Remove an environment variable

  • Command syntax: unsetenv VARIABLE

  • Should print something on stderr on failure

Simple shell 0.1.1

  • Write your own getline function

  • Use a buffer to read many chars at once and call the least possible the read system call

  • You will need to use static variables

  • You are not allowed to use getline

Simple shell 0.1

  • 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, no semicolons, no pipes, no redirections or any other advanced features.

  • The command lines are made only of one word. No arguments will be passed to programs.

  • If an executable cannot be found, print an error message and display the prompt again.

  • Handle errors.

  • You have to handle the “end of file” condition (Ctrl+D)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.