Giter Club home page Giter Club logo

simple_shell's Introduction

shelltan - Simple Shell ๐Ÿš A simple UNIX command interpreter written as part of the low-level programming and algorithm track at Holberton School.

Description ๐Ÿ’ฌ shelltan is a simple UNIX command language interpreter that reads commands from either a file or standard input and executes them.

Invocation ๐Ÿƒ Usage: shelltan [filename]

To invoke shelltan, compile all .c files in the repository and run the resulting executable:

gcc *.c -o shelltan ./shelltan Shelltan can be invoked both interactively and non-interactively. If shelltan is invoked with standard input not connected to a terminal, it reads and executes received commands in order.

Example:

$ echo "echo 'hello'" | ./shelltan 'hello' $ If shelltan is invoked with standard input connected to a terminal (determined by isatty(3)), an interactive shell is opened. When executing interactively, shelltan displays the prompt $ when it is ready to read a command.

Example:

$./shelltan $ Alternatively, if command line arguments are supplied upon invocation, shelltan treats the first argument as a file from which to read commands. The supplied file should contain one command per line. Shelltan runs each of the commands contained in the file in order before exiting.

Example:

$ cat test echo 'hello' $ ./shelltan test 'hello' $ Environment ๐ŸŒณ Upon invocation, shelltan 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" | ./shelltan /home/vagrant PWD The current working directory as set by the cd command.

$ echo "echo $PWD" | ./shelltan /home/vagrant/holberton/simple_shell OLDPWD The previous working directory as set by the cd command.

$ echo "echo $OLDPWD" | ./shelltan /home/vagrant/holberton/printf 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" | ./shelltan /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, shelltan 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. Shelltan then proceeds with the following actions:

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. If the first character of the command is none of a slash (), dot (.), nor builtin, shelltan searches each element of the PATH environmental variable for a directory containing an executable file by that name. 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 ๐Ÿ‘‹ Shelltan 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, shelltan 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 third line.

$ ./shelltan $ ^C $ ^C $ Variable Replacement ๐Ÿ’ฒ Shelltan interprets the $ character for variable replacement.

$ENV_VARIABLE ENV_VARIABLE is substituted with its value.

Example:

$ echo "echo $PWD" | ./shelltan /home/vagrant/holberton/simple_shell $? ? is substitued with the return value of the last program executed.

Example:

$ echo "echo $?" | ./shelltan 0 $$ The second $ is substitued with the current process ID.

Example:

$ echo "echo $$" | ./shelltan 6494 Comments #๏ธโƒฃ Shelltan ignores all words and characters preceeded by a # character on a line. Example:

$ echo "echo 'hello' #this will be ignored!" | ./shelltan 'hello' Operators ๐ŸŽธ Shelltan specially interprets the following operator characters:

; - Command separator Commands separated by a ; are executed sequentially.

Example:

$ echo "echo 'hello' ; echo 'world'" | ./shelltan '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 'hello'" | ./shelltan ./shelltan: 1: error!: not found $ echo "echo 'all good' && echo 'hello'" | ./shelltan 'all good' 'hello' || - OR logical operator command1 || command2: command2 is executed if, and only if, command1 returns a non-zero exit status.

Example:

$ echo "error! || echo 'but still runs'" | ./shelltan ./shelltan: 1: error!: not found 'but still runs' The operators && and || have equal precedence, followed by ;.

Shelltan 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:

$ ./shelltan $ pwd /home/vagrant/holberton/simple_shell $ cd ../ $ pwd /home/vagrant/holberton $ cd - $ pwd /home/vagrant/holberton/simple_shell $ cd ../ $ pwd /home/vagrant/holberton $ cd - $ pwd /home/vagrant/holberton/simple_shell alias Usage: alias [NAME[='VALUE'] ...] Handles aliases. alias: Prints a list of all aliases, one per line, in the form NAME='VALUE'. alias NAME [NAME2 ...]: Prints the aliases NAME, NAME2, etc. one 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, its value is replaced with VALUE. Example:

$ ./shelltan $ alias show=ls $ show AUTHORS builtins_help_2.c errors.c linkedlist.c shell.h test README.md env_builtins.c getline.c locate.c shelltan alias_builtins.c environ.c helper.c main.c split.c builtin.c err_msgs1.c helpers_2.c man_1_simple_shell str_funcs1.c builtins_help_1.c err_msgs2.c input_helpers.c proc_file_comm.c str_funcs2.c 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:

$ ./shelltan $ exit env Usage: env Prints the current environment. Example:

$ ./shelltan $ 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:

$ ./shelltan $ setenv NAME Poppy $ echo $NAME Poppy unsetenv Usage: unsetenv [VARIABLE] Removes an environmental variable. Upon failure, prints a message to stderr. Example:

$ ./shelltan

$ exit env Usage: env Prints the current environment. Example:

$ ./shelltan $ 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:

$ ./shelltan $ setenv NAME Poppy $ echo $NAME Poppy unsetenv Usage: unsetenv [VARIABLE] Removes an environmental variable. Upon failure, prints a message to stderr. Example:

$ ./shelltan $ setenv NAME Poppy $ unsetenv NAME $ echo $NAME

$ Authors โœ’๏ธ adava onimisi modupe License ๐Ÿ”’ This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgements ๐Ÿ™ Shelltan emulates basic functionality of the sh shell. This README borrows form the Linux man pages sh(1) and dash(1).

This project was written as part of the curriculum for ALX Africa. ALX Africa SE course is a full-stack software engineering program that prepares students for careers in the tech industry using project-based peer learning. For more information, visit (https://www.alxafrica.com/).

-- INSERT --

simple_shell's People

Contributors

nomynameisjames avatar rozyrocks avatar

Watchers

 avatar

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.