项目作者: gogomillan

项目描述 :
C - Shell v2
高级语言: C
项目地址: git://github.com/gogomillan/shell_v2.git
创建时间: 2020-08-12T06:19:41Z
项目社区:https://github.com/gogomillan/shell_v2

开源协议:MIT License

下载




Simple Shell v 2.0

Simple Shell V 2.0 is a project based on the original sh “Borne shell” functionality,
but a small subset and allows us to understand Unix-like OS architecture. It was
developed on Ubuntu Linux and with C language.

OS Linux
distro Ubuntu
tecnology C

The concepts covered here are:

  • Who designed and implemented the original Unix operating system
  • Who wrote the first version of the Unix shell
  • How does a shell work
  • What are a pid and a ppid
  • How to manipulate the environment of the current process
  • What is the difference between a function and a system call
  • How to create processes
  • What are the three prototypes of main
  • How does the shell use the PATH to find the programs
  • How to execute another program with the execve system call
  • How to suspend the execution of a process until one of its children terminates
  • What is EOF / end-of-file?
  • How to redirect the content of a file into another file
  • What is a pipeline
  • How to setup a pipe
  • How to setup a pipe between two processes

Table of Contents

  1. Requirements
  2. Installation
  3. Testing
  4. Examples
  5. Authors
  6. License

Requirements

  • Linux Ubuntu 14.04 LTS
  • gcc (Ubuntu 4.8.4-2ubuntu1~14.04.4) 4.8.4
  • C language understanding

Installation

  1. Clone the repo in your local machine:

Example:

  1. vagrant@~$
  2. vagrant@~$ git clone https://github.com/gogomillan/shell_v2
  3. vagrant@~$
  1. Go to the directory and compile the task you want:

Example:

  1. vagrant@~$
  2. vagrant@~$ cd shell_v2
  3. vagrant@~$ make all
  4. vagrant@~$ ./hsh
  5. ($) ls
  6. addhist.c check_variables.c error_handler.c _help.c _hlphistory.c intfuncs.c myexec.c _setenv.c
  7. alias.c currhist.c _exit.c _history.c hsh loadhist.c path.c sizehist.c
  8. AUTHORS dockerfile _getenv.c _hlpcd.c hsh.h main.c README.md string_funcs.c
  9. _cd.c _env.c _getline.c _hlpexit.c inputfile.c Makefile savehist.c _strtok.c
  10. check_builtin.c env_funcs.c handsigint.c _hlphelp.c interact.c man_1_simple_shell script.sh _unsetenv.c
  11. ($) exit
  12. vagrant@~$

Testing

This shell work like this in interactive mode:

Example:

  1. vagrant@~$ ./hsh
  2. ($) /bin/ls
  3. hsh main.c shell.c
  4. ($)
  5. ($) exit
  6. vagrant@~$

But also in non-interactive mode:

Example:

  1. vagrant@~$
  2. vagrant@~$ echo "/bin/ls" | ./hsh
  3. hsh main.c shell.c test_ls_2
  4. vagrant@~$
  5. vagrant@~$ cat test_ls_2
  6. /bin/ls
  7. /bin/ls
  8. vagrant@~$
  9. vagrant@~$ cat test_ls_2 | ./hsh
  10. hsh main.c shell.c test_ls_2
  11. hsh main.c shell.c test_ls_2
  12. vagrant@~$

Examples

hsh 1.0 +

  • Handle the right stream redirection “>”
  • Usage: “command > output_file”
    1. gonza-juan-pc@~$ echo Holberton School > test
    2. gonza-juan-pc@~$ cat -e test
    3. Holberton School$
    4. gonza-juan-pc@~$

hsh 1.1 +

  • Handle the right stream redirection “>”
  • Usage: “command n> output_file”
    1. gonza-juan-pc@~$ echo Holberton School > test
    2. gonza-juan-pc@~$ cat -e test
    3. Holberton School$
    4. gonza-juan-pc@~$ echo Holberton School 1> test
    5. gonza-juan-pc@~$ cat -e test
    6. Holberton School$
    7. gonza-juan-pc@~$ echo Holberton School 2> test
    8. gonza-juan-pc@~$ cat -e test
    9. gonza-juan-pc@~$ ls /hbtn 2> test
    10. gonza-juan-pc@~$ cat -e test
    11. ls: cannot access /hbtn: No such file or directory$
    12. gonza-juan-pc@~$

hsh 1.1 +

  • Handle the double right stream redirection “>>”
  • Usage: “command >> output_file”
    1. gonza-juan-pc@~$ rm -f test
    2. gonza-juan-pc@~$ echo Holberton School >> test
    3. gonza-juan-pc@~$ cat -e test
    4. Holberton School$
    5. gonza-juan-pc@~$ echo Holberton School >> test
    6. gonza-juan-pc@~$ cat -e test
    7. Holberton School$
    8. Holberton School$
    9. gonza-juan-pc@~$

hsh 1.2 +

  • Handle the left stream redirection “<”
  • Usage: “command < output_file”
    1. gonza-juan-pc@~$ cat -e small_file
    2. Holberton$
    3. Second line$
    4. gonza-juan-pc@~$ rev < small_file
    5. notrebloH
    6. enil dnoceS
    7. gonza-juan-pc@~$

hsh 1.3 +

  • Handle the double left stream redirection << (also known as heredoc)
  • Usage: “command << delimiter”
    1. gonza-juan-pc@~$ cat -e << HOLBERTON
    2. > qwertyuiop
    3. > ls -l
    4. > cat -e small_file
    5. > HOLBERTONnope
    6. > nopeHOLBERTON
    7. > HOLBERTON
    8. > HOLBERTON
    9. qwertyuiop$
    10. ls -l$
    11. cat -e small_file$
    12. HOLBERTONnope$
    13. nopeHOLBERTON$
    14. HOLBERTON $
    15. gonza-juan-pc@~$

hsh 1.4 +

  • Handle the pipe stream redirection “|”
  • Usage: “command1 | command2”
    1. gonza-juan-pc@~$ ls /var | rev
    2. spukcab
    3. ehcac
    4. hsarc
    5. bil
    6. lacol
    7. kcol
    8. gol
    9. liam
    10. scirtem
    11. tpo
    12. nur
    13. loops
    14. pmt
    15. gonza-juan-pc@~$ ls -lr /var | cat -e
    16. total 44$
    17. drwxrwxrwt 2 root root 4096 Jul 5 10:26 tmp$
    18. drwxr-xr-x 9 root root 4096 Feb 17 2016 spool$
    19. lrwxrwxrwx 1 root root 4 Nov 19 2016 run -> /run$
    20. drwxr-xr-x 2 root root 4096 Feb 17 2016 opt$
    21. drwxrwsrwt 2 root whoopsie 4096 Feb 17 2016 metrics$
    22. drwxrwsr-x 2 root mail 4096 Feb 17 2016 mail$
    23. drwxrwxr-x 14 root syslog 4096 Jul 11 09:34 log$
    24. lrwxrwxrwx 1 root root 9 Nov 19 2016 lock -> /run/lock$
    25. drwxrwsr-x 2 root staff 4096 Apr 10 2014 local$
    26. drwxr-xr-x 70 root root 4096 Mar 21 13:06 lib$
    27. drwxrwsrwt 2 root whoopsie 4096 Jul 11 14:54 crash$
    28. drwxr-xr-x 17 root root 4096 Feb 17 2016 cache$
    29. drwxr-xr-x 2 root root 4096 Jul 11 09:33 backups$
    30. gonza-juan-pc@~$ echo "Holberton" | wc -
    31. 10
    32. gonza-juan-pc@~$

hsh 1.5 +

  • Handle the commands separator “;”
  • Usage: “command1 ; command2”
    1. gonza-juan-pc@~$ ls /var ; ls /var
    2. backups cache crash lib local lock log mail metrics opt run spool tmp
    3. backups cache crash lib local lock log mail metrics opt run spool tmp
    4. gonza-juan-pc@~$ ls /hbtn ; ls /var
    5. ls: cannot access /hbtn: No such file or directory
    6. backups cache crash lib local lock log mail metrics opt run spool tmp
    7. gonza-juan-pc@~$ ls /var ; ls /hbtn
    8. backups cache crash lib local lock log mail metrics opt run spool tmp
    9. ls: cannot access /hbtn: No such file or directory
    10. gonza-juan-pc@~$ ls /var ; ls /hbtn ; ls /var ; ls /var
    11. backups cache crash lib local lock log mail metrics opt run spool tmp
    12. ls: cannot access /hbtn: No such file or directory
    13. backups cache crash lib local lock log mail metrics opt run spool tmp
    14. backups cache crash lib local lock log mail metrics opt run spool tmp
    15. gonza-juan-pc@~$

hsh 1.6 +

  • Handle the “&&” logical operator
    1. gonza-juan-pc@~$ ls /var && ls /var
    2. backups cache crash lib local lock log mail metrics opt run spool tmp
    3. backups cache crash lib local lock log mail metrics opt run spool tmp
    4. gonza-juan-pc@~$ ls /hbtn && ls /var
    5. ls: cannot access /hbtn: No such file or directory
    6. gonza-juan-pc@~$ ls /var && ls /var && ls /var && ls /hbtn
    7. backups cache crash lib local lock log mail metrics opt run spool tmp
    8. backups cache crash lib local lock log mail metrics opt run spool tmp
    9. backups cache crash lib local lock log mail metrics opt run spool tmp
    10. ls: cannot access /hbtn: No such file or directory
    11. gonza-juan-pc@~$ ls /var && ls /var && ls /var && ls /hbtn && ls /hbtn
    12. backups cache crash lib local lock log mail metrics opt run spool tmp
    13. backups cache crash lib local lock log mail metrics opt run spool tmp
    14. backups cache crash lib local lock log mail metrics opt run spool tmp
    15. ls: cannot access /hbtn: No such file or directory
    16. gonza-juan-pc@~$

hsh 1.7 +

  • Handle the “||” logical operator
    1. gonza-juan-p@~$ ls /var || ls /var
    2. backups cache crash lib local lock log mail metrics opt run spool tmp
    3. gonza-juan-p@~$ ls /hbtn || ls /var
    4. ls: cannot access /hbtn: No such file or directory
    5. backups cache crash lib local lock log mail metrics opt run spool tmp
    6. gonza-juan-p@~$ ls /hbtn || ls /hbtn || ls /hbtn || ls /var
    7. ls: cannot access /hbtn: No such file or directory
    8. ls: cannot access /hbtn: No such file or directory
    9. ls: cannot access /hbtn: No such file or directory
    10. backups cache crash lib local lock log mail metrics opt run spool tmp
    11. gonza-juan-p@~$ ls /hbtn || ls /hbtn || ls /hbtn || ls /var || ls /var
    12. ls: cannot access /hbtn: No such file or directory
    13. ls: cannot access /hbtn: No such file or directory
    14. ls: cannot access /hbtn: No such file or directory
    15. backups cache crash lib local lock log mail metrics opt run spool tmp
    16. gonza-juan-p@~$

hsh 1.8 +

  • 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 a environment variable
      • Command syntax: unsetenv VARIABLE
      • Should print something on stderr on failure

hsh 1.0 +

  • Handle Ctrl+C: your shell should not quit when the user inputs ^C
    1. gonza-juan-pc@ubuntu:~/shell$ ./hsh
    2. $ ^C
    3. $ ^C
    4. $ ^C
    5. $ ls /var
    6. backups cache crash lib local lock log mail metrics opt run snap spool tmp
    7. $ ^C
    8. $ ^C
    9. $ ^C
    10. $ exit
    11. gonza-juan-pc@ubuntu:~/shell$

hsh 1.0 +

  • Handle Ctrl+C: your shell should not quit when the user inputs ^C

    • 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

Authors

Juan Marcos Cabezas

Gonzalo Gomez Millan

License

MIT License LICENSE